0

How does one get yesterday's date format in a batch file?

I'd like it to look like so: M_d_yyyy

Note that if there's a single digit day and month, I'd like it to be single digits.

Example: 8_5_2013 is August 5th, 2013.

I looked around for a few days but couldn't find a solution.. any lead is much appreciated.

sojim
  • 492
  • 2
  • 6
  • 19
  • possible duplicate of [Yesterdays Date in Batch File](http://stackoverflow.com/questions/5017701/yesterdays-date-in-batch-file) – Endoro Sep 24 '13 at 00:38
  • 2
    Stack Overflow is a question and answer site for professional and enthusiast programmers. Questions must **demonstrate a minimal understanding of the problem being solved**. Tell us what you've tried to do, why it didn't work, and how it _should_ work. See also: [Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist). – Endoro Sep 24 '13 at 00:43
  • I needed to get yesterday's date, the "Yesterdays Date in Batch File" example requires 2 pages of code inserted. I wanted something that's short and simple, and Mike's solution was just that. I never heard of UnxUtils until Mike mentioned it to me, he just opened a door of many possibilities with that collection. I searched around for a few days for the solution and only asked a question as a last resort. I already found the suggested answer long ago, it did not solve my specific problem. – sojim Sep 25 '13 at 07:47

2 Answers2

3

Nothing wrong with free 3rd party executables, but some of us are not allowed to use them on our work machines.

I have written a powerful hybrid JScript/batch utility called getTimestamp.bat that can do nearly any date and time computation on a Windows machine.

There are a great many options for specifying the base date and time, many options for adding positive or negative offsets to the date and time, many options for formatting the result, and an option to capture the result in a variable. Both input and output can be directly expressed as local time, UTC, or any time zone of your choosing. Full documentation is embedded within the script.

The utility is pure script that will run on any modern Windows machine from XP forward - no 3rd party executable required.

Assuming getTimestamp.bat is in your current directory, or better yet, somewhere within your PATH, then the following simple call will define a dt variable containing yesterday's date in M_D_YYYY format:

call getTimestamp -od -1 -f {m}_{d}_{yyyy} -r dt

Note: when I put a date in a file name, I like to use YYYY_MM_DD format because that format will sort chronologically when getting a directory listing.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Dave, can it solve the OP's question using month name and day with 1st/2nd/3rd/4th nomenclature? Of course some simple code could change the month name. – foxidrive Sep 24 '13 at 12:02
  • @foxidrive - The utility already supports month names, but it would have to be modified to support ordinal number output. However, the answer as is gives the OP's desired result, as evidenced by the accepted answer. The OP wants `8_5_2013`. The `August 5th, 2013` is just to make clear how the numeric string is to be interpretted. – dbenham Sep 24 '13 at 13:49
  • Another great solution for those who aren't allowed to use 3rd party executables. Agreed with the YYYY_MM_DD format reasoning, I do that too, this isn't my call though! – sojim Sep 25 '13 at 07:33
  • This is cool in lots of ways: the fact that it doesn't rely on external utilities, and the hybrid JS/batch technique (which I've seen before, but forgot existed). Why do you prefer the hybrid technique over just writing a .JSE? Doesn't PATHEXT contain .jse by default? – Mike Clark Sep 25 '13 at 22:12
  • @MikeClark - Mostly because I like the convenience of calling the utility directly instead of requiring CSCRIPT //nologo. The CSCRIPT is required, despite PATHEXT, because the system's default may be WSCRIPT. I perform much of the functionality in batch instead of JScript only because I am expert at batch, and only dabble with JScript. Even if I put more logic in JScript, I would still use a hybrid script to provide option for output as environment variable, and to hide CSCRIPT //NOLOGO. – dbenham Sep 25 '13 at 23:22
  • @dbenham Thanks. The cscript/wscript difference explains it. It's my understanding that .jse is associated with wscript by default, and wscript does not have stdout. That's reason enough. Nice work! – Mike Clark Sep 25 '13 at 23:32
  • @dbenham I tested your solution and it's giving me blank responses. I can execute getTimestamp ok but when I call it with switches, it returns blank. – sojim Oct 07 '13 at 09:52
  • @sojim - The switches given in this answer are supposed to result in no visible output. The `-r dt` option caused the result to be stored in the `dt` variable so that it can be used by your batch script. `ECHO dt` will display the result. Type `getTimeStamp -?` from the command prompt to get full help on how the utility works. – dbenham Oct 07 '13 at 12:14
2

I think you should get date.exe from UnxUtils.

date.exe --date="1 day ago" "+%-m_%d_%Y"

Download: http://sourceforge.net/projects/unxutils/files/unxutils/current/
Man page: http://www.ss64.com/bash/date.html


@echo off
setlocal
set magic="c:\unx\usr\local\wbin\date.exe" --date="1 day ago" "+%%-m_%%d_%%Y"
for /f %%i in ('%magic%') do set yesterdate=%%i
echo yesterdate = %yesterdate%

If you want to do it with just batch language, you'll end up with nearly 100 lines of incomprehensible batch code. UPDATE: or use dbenham's hybrid batch/JScript solution posted in the answer below, which at least uses sane Windows APIs.


See Also: How to get current datetime on Windows command line, in a suitable format for using in a filename?

Community
  • 1
  • 1
Mike Clark
  • 10,027
  • 3
  • 40
  • 54
  • I tried to set it as a variable: `set yesterdate=E:\Progra~2\UnxUtils\usr\local\wbin\date.exe --date="1 day ago" "+%-m_%e_%Y" echo %yesterdate%` But it's just outputting the command. Any idea why? – sojim Sep 24 '13 at 02:27
  • Because you can't use set like that to just capture the output of a command. Batch language is obtuse. I updated my answer. – Mike Clark Sep 24 '13 at 02:52
  • I've got to learn that for and "usebackq" command.. this is why i love Linux lol.. windows environment is OBTUSE! – sojim Sep 24 '13 at 03:07
  • 1
    It doesn't need `usebackq` when you use '%magic%' with regular ticks. – foxidrive Sep 24 '13 at 03:35
  • date.exe is an unfortunate name for a Windows utility as it conflicts with the internal DATE command. – dbenham Sep 24 '13 at 11:16
  • @MikeClark Right now it's outputting `10_ 6_2013` looks like it took out the zero but not the space. – sojim Oct 07 '13 at 09:59
  • It's blank padded thats why hmm.. couldn't find day of month without blank padded.. I see that %d has 0 padded. – sojim Oct 07 '13 at 10:08
  • @sojim Sorry, I should have caught that. Use `%d` instead of `%e`. http://unixhelp.ed.ac.uk/CGI/man-cgi?date. `%d` outputs 01, 02, ..., 31. – Mike Clark Oct 07 '13 at 17:02