12

Is there a way to get time using a Windows command? If not, can the date and time commands output be modified?

For example date in Windows gives the date with / etc. I would like to get an output that has no special characters such as / :

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
moorecats
  • 3,242
  • 3
  • 21
  • 23
  • 2
    With *epoch time* -- do you mean the current count of all seconds since Jan 1, 1970? – Kurt Pfeifle Aug 12 '10 at 00:04
  • ... or since 1904-01-01 or 100 ns intervals since 1601-01-01 or various other epochs ;-) The UNIX epoch is by far the weirdest, imho ;-) – Joey Aug 12 '10 at 22:21
  • @moorecats: if my answer worked great to you, you can upvote it and then select it as the answer to your question. This is how StackOverflow works. See the FAQ - http://stackoverflow.com/faq. You can of course select other answer if you think it is a better fit for your question. – Leniel Maccaferri Aug 12 '10 at 23:18
  • Are you sure it is DOS you are using? Isn't it the Windows console? – jarnosz Apr 11 '16 at 03:23
  • ...but that is not Epoch time. Epoch time is the number of seconds since a certain Epoch (for the Unix Epoch, that would be 1970/01/01). – wibbuffey Dec 14 '21 at 22:42

7 Answers7

9

Use this command to show numbers of seconds after epoch.

(Cmd command)

powershell -command "(New-TimeSpan -Start (Get-Date "01/01/1970") -End (Get-Date)).TotalSeconds"

Benjamin2002
  • 311
  • 1
  • 4
  • 13
  • 2
    This doesn't return an integer value in the way the Unix `date +"%s"` would. But the decimal point and digits after it can be trimmed by casting to int32: `powershell -command "[int32](New-TimeSpan -Start (Get-Date "01/01/1970") -End (Get-Date)).TotalSeconds"` – AJM Nov 01 '19 at 15:25
  • 4
    In powershell/pwsh you can shorten this to : `[int32](New-TimeSpan "1/1/1970").TotalSeconds` Or from CMD: `powershell -command "[int32](New-TimeSpan "1/1/1970").TotalSeconds"` – Dennis Oct 05 '20 at 10:04
  • 2
    Note that (as the command suggests) this is PowerShell, not Batch. Some systems don't have PowerShell installed, so this might not work. (It is installed on most modern Windows systems though.) – wibbuffey Dec 20 '21 at 02:07
4

Through my own research online, I was not able to find a way to do this via a batch file directly. However, I was able to find this solution that worked for me:

In toEpoch.vbs:

WScript.Echo DateDiff("s", "01/01/1970 00:00:00", Now())

Then called from my batch script like so:

for /f "delims=" %%x in ('cscript /nologo toEpoch.vbs') do set epoch=%%x

That set the %epoch% variable with the current unix timestamp and I was able to use it as I needed to.

Hope this helps.

3

from the command line try this

for /f "tokens=2,3,4 delims=/ " %f in ('date /t') do @echo %h%g%f

remember to double up the % chars if in batch file

@echo off
setlocal
for /f "tokens=2,3,4 delims=/ " %%f in ('date /t') do set d=%%h%%g%%f
for /f "tokens=1,2 delims=: " %%f in ('time /t') do set t=%%f%%g
echo datetime is : %d%%t: =0%
endlocal

I got this output:

c:\development>xx.bat
datetime is : 201008111108

