How do I get current datetime on the Windows command line, in a suitable format for using in a filename? -


update: it's 2016 i'd use powershell unless there's compelling backwards-compatible reason it, particularly because of regional settings issue using date. see @npocmaka's https://stackoverflow.com/a/19799236/8479


what's windows command line statement(s) can use current datetime in format can put filename?

i want have .bat file zips directory archive current date , time part of name, example, code_2008-10-14_2257.zip. there easy way can this, independent of regional settings of machine?

i don't mind date format, ideally it'd yyyy-mm-dd, simple fine.

so far i've got this, on machine gives me tue_10_14_2008_230050_91:

rem datetime in format can go in filename. set _my_datetime=%date%_%time% set _my_datetime=%_my_datetime: =_% set _my_datetime=%_my_datetime::=% set _my_datetime=%_my_datetime:/=_% set _my_datetime=%_my_datetime:.=_%  rem use timestamp in new zip file name. "d:\program files\7-zip\7z.exe" -r code_%_my_datetime%.zip code 

i can live this, seems bit clunky. ideally it'd briefer , have format mentioned earlier.

i'm using windows server 2003 , windows xp professional. don't want install additional utilities achieve (although realise there nice date formatting).

see windows batch file (.bat) current date in mmddyyyy format:

@echo off /f "tokens=2-4 delims=/ " %%a in ('date /t') (set mydate=%%c-%%a-%%b) /f "tokens=1-2 delims=/:" %%a in ('time /t') (set mytime=%%a%%b) echo %mydate%_%mytime% 

if prefer time in 24 hour/military format, can replace second line this:

for /f "tokens=1-2 delims=/:" %%a in ("%time%") (set mytime=%%a%%b) 

c:> .\date.bat
2008-10-14_0642

if want date independently of region day/month order, can use "wmic os localdatetime" source, since it's in iso order:

@echo off /f "usebackq tokens=1,2 delims==" %%i in (`wmic os localdatetime /value 2^>nul`) if '.%%i.'=='.localdatetime.' set ldt=%%j set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2% %ldt:~8,2%:%ldt:~10,2%:%ldt:~12,6% echo local date [%ldt%] 

c:>test.cmd
local date [2012-06-19 10:23:47.048]


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -