Date Format Question

View: New views
4 Messages — Rating Filter:   Alert me  

Date Format Question

by habs3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi All

I am trying to get a date into a variable in a specific format. If i use %date% i get Mon 02/02/2009. however i need 20090202 as the format. Any Ideas would be great.



     

Re: Date Format Question

by giskier :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

--- In batchworld@..., habs3 <habs3@...> wrote:
>
> Hi All
>
> I am trying to get a date into a variable in a specific format. If
i use %date% i get Mon 02/02/2009. however i need 20090202 as the
format. Any Ideas would be great.
>

You can use the following command to format the date as you need it.  
If you are using this command inside a batch file, every percent sign
should appear twice (e.g.  %%p %%s, etc):

for /F "usebackq tokens=2-4 delims=^/ " %p in (`date /t`) do echo %r%
p%q

This will take the string 'Mon mm/dd/yyyy and break it in 4 tokens.  %
r will be yyyy, %p will be mm and %q will be dd.

To debug your batch files, visit http://www.steppingsoftware.com.  
Running Steps 1.1 is a really cool batch file debugger.

Thanks


GISkier


Re: Date Format Question

by Kenneth C. Mazie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Here's code that will allow you to manipulate the date as desired:

:: Extract the date and apply it to variables.
@echo off
for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
   set filedate=%%l-%%j-%%k
   set DayOfWeek=%%i
   set Month=%%j
   set Day=%%k
   set Year=%%l
   set Date=%%i %%j/%%k/%%l
)

You can use any or all of the variables to print the date in any form
you like.
Good luck.....
Ken Mazie


--- In batchworld@..., habs3 <habs3@...> wrote:
>
> Hi All
>
> I am trying to get a date into a variable in a specific format. If i
use %date% i get Mon 02/02/2009. however i need 20090202 as the
format. Any Ideas would be great.
>



Re: Re: Date Format Question

by foxidrive-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Here's another generic routine that caters for many different formats of
the local date.


@echo off
setlocal EnableExtensions
:: Date and time routines by Ritchie Lawrence
:: Windows NT4 and above
:begin
(set today=%date%)
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
  for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
  set %%a=%%d&set %%b=%%e&set %%c=%%f))
(set yy=%yy%&set mm=%mm%&set dd=%dd%)
for /f "tokens=5-8 delims=:. " %%a in ('echo/^|time') do (
  set hh=%%a&set nn=%%b&set ss=%%c&set cs=%%d)
if 1%hh% LSS 20 set hh=0%hh%
(set hh=%hh%&set nn=%nn%&set ss=%ss%&set tt=%cs%)
if not "%date%"=="%today%" goto :begin

:: The following lines set and echo the timestamp variable
set timestamp=%yy%-%mm%-%dd%_%hh%.%nn%.%ss%
echo timestamp=%timestamp%