3

Date and Time on 2 computers:

10/25/2017 3:36:05 a.m. __ on Windows 7  
2017-10-25 03:07:23 ______ on Windows XP  

As regional settings change,
what is the recommended utility program and
batch file command to
automate an output, to a standard date time format?
( Rename directory R:\1 )

ren R:\1 R:\20171025_021459

R:\yyyyMMdd_HHmmss  
R:\20171025_021459  

2 Answers 2

3

How can I get a standard date time format that ignores regional settings?

Use the following batch file (which is the correct way to parse the local time output from wmic):

@echo off
setlocal
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1-6" %%g in (`wmic Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year ^| findstr /r /v "^$"`) do (
  set _day=00%%g
  set _hours=00%%h
  set _minutes=00%%i
  set _month=00%%j
  set _seconds=00%%k
  set _year=%%l
  )
rem pad with leading zeros
set _month=%_month:~-2%
set _day=%_day:~-2%
set _hh=%_hours:~-2%
set _mm=%_minutes:~-2%
set _ss=%_seconds:~-2%
rem use YYYYmmDD_HHmmSS format for the timestamp
set _stamp=%_year%%_month%%_day%_%_hh%%_mm%%_ss%
ren R:\1 %_stamp%
endlocal

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • for /f - Loop command against the results of another command.
  • getdate - Display the date and time independent of OS Locale, Language or the users chosen date format (Control Panel/Regional).
  • variables - Extract part of a variable (substring).
  • wmic - Windows Management Instrumentation Command.
3
  • I have always used WMIC this way to get the date and time. Everything comes out zero filled: for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
    – Squashman
    Oct 25, 2017 at 20:37
  • @Squashman That works as well. However, the way I do it is more flexible as you can re-order or omit components depending on your needs.
    – DavidPostill
    Oct 26, 2017 at 5:06
  • Sure, I will agree on that if the users question didn't need the date and time zero filled and in a different order then what get localdatetime outputs.
    – Squashman
    Oct 26, 2017 at 12:12
2

This is a shorter way to do what you specifically want to do. In my line of work Service Level Agreements are king and shaving off any amount of processing time regardless of how trivial, is what I will normally use.

@echo off
REM Get date and time in YYYYMMDDhhmmss format
for /f "tokens=2 delims==." %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
rem put underscore in the middle of date and time stamp
ren R:\1 %dt:~0,8%_%dt:~8,6%

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .