Batch: Timestamp to UNIX Time

前端 未结 5 738
感情败类
感情败类 2020-12-16 16:44

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 ther

相关标签:
5条回答
  • 2020-12-16 17:19

    By far best solution is to download a freestanding date.exe unix-port.

    Recommend that you rename it to unixdate.exe, to avoid conflict with MS Date command.

    Get it from here

    1. download & unzip. As far as I know, all the utilities are 'portable', that is, they don't require any installation program for them to work. I've only used a few (rm.exe & date.exe). Also, you can ReName the exe's in case they might match other system utils you already have
    2. Find the actual executable ports in the subfolder (usr\local\wbin)
    3. To get HELP info type the command followed with --help (must spell help in lowercase)

    Example:

    ((PROMPT)):unixdate +%Y%M%d
    20141704
    
    ((PROMPT)):unixdate +%Y%b%d
    2014Sep04
    
    0 讨论(0)
  • 2020-12-16 17:23

    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).

    Here's a simplified script, that employs a little WMI:

    @echo off
    setlocal
    call :GetUnixTime UNIX_TIME
    echo %UNIX_TIME% seconds have elapsed since 1970-01-01 00:00:00
    goto :EOF
    
    :GetUnixTime
    setlocal enableextensions
    for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do (
        set %%x)
    set /a z=(14-100%Month%%%100)/12, y=10000%Year%%%10000-z
    set /a ut=y*365+y/4-y/100+y/400+(153*(100%Month%%%100+12*z-3)+2)/5+Day-719469
    set /a ut=ut*86400+100%Hour%%%100*3600+100%Minute%%%100*60+100%Second%%%100
    endlocal & set "%1=%ut%" & goto :EOF
    

    The result will be returned into the first parameter passed to GetUnixTime, i.e. %UNIX_TIME%.
    For example:

    1341791426 seconds have elapsed since 1970-01-01 00:00:00
    

    Hope it helps!

    0 讨论(0)
  • 2020-12-16 17:27

    create a .bat file called "getUtime.bat"

    @echo off
    echo WScript.Echo(new Date().getTime()); > %temp%\time.js
    cscript //nologo %temp%\time.js
    del %temp%\time.js
    

    and call like this

    "C:\>getUTime"
    1430894237616
    
    0 讨论(0)
  • 2020-12-16 17:30

    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.

    0 讨论(0)
  • 2020-12-16 17:36

    What about simple 1-line long C program returning UNIX timestamp? You can retrieve value from %errorlevel% in batch script.

    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
        return (int) time(NULL);
    }
    

    In my test in command prompt it worked:

    C:\Users\dabaran\Desktop\cs50\src\C>.\time || echo %errorlevel% && set mytstamp=%errorlevel%
    1419609373
    
    C:\Users\dabaran\Desktop\cs50\src\C>echo %mytstamp%
    1419609373
    
    0 讨论(0)
提交回复
热议问题