47

I have a batch script that outputs a file, and I'm trying to ensure that each time the script is executed, no existing files are overwritten, so I'm trying to put a timestamp on it.

Currently I have this:

set  stamp=%DATE:/=-%_%TIME::=-%

But if the time is 1-9 AM, it gives something like:

13-06-2012_ instead of a full 13-06-2012_12-39-37.28

How can I fix this?

I'm using Windows 7, and the output of echo %date% %time% in a command line window is (my clock format for 'short date' is set to display 3-letter months):

03-Sep-12 9:06:21.54

Basically I want a solution that solves the issue regardless of what the clock format is set to.


Edit: Since no one likes to read past the title, I will explicitly state this question is about a truncation issue. And I found a solution.

I've been using the following timestamp for a good while now, works well.

set timestamp=%DATE:/=-%_%TIME::=-%
set timestamp=%timestamp: =%

It produced a timestamp like: 18-03-2013_13-37-43.26, by replacing / and : in %TIME% and %DATE%, then stripping white space. The whitespace was the problem in my original question, really.

bryc
  • 12,710
  • 6
  • 41
  • 61
  • Does this answer your question? [How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?](https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format) – feeela Mar 23 '20 at 11:30
  • @feeela That does not solve or explain the truncation issue. See [my accepted answer](https://stackoverflow.com/a/11038702/815680). – bryc Mar 24 '20 at 00:38

9 Answers9

77

The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher, using WMIC.

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause

Output example:

datestamp: "20200828"
timestamp: "085513"
fullstamp: "2020-08-28_08-55-13"
Press any key to continue . . .
MrCalvin
  • 1,675
  • 1
  • 19
  • 27
foxidrive
  • 40,353
  • 10
  • 53
  • 68
13

See Stack Overflow question How to get current datetime on Windows command line, in a suitable format for using in a filename?.

Create a file, date.bat:

@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-3 delims=/:/ " %%a in ('time /t') do (set mytime=%%a-%%b-%%c)
set mytime=%mytime: =% 
echo %mydate%_%mytime%

Run date.bat:

C:\>date.bat
2012-06-14_12-47-PM

UPDATE:

You can also do it with one line like this:

for /f "tokens=2-8 delims=.:/ " %%a in ("%date% %time%") do set DateNtime=%%c-%%a-%%b_%%d-%%e-%%f.%%g

Community
  • 1
  • 1
Nicholas DiPiazza
  • 10,029
  • 11
  • 83
  • 152
  • 1
    sorry that seems to return an invalid date as well, i got `-06-2012_01` – bryc Jun 14 '12 at 17:25
  • 2
    Why not just do it all on one line? `for /f "tokens=2-8 delims=.:/ " %%a in ("%date% %time%") do set DateNtime=%%c-%%a-%%b_%%d-%%e-%%f.%%g` This example uses the 24 Hour clock rather than 12 Hours + AM/PM, and includes the miliseconds. If you don't want the miliseconds, just don't use `%%g`. – James K Aug 29 '12 at 16:06
  • hi james i put your answer onto my answer – Nicholas DiPiazza May 05 '14 at 14:27
  • 1
    This answer assumes the computer is set to US date formatting and doesn't work on other systems without modification – silver Aug 11 '20 at 03:04
7

Here's a batch script I made to return a timestamp. An optional first argument may be provided to be used as a field delimiter. For example:

c:\sys\tmp>timestamp.bat
20160404_144741
c:\sys\tmp>timestamp.bat -
2016-04-04_14-45-25
c:\sys\tmp>timestamp.bat :
2016:04:04_14:45:29

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:: put your desired field delimiter here.
:: for example, setting DELIMITER to a hyphen will separate fields like so:
:: yyyy-MM-dd_hh-mm-ss
::
:: setting DELIMITER to nothing will output like so:
:: yyyyMMdd_hhmmss
::
SET DELIMITER=%1

SET DATESTRING=%date:~-4,4%%DELIMITER%%date:~-7,2%%DELIMITER%%date:~-10,2%
SET TIMESTRING=%TIME%
::TRIM OFF the LAST 3 characters of TIMESTRING, which is the decimal point and hundredths of a second
set TIMESTRING=%TIMESTRING:~0,-3%

:: Replace colons from TIMESTRING with DELIMITER
SET TIMESTRING=%TIMESTRING::=!DELIMITER!%

:: if there is a preceeding space substitute with a zero
echo %DATESTRING%_%TIMESTRING: =0%
Emile Mercier
  • 216
  • 2
  • 3
3
for /f "tokens=2-8 delims=.:/ " %%a in ("%date% %time: =0%") do set DateNtime=%%c-%%a-%%b_%%d-%%e-%%f.%%g
echo %DateNtime%

Or, from the command line:

for /f "tokens=2-8 delims=.:/ " %a in ("%date% %time: =0%") do echo %c-%a-%b_%d-%e-%f.%g 

EDIT: As per bryce's non-standard time/date specs. (03-Sep-12 9:06:21.54)

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-7 delims=.:/- " %%a in ("%date% %time%") do (
  if "%%b"=="Jan" set MM=01
  if "%%b"=="Feb" set MM=02
  if "%%b"=="Mar" set MM=03
  if "%%b"=="Apr" set MM=04
  if "%%b"=="May" set MM=05
  if "%%b"=="Jun" set MM=06
  if "%%b"=="Jul" set MM=07
  if "%%b"=="Aug" set MM=08
  if "%%b"=="Sep" set MM=09
  if "%%b"=="Oct" set MM=10
  if "%%b"=="Nov" set MM=11
  if "%%b"=="Dec" set MM=12
  set HH=0%%d
  set HH=!HH:~-2!
  echo 20%%c-!MM!-%%a_!HH!-%%e-%%f.%%g
)
endlocal
James K
  • 4,005
  • 4
  • 20
  • 28
