Batch: Timestamp to UNIX Time
NickName:beary605 Ask DateTime:2012-07-09T01:13:31

Batch: Timestamp to UNIX Time

For all I know, Batch does not have a command that gives the UNIX time. The closest one I can find is %time%, which only displays the timestamp.

Is there a command, or set of commands in Batch with which you can get the UNIX time?

Copyright Notice:Content Author:「beary605」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/11385030/batch-timestamp-to-unix-time

Answers
Eitan T 2012-07-08T19:14:19

There's Richie Lawrence's batch library that has all those nifty handy scripts. The one you need is DateToSec (which uses GetDate and GetTime).\n\nHere's a simplified script, that employs a little WMI:\n\n@echo off\nsetlocal\ncall :GetUnixTime UNIX_TIME\necho %UNIX_TIME% seconds have elapsed since 1970-01-01 00:00:00\ngoto :EOF\n\n:GetUnixTime\nsetlocal enableextensions\nfor /f %%x in ('wmic path win32_utctime get /format:list ^| findstr \"=\"') do (\n set %%x)\nset /a z=(14-100%Month%%%100)/12, y=10000%Year%%%10000-z\nset /a ut=y*365+y/4-y/100+y/400+(153*(100%Month%%%100+12*z-3)+2)/5+Day-719469\nset /a ut=ut*86400+100%Hour%%%100*3600+100%Minute%%%100*60+100%Second%%%100\nendlocal & set \"%1=%ut%\" & goto :EOF\n\n\nThe result will be returned into the first parameter passed to GetUnixTime, i.e. %UNIX_TIME%.\nFor example:\n\n1341791426 seconds have elapsed since 1970-01-01 00:00:00\n\n\nHope it helps!",


mguven guven 2015-05-06T06:44:51

create a .bat file called \"getUtime.bat\"\n\n@echo off\necho WScript.Echo(new Date().getTime()); > %temp%\\time.js\ncscript //nologo %temp%\\time.js\ndel %temp%\\time.js\n\n\nand call like this \n\n\"C:\\>getUTime\"\n1430894237616\n",


damian1baran 2014-12-26T16:00:15

What about simple 1-line long C program returning UNIX timestamp? You can retrieve value from %errorlevel% in batch script.\n\n#include <stdio.h>\n#include <time.h>\n\nint main(void)\n{\n return (int) time(NULL);\n}\n\n\nIn my test in command prompt it worked:\n\nC:\\Users\\dabaran\\Desktop\\cs50\\src\\C>.\\time || echo %errorlevel% && set mytstamp=%errorlevel%\n1419609373\n\nC:\\Users\\dabaran\\Desktop\\cs50\\src\\C>echo %mytstamp%\n1419609373\n",


spacefluff432 2021-05-28T14:00:35

There's a really simple way of doing this in a single batch file with no external scripts, files, libraries, etc.\n<!-- :\n for /f "tokens=* usebackq" %%a in (`start /b cscript //nologo "%~f0?.wsf"`) do (set timestamp=%%a)\n echo %timestamp%\n pause\n exit /b\n-->\n\n<job><script language="JavaScript">\n WScript.Echo(new Date().getTime());\n</script></job>\n\nThe way it works is, code for a batch script AND code for a JS file are contained in the same .cmd or .bat file. you can force cscript to run the batch file as a script by commenting out the batch code and the : after the first line means batch will ignore it and run the batch code directly. so there you go!",


ctor 2012-07-08T17:21:59

There is no batch command for returning UNIX time. Your only options would be to write a program which could be run from a batch file that would return the UNIX time, or you could use the Windows PowerShell.",


More about “Batch: Timestamp to UNIX Time” related questions

Batch: Timestamp to UNIX Time

For all I know, Batch does not have a command that gives the UNIX time. The closest one I can find is %time%, which only displays the timestamp. Is there a command, or set of commands in Batch with

Show Detail

converting unix timestamp to local time

I'm attempting to convert time.time() to the local time. Reading python datetime to unix timestamp, I understand there's some limitations given that the timestamp is in UTC. However, in lieu of just

Show Detail

Javascript get time in unix from a unix timestamp

How can I extract time only as a unix number from a unix timestamp? For example: const timeStamp = 1671682809 // Thursday, December 22, 2022 11:20:09 AM GMT+07:00 const unixTimeOnly = extractTime(

Show Detail

Marshalling time.Time to unix timestamp

I have a struct like so type log struct { [...] Timestamp timestamp `json:"timestamp"` } and I want to have the Timestamp as unix timestamp instead of whatever go does by default (2018-09-

Show Detail

Are UNIX_TIMESTAMP() and time() the same

Does MySQL UNIX_TIMESTAMP() show the same UNIX timestamp as PHP's time() function. Even when the servers are in two different locations? I've been looking into time and date for hours now and I've

Show Detail

generate unix timestamp from last time

suppose i have one column in mysql database which is stated last time one equipment is up in h:i:s format (ex: 00:05:11) or 1d21h, means that the equipment is on since 5 min before, what is the best

Show Detail

Javascript - Setting Custom Time in Unix Timestamp

I'm working on a program that takes input from a user for a time. The program will take the information and automatically generate a Unix Timestamp using the current date as the date in the timesta...

Show Detail

Converting TIMESTAMP to unix time in PHP?

Currently I store the time in my database like so: 2010-05-17 19:13:37 However, I need to compare two times, and I feel it would be easier to do if it were a unix timestamp such as 1274119041. (Th...

Show Detail

Convert NTP timestamp to Unix time

I am getting the current time from an NTP server. The reference epoch of NTP is 0:00:00.000 on 1 January 1900 and the library I use for NTP returns the number of seconds that elapsed since that ref...

Show Detail

Convert Unix timestamp to timestamp without time zone

How do I convert a Unix timestamp (or epoch time) to a PostgreSQL timestamp without time zone? For example, 1481294792 should convert to 2016-12-09 14:46:32. I tried SELECT to_timestamp(148129479...

Show Detail