8

I'm having trouble generating a timestamp in a Windows batch file, because I get diferent date formats on different Windows versions.

My machine:

>echo %date%
>Tue 11/17/2009

Friends machine:

>echo %date%
>11/17/2009

I guess there has to be some way of getting the date (11/17/2009) from both strings using for /f. I've been trying and googling and can't find the answer.

Is there another way to get a timestamp without using %date%?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
eliocs
  • 18,511
  • 7
  • 40
  • 52

5 Answers5

4

Check out doff.exe. I use this a lot for getting timestamps for naming log files. From its web site:

DOFF prints a formatted date and time, with an optional date offset, (e.g -1 prints yesterday's date, +1 prints tomorrow's date). To view all the options available, execute "doff -h". I typically use this utility for renaming log files so that they include a timestamp, (see the third example below). This code should compile under Unix/Linux, as well as DOS.

Sample commands:

C:\>doff
19991108131135

With no parameters the output is the current date/time in the following format: yyyymmddhhmiss

C:\>doff mm/dd/yyyy
11/08/1999

In the above example a date format specification is given.

@echo off
for /f "tokens=1-3 delims=/ " %%a in ('doff mm/dd/yyyy -1') do (
set mm=%%a
set dd=%%b
set yyyy=%%c)
rename httpd-access.log httpd-access-%yyyy%%mm%%dd%.log

The sample batch file above shows a neat way to rename a log file based on yesterday's date. The "for" command executes doff to print yesterday's date, (the "-1" parameter specifies yesterday), then extracts each component of the date into DOS batch file variables. The "rename" command renames "httpd-access.log" to "httpd-access-[yesterday's date].log"


Also check out Microsoft's now.exe, available in the Windows Server 2003 Resource Kit Tools. One bad thing I found out (the hard way) about it is it sets the ERRORLEVEL to the number of characters printed.

Looks like this:

c:\>now

Thu May 19 14:26:45 2011

Help:

NOW   :  Display Message with Current Date and Time

Usage : NOW [message to be printed with time-stamp]

    NOW displays the current time, followed by its command-line arguments.
    NOW is similar to the standard ECHO command, but with a time-stamp.
Community
  • 1
  • 1
D Rickard
  • 153
  • 1
  • 9
2

Use VBScript if you want to get independent date time settings:

thedate = Now
yr = Year(thedate)
mth = Month(thedate)
dy = Day(thedate)
hr = Hour(thedate)
min = Minute(thedate)
sec = Second(thedate)
WScript.Echo yr&mth&dy&hr&min&sec
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • This is really usefull used inside the script by: >cscript date.vbs //Nologo (where date.vbs contains the code you included) – eliocs Nov 17 '09 at 11:18
  • Doesn't this just return the local datetime? The question was to generate the UTC datetime - and is what I'm looking for. – Jim2B Jul 26 '16 at 15:27
1

Unfortunately, it can't be done directly, so you need to resort to hacks like GetDate.cmd.

There are lots of VBScript and small external commandline tools available too, which isn't something I'd take a dependency on unless you're already using something of that nature in your overall system.

Personally, I'd be trying to route around it by using PowerShell which neatly sidesteps the issue completely.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
0

As I have posted in here: Batch: Timestamp to UNIX Time

What about simple 1-line long C program returning UNIX timestamp? You can retrieve value from %errorlevel% in batch script.

#include <stdio.h>
#include <time.h>

int main(void)
{
    return (int) time(NULL);
}

In my test in command prompt it worked:

C:\Users\dabaran\Desktop\cs50\src\C>.\time || echo %errorlevel% && set mytstamp=%errorlevel%
1419609373

C:\Users\dabaran\Desktop\cs50\src\C>echo %mytstamp%
1419609373
Community
  • 1
  • 1
damian1baran
  • 1,307
  • 1
  • 14
  • 13
0

You don't need VBScript. You can do it with something like this:

echo %date:~-10,2%/%date:~-7,2%/%date:~-4,4%

Source

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Darren
  • 17
  • 1