Get date in Windows CMD script

Apparently it depends on the Windows installation language, how the date function or %DATE% environment variable is formatted. So here is a very ugly hack to get a julian date (YYYYMMDDHHiiss) in a Windows batch script, independent off system locale settings:

  1. @echo off
  2. FOR /F "usebackq tokens=1-10 delims=+|NOW():- " %%a IN (`mysql -e "SELECT NOW();" -u root -ptest`) DO set datetime=%%a%%b%%c%%d%%e%%f
  3. echo %datetime%

Notice that this requires MySQL. If you do not like to use your MySQL root code, you can create a new user with absolutely no rights granted.

But again, this solution is stupid, I hope you will never need it or use it. I just thought I would share, since it was driving my co-worker crazy, and we ended up using it to name a MySQL dump.

This entry was posted in Batch, MySQL and tagged , , , , . Bookmark the permalink.

2 Responses to Get date in Windows CMD script

  1. corolla says:

    Thinking. It’s always the same thing. To think is to go crazy.

  2. Pingback: Read output from VBS in CMD scripts | 5p.dk

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>