• 0

Dates format in batch files


Question

Hello

I've been compiling a batch file that echos the date according to the following format: YYYY-MM-DD. I have successfully worked it out, and it works fine on my computer; However, when I run the file on a different PC, my code will not get the date properly.

After investigation, I concluded that the problem is in the date format set on each PC. While on my computer the date format is MM-DD-YYYY on the other computer the date is DD-MM-YYYY.

So my question is, what should I do in a batch file so that whenever I want to retrieve a date through %date% it would follow a certain format in order for me to be able to select the days and months in a standard way. In fewer words, how can I make the code compatible with whatever computer no matter what the selected date format is on it.

My code

echo %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%

Thank you..

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Here try this, it a Batch that creates a VBS file that passes the date format back to the original batch file.

@Echo Off
CLS
Mode 55,5
Color F9
Title Time Format
Set "Cmd=%Temp%\Time.cmd"
Set "VBS=%Temp%\T.vbs"

Echo  Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject") > %VBS%
Echo  Dim Dy, Mth, Ts >> %VBS%
Echo   Mth = Month(Now) >> %VBS% 
Echo   Dy = Day(Now) >> %VBS% 
Echo   If Len(Dy) = 1 Then Dy= "0"  ^& Dy >> %VBS% 
Echo   If Len(Mth) = 1 Then Mth= "0" ^& Mth >> %VBS% 
Echo   Set Ts = Fso.CreateTextFile("%Cmd%") >> %VBS%
Echo	Ts.Writeline "Set T=" ^& Year(Now) ^& "/" ^& Mth ^& "/" ^& Dy >> %VBS% 
Echo.   Ts.Close >> %VBS%

Start /w %VBS%
Call %Cmd%
Echo.
Echo Time Stamp ^> %T%
Pause
Del %VBS%
Del %Cmd%
Link to comment
Share on other sites

  • 0

The only way to get less lines for the code would be to make it a vbs script only.

Dim Dy, Mth
 Dy = Day(Now)
 Mth= Month(Now)
 If Len(Dy) = 1 Then Dy = "0" & Dy
 If Len(Mth) = 1 Then Mth = "0" & Mth
 WScript.Echo Year(Now) & "/" & Mth & "/" & Dy

This script could be a couple of lines smaller but I have it checked the length of the day and

month varible and if it 1 character then it adds a zero to it to make it 2 characters

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.