Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a UNIVERSAL Windows batch file timestamp

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%?

like image 768
eliocs Avatar asked Nov 17 '09 09:11

eliocs


People also ask

How do I timestamp a file in Windows?

Applications can retrieve and set the date and time a file is created, last modified, or last accessed by using the GetFileTime and SetFileTime functions.

How do I get the current date and time in CMD?

On a Microsoft Windows system, you can obtain the current date using the date /t command (the /t option prevents the command from prompting for a change to the the date) or by using echo %date% to display the contents of the date environment variable.

How do you display the current time in DOS?

Display or set the system time. TIME /T Key new_time : The time as HH:MM TIME with no parameters will display the current time and prompt for a new value. Pressing ENTER will keep the same time. /T : Just display the time, formatted according to the current Regional settings.


2 Answers

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.
like image 133
D Rickard Avatar answered Oct 04 '22 02:10

D Rickard


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
like image 20
ghostdog74 Avatar answered Oct 04 '22 02:10

ghostdog74