48

I'm trying to write a cmd script that gets the current date and time and formats it into a way that sqlserver can input it as a datetime.

So far, I have:

@echo off
for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set dow=%%i
set mon=%%j
set day=%%k
set yr=%%l
set mydate=%%j/%%k/%%l
)

This prints out 10/22/2010

I have not been able to figure out how to get the time into a usable format. I tried working with time /t, but it only gives the hours and minutes, and I need the seconds also.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
chama
  • 5,973
  • 14
  • 61
  • 77

7 Answers7

76

Use the %TIME% pseudo-variable.

You can also use %DATE% in above script.

Both have the same limitations, in that they depend on your locale. The code is not portable and you'll be up for surprises in other environments. But that's how date and time in cmd works.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • thank you. Is there a way I can parse this so I can have the hours, minutes, and seconds in separate variables? – chama Nov 22 '10 at 18:19
  • You wrote the code in your question already, so you know the answer already. Just split at `:` or whatever your locale uses for separating time parts. – Joey Nov 22 '10 at 18:24
  • I had forgotten the last ). No wonder I couldn't figure out why it wasn't working... Thanks again! – chama Nov 22 '10 at 18:31
  • 3
    I was hoping to use this to see how many seconds a command takes... unfortunately `echo %time% && command & echo %time%` always prints the same value for time for both echos, even when the command is not instant. – cowlinator Oct 25 '19 at 01:10
  • 3
    @cowlinator: Use delayed expansion and `!time!`, as `%time%` gets evaluated and replaced by its value at _parsing_ time. – Joey Oct 25 '19 at 06:26
  • @Joey `echo !time!` doesn't work. Please give an exact code how to fix the cowlinator's example. Until then, I place downvote as this doesn't solve the question for me. – Tomas Jul 10 '23 at 08:27
  • @Tomas: `!time!` will work if you have enabled delayed expansion, either in a batch file with `setlocal enabledelayedexpansion` or in a separate `cmd` process started with `/v:on`. – Joey Jul 10 '23 at 10:13
  • @Joey thanks. I think you need to update the answer, as it seems incomplete like this. Also, is it possible that `setlocal enabledelayedexpansion` could break other programs/scripts which will be run from the console? – Tomas Jul 10 '23 at 14:49
21

Alternative approach where %TIME% is in format H:MM:SS.FF and H in range 0 - 23 and can be represented with one digit (based on this article):

set YYYY=%DATE:~10,4%
set MM=%DATE:~4,2%
set DD=%DATE:~7,2%

set HH=%TIME: =0%
set HH=%HH:~0,2%
set MI=%TIME:~3,2%
set SS=%TIME:~6,2%
set FF=%TIME:~9,2%

set dirName=%YYYY%%MM%%DD%
set fileName=%dirName%\%YYYY%%MM%%DD%_%HH%%MI%%SS%.tmp

echo %fileName%
aritz331_
  • 68
  • 2
  • 9
Stas Makutin
  • 219
  • 2
  • 4
7

Here is the way I went about doing my bat script:

@echo off
For /f "tokens=2-4 delims=/ " %%a in ("%DATE%") do (
    SET YYYY=%%c
    SET MM=%%a
    SET DD=%%b
)
For /f "tokens=1-4 delims=/:." %%a in ("%TIME%") do (
    SET HH24=%%a
    SET MI=%%b
    SET SS=%%c
    SET FF=%%d
)
echo %%DATE%%=%DATE%
echo %%TIME%%=%TIME%
echo %YYYY%-%MM%-%DD%_%HH24%-%MI%-%SS%-%FF%
echo %YYYY%/%MM%/%DD% %HH24%:%MI%:%SS%
echo %MM%/%DD%/%YYYY% %HH24%:%MI%:%SS%
echo YYYY=%YYYY%
echo MM=%MM%
echo DD=%DD%
echo HH24=%HH24%
echo MI=%MI%
echo SS=%SS%
echo FF=%FF%
pause

You can now remove all the echos and use this from any bat file by typing:

call SetDateTimeVariables.bat

Example: Creating an archive directory and storing the location:

@echo off
SETLOCAL
call test2.bat
ENDLOCAL&(
    SET ARCHIVE_DIR="archive/%YYYY%%MM%%DD%%HH24%%MI%%SS%"
)
MKDIR "%ARCHIVE_DIR%"
ScrappyDev
  • 2,307
  • 8
  • 40
  • 60
4
echo %time:~-5%

http://ss64.com/nt/syntax-substring.html

the last 5 characters of %time% is always seconds dot milliseconds

example output:

33.84
johny why
  • 2,047
  • 7
  • 27
  • 52
2
for /f %%# in ('wMIC Path Win32_LocalTime Get /Format:value') do @for /f %%@ in ("%%#") do @set %%@
echo %day%
echo %DayOfWeek%
echo %hour%
echo %minute%
echo %month%
echo %quarter%
echo %second%
echo %weekinmonth%
echo %year%

Output example:

10
0
8
3
10
4
5
3
2021

Source: https://www.it-swarm.com.de/de/windows/wie-erhalte-ich-die-aktuelle-uhrzeit-der-windows-befehlszeile-einem-geeigneten-format-fuer-die-verwendung-einem-dateinamen/958474138/

  • Numbers can be 0 padded as needed, e.g. `if %hour% LSS 10 set hour=0%hour%` - see https://superuser.com/a/277335/59640 – aland Aug 24 '23 at 22:26
1

try it

echo %date:~10,4%-%date:~4,2%-%date:~7,2%_%time:~0,2%-%time:~3,2%-%TIME:~6,2%

it will prints output in form of YYYY-MM-DD_HH-MM-SS

you can see below snapshot

enter image description here

Harsh Patel
  • 1,032
  • 6
  • 21
  • 1
    so it's `-3.-02_ 9-51-28` now in my part of the world? – Stephan Mar 05 '22 at 08:52
  • @Stephan I shared answer about how to get date, month, year, hour, minutes, seconds individually using that simple one line. actually I am working on auto DB backup schedule, so I was needed YYYY-MM-DD_HH-MM-SS format for save backup file in BATCH file. after many answer seen I found that simple one line solution for this. so I shared it for other people – Harsh Patel Mar 05 '22 at 09:03
  • I just shared the result of your code on my computer. Relying on locale user settings is the worst idea. Those settings are dependent on locale (language) and user preference and may be different or change, even on the same computer. There are [many methods independent of locale settings](https://stackoverflow.com/a/19799236/2152082) that may be less "elegant" but reliable and portable. – Stephan Mar 05 '22 at 09:36
  • This essentially a 1 line version of https://stackoverflow.com/a/21607440/131391 – aland Aug 20 '23 at 16:00
0

for /f "delims=-/,:._ tokens=1,2,3,4,5,6,7,8,9" %a in ("%DATE: =-%_%TIME: =0%") do @echo %a %b %c %d %e %f %g %h %i

will output something like:

Fri 09 19 1969 12 34 56 78

returns (parameters assigned):

%a = day_name .. %h = milliseconds (isn't it actually hundredths?)

(date and month might be swapped according to your locale)

Explanation:

  • change SPACE in %DATE% to become dash (-) to separate day_name
  • change SPACE in %TIME% to become 0 (sanitizer for hour under 10)
  • %DATE% and %TIME% separated by underscore (_) and quoted
  • delimiters are all those symbols

Yes, there's parameter %i or token no 9 that is empty, just to make a point that that does not matter.