2

Thanks to an answer to Stack Overflow quesion Creating a file name as a timestamp in a batch job, I found that it was a space terminating the filename.

Community
  • 1
  • 1
bryc
  • 12,710
  • 6
  • 41
  • 61
1

Windows batch log file name in 2018-02-08_14.32.06.34.log format:

setlocal
set d=%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%
set t=%time::=.% 
set t=%t: =%
set logfile="%d%_%t%.log"

ping localhost -n 11>%1/%logfile%
endlocal
beloblotskiy
  • 948
  • 9
  • 7
0

It wants the full time in DD-MM-YYYY_HH-MM-SS.TT where TT is the ticks. The exception says it all.

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
0

In the past, I've used a .cmd script I found on the Internet. I hate the way localization normally messes with dates. Anytime you have dates in filenames (or anywhere else, if I may be so bold) I figure you want them in ISO 8601 format:

2015-02-19T14:54:51Z

or something else that has Y M D H M in that order, such as

2015-02-19 14:54

because it fixes the MDY / DMY ambiguity and because it's sortable as text.

I don't know where I got that .cmd script, but it may have been http://ss64.com/nt/syntax-getdate.html, which works beautifully on my YYYY-MM-DD Windows 8.1 and on a M/D/YYYY vanilla install of Windows 7. Both give the same format:

2015-02-09 04:43

Mathieu K.
  • 283
  • 3
  • 14
0

BATCH/CMD FILE like DateAndTime.cmd (not in CMD-Console)

Code:

SETLOCAL EnableDelayedExpansion
(set d=%date:~8,2%-%date:~3,2%-%date:~0,2%) & (set t=%time::=.%) & (set t=!t: =0!) & (set STAMP=!d!__!t!)

Create output:

echo %stamp%

Output:

2020-02-25__08.43.38.90

Or also possible in for lines in CMD-Console and BATCH/CMD File

set d=%date:~6,4%-%date:~3,2%-%date:~0,2%
set t=%time::=.%
set t=%t: =0%
set stamp=%d%__%t%

"Create output" and "Output" same as above

Anubis
  • 1