tags:

views:

83

answers:

3

Is there a way to get epoch time using a dos command? If not, can the date, time dos command output be modified?

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

A: 

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

Leniel Macaferi
This worked great.the article was great, thanks for pointing that to me.
moorecats
+1  A: 

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

I got this output:

c:\development>xx.bat
datetime is : 201008111108
Preet Sangha
And I get `datetime is : 2331` ;-)
Joey
@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`)...
pipitas
@pip - sweet! thank you. Feel free to just edit the answer.
Preet Sangha
@johannes : what does date /t return on your system?
Preet Sangha
ISO 8601 of course. This was just to highlight that such an approach is doomed to fail in the general case.
Joey
Thanks everyone, I used this solution and it worked great
moorecats
@Preet Sangha: I dunno how much SO 'credit' one needs to edit other people's answers... but I for sure don't have it.
pipitas
A: 

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
@Johannes 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'?)
pipitas
@Johannes 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** ??
pipitas
@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