Solved

Date format DDMMYY

Posted on 2010-09-06
10
1,810 Views
Last Modified: 2012-08-13
Hello

I need some DOS code to get the following date format DDMMYY, can anyone provide?

Thanks!
0
Comment
Question by:SickBoy23
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 18

Expert Comment

by:x-men
ID: 33612426
c:\>date /t

returns the date, the format is set on regional settings under short date format...

I'm not sure about the intent of the question, can you clarify?
0
 

Author Comment

by:SickBoy23
ID: 33612460
date /t provides in format 06/09/2010, I need in format 060910.  I intend to use this for scripting purposes however need in format DDMMYY

Thanks!
0
 
LVL 18

Expert Comment

by:x-men
ID: 33612587
something like this?
set z=06/09/2010

for /f "tokens=1,2,3 delims=/" %i in ('echo %z%') do echo %i%j%k

Open in new window

0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 100 total points
ID: 33612628
This is another way if you look at my article here that works independent of date format on the pc:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html
Ask if you need more help customising.

Steve
0
 
LVL 52

Assisted Solution

by:Bill Prew
Bill Prew earned 150 total points
ID: 33612772
If you are okay assuming the date format on that maching is mm/dd/yyyy then you could just do:

set MyDate=%DATE:~-10,2%%DATE:~-7,2%%DATE:~-4,2%

~bp
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 95

Accepted Solution

by:
Lee W, MVP earned 250 total points
ID: 33613521
Expanding on Billprew's comment, Windows puts the current date (and the current time) into variables - %date% and %time%.

You can use these variables, but they tend to change with regional settings.  Run "ECHO %DATE%" to see how %date% is stored and then you can pull out only the aspects of the variable you want - for example, for me, I get the following when I run echo %date% (the numbers beneath in the code section illustrate the line positions)

So if I want the Month, I want to start at the 4th character and take two characters:
Month=%Date:~4,2%
The Day starts are character 7 and requires 2 digits
Day=%Date:~7,2%
And the two digit year is the last two characters - the "-" meaning starting from the reverse end of the string take everything else
Year=%Date:~-2%
And the four digit year is the last four characters
Year=%Date:~-4%

Hopefully, you can use the above and below to learn how to retrieve portions of the variable and that should answer your question. I will add that using the For... date/t method as described above is potentially better in scripts that need to be used by others in other regions.  Once you hard code DATE:~ lines, you expect the region never to change - you could do some if statements to determine the region, but then you're making things more complicated.

Mon 09/06/2010 [produced from an "ECHO %DATE%"]
00000000001111
01234567890123

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33613827
To add to that slightly even on the same machine if you have a scipt running nicely then decide to schedule it as an admin user etc. then it may well have a different profile with default date/time formats.  It might not catch you out until you realise that it uses " 1/ 1/2010" instead of "01/01/2010" or " 1- 1-2010" etc. so justbe careful!

Steve
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 33614724
Here's a routine you could use, that sets variables m, mm, d, dd, yy, yyyy which can be referenced in the script as shown in the sample echo.  Hope this helps.

~bp
@echo off
call :GetDate
echo %m%/%d%/%yy%
echo %mm%/%dd%/%yyyy%
exit /b
 
:GetDate
  REM Get date format settings from registry
  for /F "tokens=3" %%A in ('reg query "HKCU\Control Panel\International" /v iDate 2^>NUL') do set "iDate=%%A"
  for /F "tokens=3" %%A in ('reg query "HKCU\Control Panel\International" /v sDate 2^>NUL') do set "sDate=%%A"
 
  REM Get the current system date (ignore day name if present)
  for %%A in (%Date%) do set "Today=%%A"
 
  REM Parse current date based on delimiter from registry, and assign to desired user variable
  for /F "tokens=1-3 delims=%sDate%" %%A in ("%Today%") do (
    if "%iDate%"=="0" set /a "d=1%%B-100" & set /a "m=1%%A-100" & set "yyyy=%%C"
    if "%iDate%"=="1" set /a "d=1%%A-100" & set /a "m=1%%B-100" & set "yyyy=%%C"
    if "%iDate%"=="2" set /a "d=1%%C-100" & set /a "m=1%%B-100" & set "yyyy=%%A"
  )
  set "mm=%m%"
  set "dd=%d%"
  set "yy=%yyyy:~-2%"
  if %m% LSS 10 set "mm=0%m%"
  if %d% LSS 10 set "dd=0%d%"
  exit /b
)

Open in new window

0
 

Author Closing Comment

by:SickBoy23
ID: 33615557
Thanks Lee your explanation provides a great understanding which will allow me to manipulate the dates in any format I wish thanks!

Cherrs Bill and the rest of the guys. Points awarded hopefully fairly.
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 33616653
Thanks.

~bp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now