-1

I want to get date in a particular format in my batch file. I have to pass it as parameter ahead. It should work on any machine. I have seen date parsing, but that becomes machine specific. How can I do it for machine independent.

Format for example is "2014-04-29 05:22:23"

Thankyou

Hackoo
  • 15,943
  • 3
  • 28
  • 59
User3091
  • 201
  • 5
  • 18
  • for date/time independent of local settings start from [here](http://stackoverflow.com/a/18024049/2152082). Splitting and inserting the `-` and `:` should be quite trivial. Ask, if you need help. – Stephan May 06 '16 at 09:35
  • Hey can you please explain what is this , for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I I tried running it on command prompt. Its giving me error – User3091 May 06 '16 at 09:41
  • on command line, use a single `%` (as described in the third comment [there](http://stackoverflow.com/questions/7727114/batch-command-date-and-time-in-file-name/18024049#comment41258167_18024049). `for /f` is a way to get the output of a command into a variable. – Stephan May 06 '16 at 09:47
  • Ok.. thanks. I will try and let you know – User3091 May 06 '16 at 09:49
  • 2
    Possible duplicate of [How to get current datetime on Windows command line, in a suitable format for using in a filename?](http://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-us) – aschipfl May 06 '16 at 10:04
  • @Stephan - Hey working. I will just add it as answer. Thankyou – User3091 May 06 '16 at 10:43

1 Answers1

0

Added in my batch file.

for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
set DATE_TIME_NOW=%datetime:~0,4%-%datetime:~4,2%-%datetime:~6,2% %datetime:~8,2%:%datetime:~10,2%:%datetime:~12,2%
echo %DATE_TIME_NOW%

The output is

2014-04-29 05:22:23
Stephan
  • 47,723
  • 10
  • 50
  • 81
User3091
  • 201
  • 5
  • 18