106

I need to get the date in YYYYMMDD format in batch file.

I am doing this using :

set mydate=%date:~6,4%%date:~3,2%%date:~0,2%
echo %mydate%

I need it to be consistent across system, even on changing the time settings.

Please advise.

BioGeek
  • 21,897
  • 23
  • 83
  • 145
user2061002
  • 1,069
  • 2
  • 7
  • 3
  • You may refer to [this][1] link for more information. [1]: http://stackoverflow.com/questions/14427490/how-to-set-date-from-cmd-after-retrieving-it/14435810#14435810 – Dale Feb 11 '13 at 12:36
  • 1
    echo %date:~4,2%-%date:~7,2%-%date:~10,4% – Ali Bayat Oct 07 '17 at 08:32
  • 3
    @AliBayat that doesn't work "consistent across systems" as the OP requires – phuclv Apr 16 '18 at 15:56

2 Answers2

62

If, after reading the other questions and viewing the links mentioned in the comment sections, you still can't figure it out, read on.

First of all, where you're going wrong is the offset.

It should look more like this...

set mydate=%date:~10,4%%date:~6,2%/%date:~4,2%
echo %mydate%

If the date was Tue 12/02/2013 then it would display it as 2013/02/12.

To remove the slashes, the code would look more like

set mydate=%date:~10,4%%date:~7,2%%date:~4,2%
echo %mydate%

which would output 20130212

And a hint for doing it in the future, if mydate equals something like %date:~10,4%%date:~7,2% or the like, you probably forgot a tilde (~).

Community
  • 1
  • 1
BDM
  • 3,760
  • 3
  • 19
  • 27
  • 18
    The offsets are not necessarily wrong: it all depends on your current regional settings for date/time. On my PC, %date% gives Sun 04/26/2014. There's no safe way that I can think of that would it safely this way – BxlSofty Apr 26 '14 at 08:53
  • 8
    Exclamation mark ! doesn't seem to work in DOS ?? – Anthony Hayward Nov 25 '14 at 18:12
  • 3
    Where the exclamation mark fails in DOS, replace it with a percent sign %. That worked for me anyhoo. – Alex Hall Jun 10 '16 at 02:55
  • 19
    This is a great approach. To get `yyyyMMdd` (e.g., 20161224), use `set CURRENT_DATE=%date:~10,4%%date:~4,2%%date:~7,2%`. – Parker Dec 24 '16 at 14:32
  • 1
    (1) exclamation mark does not work (2) doesn't work on other locales – Dims Feb 21 '17 at 08:00
  • 2
    This requires 'setlocal enabledelayedexpansion' – Patrick Sep 19 '17 at 03:48
  • @Yves. +1 for pointing that out. The rest of you guys: next time, use an example with a day greater than 12 (so it's clear that iy isn't the month). In the US, we use the inexplicable mm/dd/yyyy format -- neither least significant nor most significant part first. Breaking it down to make this clearer: set startYear=%date:~10,4%; set startmonth=%date:~4,2%; set startDay=%date:~7,2%, set startDate=%startYear%-%startMonth%-%startDay%; echo %startDate% gives 2018-03-25, which can be used as part of a filename which conveniently sorts alphabetically in date order. – riderBill Mar 25 '18 at 22:04
  • I don't see the point of using ! instead of %, unless the statement is inside a long running for loop, where the date might change during execution. OTOH, I prefer delayed expansion, too -- it prevents unexpected things from happening in the disaster of a scripting language that for whatever reason, we still use. – riderBill Mar 25 '18 at 22:12
  • 7
    @vallismortis this is a very defective approach. The OP has specifically said "I need it to be consistent across system, even on changing the time settings." – phuclv Apr 16 '18 at 14:29
  • @LưuVĩnhPhúc I've been using this approach for nightly backup/restore scripts for over a year on multiple systems in different time zones, and it has never failed for me. That said, I would be interested in an alternative answer that solves the issues you refer to. – Parker Apr 16 '18 at 17:13
  • @vallismortis it's absolutely not related to timezone but about [time format](https://en.wikipedia.org/wiki/Date_and_time_representation_by_country) – phuclv Apr 16 '18 at 17:18
  • As @phuclv said, this only works if you have configured your system to a specific date format (the default US format, I think). It doesn't work if %DATE% returns the format "20/11/2020" instead of "Fri 11/20/2020", which seems to be standard on UK systems, and I'm guessing most countries. I know this because I just worked out that's why a client's batch jobs aren't working on a new server. ;) – Steve Nov 18 '20 at 14:18
  • 1
    Apparently today is `2021/0/02`? -1 – user541686 Feb 02 '21 at 03:59
  • For anyone looking for quick hack and tldr: `echo %date:~10,4%%date:~7,2%%date:~4,2%-%time:~0,2%%time:~3,2%%time:~6,2%` – Rajesh Swarnkar Dec 15 '21 at 07:36
  • All of this depends on the current Windows date format settings and won't work in many cases. – Jim Garrison Sep 09 '22 at 00:55
1

You can try this ! This should work on windows machines.

for /F "usebackq tokens=1,2,3 delims=-" %%I IN (`echo %date%`) do echo "%%I" "%%J" "%%K"