Solved

Date format DDMMYY

Posted on 2010-09-06
10
1,801 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 51

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 51

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 51

Expert Comment

by:Bill Prew
ID: 33616653
Thanks.

~bp
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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 video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

26 Experts available now in Live!

Get 1:1 Help Now