[Edited per Kurt Pfeifle's comment about spaces in time expansion]

Jesse Chisholm
  • 3,857
  • 1
  • 35
  • 29
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • And I get `datetime is : 2331` ;-) – Joey Aug 11 '10 at 21:31
  • @Preet Sangha: your `t` may contain leading blanks if the hour is before 10:00 h. You can fix that by `set t=%t: =0` (which substitutes all blanks with `0`)... – Kurt Pfeifle Aug 11 '10 at 23:57
  • @johannes : what does date /t return on your system? – Preet Sangha Aug 12 '10 at 21:24
  • ISO 8601 of course. This was just to highlight that such an approach is doomed to fail in the general case. – Joey Aug 12 '10 at 21:33
  • @Preet Sangha: I dunno how much SO 'credit' one needs to edit other people's answers... but I for sure don't have it. – Kurt Pfeifle Aug 13 '10 at 19:53
  • I get: datetime is : 20181020.16 – Jakob Sternberg Oct 03 '18 at 18:17
  • `for /f "tokens=1,2,3 delims=/ " %%f in ('date /t') do set d=%%h%%g%%f` i did get the day so i change this `tokens=2,3,4` into `tokens=1,2,3` – David-mu May 28 '20 at 12:21
  • @wibbuffey It would be really helpful if you explained - why you think this. The comment on its own doesn't help anyone. – Preet Sangha Dec 16 '21 at 23:15
  • @PreetSangha Since I can't edit the comment, I've just deleted it. Here's an explanation. An epoch is a date at which a system begins recording time. (For example, on most Unix systems, there is what is called the "Unix Epoch", which is January 1st, 1970 at 12:00 AM UTC.) The "epoch time" is the number of seconds or milliseconds (depends on the implementation) since that time. While the original question asked for something that your answer gave, the original question was NOT asking for Epoch time. – wibbuffey Dec 20 '21 at 02:04
  • 1
    @PreetSangha By the way, my primary source was [this](http://en.wikipedia.org/wiki/Epoch_(computing)), as I was unsure exactly how to explain epoch time to someone who had not heard of it. – wibbuffey Dec 20 '21 at 02:06
  • @wibbuffey Thank you for explaining. I completely see what you're referring to now. I've edited the question to make remove this inconsistency – Preet Sangha Jan 19 '22 at 01:03
  • 1
    @PreetSangha Thank ***you***! It is extremely helpful to the health of StackOverflow to edit questions like this. – wibbuffey Jan 21 '22 at 03:10
2

The CoreUtils for Windows project, http://gnuwin32.sourceforge.net/packages/coreutils.htm has a date command, which give you the same options as under Linux.

Download the software, and rename date.exe to gnudate.exe, to avoid a conflict with the Dos date command. You need the files libintl-2.dll and libiconv-2.dll to run the command.

For all available options type:

gnudate --help 

For example gnudate "+%a %e %b %Y %H:%M:%S" will give:

Sun 10 apr 2016 21:52:35

The command gnudate +%s will give the seconds since Epoch:

1460325461

The next Dos batch file shows the usage of gnudate. You will need to double the % in the gnudate +%s parameter.

rem set the variable s to the epoch seconds.
for /f "tokens=1 delims=" %%A in ('gnudate +%%s') do set s=%%A
rem use `%s%` for the time offset parameter of the ffmpeg drawtext filter.
ffmpeg -y -f lavfi -i testsrc=duration=15.3:size=cif:r=10 -vf "drawtext=fontfile=arial.ttf:text=%%{pts\\\:localtime\\\:%s%\\\:%%a %%d %%b %%Y %%H\\\\\\:%%M\\\\\\:%%S}:fontsize=10:x=w-text_w:y=h-lh:box=1" a.mp4

ffplay a.mp4

This batch file was tested with Windows 8 in a virtual machine under Linux.

To run it, you'll need to install ffmpeg.
You can download the Static build from https://ffmpeg.zeranoe.com/builds/.

Arjen Rodenhuis
  • 199
  • 1
  • 3
1

There is no reliable way of getting a date in batch files without resorting to external tools or other languages such as VBScript.

From VBScript you can access the current date and time with the Date and Time functions. FormatDateTime will get you a culture-neutral date/time format which you can then parse.

You can get a result frmo the VBScript by using WScript.Echo from within the script and calling it like so from the batch:

for /f "delims=" %%x in ('cscript /nologo foo.vbs') do set result=%%x

Then the variable %result% contains whatever the VBScript had as output in its first line.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • Rössel: Why do you say that *'there is no reliable way of getting a date in batch files'*? In which sense are `date/t` and `time/t` or `echo.%date%` and `echo.%time%` unreliable? -- (They may not be easy to process in batch files, but how are they 'unreliable'?) – Kurt Pfeifle Aug 11 '10 at 23:53
  • 1
    Rössel: Did you mean to say: *There is no reliable way of getting a date in batch files* **so that they work reliably across different locales and timezones** ?? – Kurt Pfeifle Aug 12 '10 at 00:02
  • @pipitas: Indeed, that's what I'm saying. Or in other words: You can get a date/time just fine to give it a human to read, but you can't reliably get the date or time in a structured format to process it further with the batch file. There exist some solutions which work for a larger variety of locales but still fail in some. Thus I call it unreliable. If I want to wriet a program and distribute it to others I certainly don't want the locale to affect its function. – Joey Aug 12 '10 at 09:21
0

To get time in the "yyyymmdd" format, try this:

for /F "tokens=2-4 delims=/ " %i in ('date /t') do echo %k%i%j

More on:

http://www.sprint.net.au/~terbut/usefulbox/msdoscmds.htm

Also you can do it this way:

echo %date:~10,4%%date:~4,2%%date:~7,2%
Mike Q
  • 6,716
  • 5
  • 55
  • 62
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
0

'date' separator is locale specific.

Using Java set 'TS' environment variable as Unix timestamp:

for /f %i in ('jrunscript -e "java.lang.System.out.println(java.lang.Long.toString(java.time.Instant.now().toEpochMilli()))"') do @set TS=%i
  • Whilst this may answer the question, it includes a non built-in program/utility in order to achieve it. The question specifically asked for a 'Windows command' and used only the tags [[tag:windows]] [[tag:batch-file]] and [[tag:cmd]]. Please therefore consider whether your answer is relevant, especially on a ten year old question without a [[tag:java]] tag. Perhaps even consider adding it to a more appropriately tagged question, or if you cannot locate one, create your own _(within the guidelines)_, then self-answer it, _(with the above)_. Thank you. – Compo Dec 14 '21 at 16:43