yyyy variable ddmmyyyy batch bat and windows bash command-line batch-file unix-timestamp

windows - variable - get date and time cmd



¿Cuál es el equivalente de Windows del comando "date+% s"? (3)

Aquí hay una solución de lote nativa que debería funcionar en cualquier localidad. Utiliza WMIC para obtener la hora local actual de una manera independiente. Todo lo demás es una cuestión "simple" de análisis de cadenas y matemática básica.

:UnixTime [ReturnVar] [TimeStamp] :: :: Computes the Unix time from the current local time as reported by the :: operating system. The Unix time is the number of seconds that have elapsed :: since midnight Coordinated Universal Time (UTC), January 1, 1970, not :: counting leap seconds. :: :: The result is returned in variable ReturnVar, :: or the result is echoed if ReturnVar is not specified :: :: If the TimeStamp is provided in the 2nd parameter, then the Unix time for :: the TimeStamp is computed, rather then for the current time. :: :: The TimeStamp must have the same format as used by WMIC: :: :: YYYYMMDDhhmmss.ffffffSzzz :: :: where: :: :: YYYY = gregorian year :: MM = month :: DD = day :: hh = hour in 24 hour format :: mm = minute :: ss = seconds :: ffffff = fractional seconds (microseconds) :: S = timezone sign: + or - :: zzz = timezone: minutes difference from GMT :: :: Each component must be zero prefixed as needed to maintain the proper width. :: :: The ReturnVar parameter must be provided in order to use the TimeStamp. :: A ReturnVar of "" will function the same as no ReturnVar. This enables the :: specification of a TimeStamp without an actual ReturnVar. :: @echo off setlocal set "ts=%~2" if not defined ts for /f "skip=1 delims=" %%A in (''wmic os get localdatetime'') do if not defined ts set "ts=%%A" set /a "yy=10000%ts:~0,4% %% 10000, mm=100%ts:~4,2% %% 100, dd=100%ts:~6,2% %% 100" set /a "dd=dd-2472663+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4" set /a ss=(((1%ts:~8,2%*60)+1%ts:~10,2%)*60)+1%ts:~12,2%-366100-%ts:~21,1%((1%ts:~22,3%*60)-60000) set /a ss+=dd*86400 endlocal & if "%~1" neq "" (set %~1=%ss%) else echo %ss% exit /b


Tenga en cuenta que esta solución tiene una vida útil limitada. Dejará de funcionar en 2038-01-19 cuando el tiempo de Unix excede el valor máximo de un entero de 32 bits con signo.

EDITAR - El código se ha editado para admitir la conversión de una cadena de marca de tiempo en la línea de comando en lugar de la hora local actual. El rango preciso de veces admitido es 1901-12-13 20: 45: 52.000000 a 2038-01-19 03: 14: 07.999999 GMT. Los tiempos anteriores a 1970-01-01 00: 00: 00.000000 arrojarán valores negativos.

Estoy escribiendo un script por lotes y necesito el tiempo de Unix. Es fácil en Linux, pero no puedo entender cómo hacerlo en Windows.


Puede usar vbscript en Windows, el intérprete está disponible en su sistema.

''--------------------epoch.vbs----------------------- option explicit dim s,o,z for each o in GetObject("winmgmts:").InstancesOf ("Win32_OperatingSystem") z=o.CurrentTimeZone next s=DateDiff("s", "01/01/1970 00:00:00", Now())-(60*z) wscript.echo(s) wscript.quit


Si por "tiempo de Unix" te refieres al segundo de época , entonces Windows no incluye herramientas para producir eso. En cambio, puede instalar herramientas de terceros. Por ejemplo:

  • Instala Cygwin .
  • Encuentre la date binaria (en C:/Cygwin/ o donde la haya instalado)
  • Úselo como lo haría en Linux.

Alternativamente, según el impresionante comentario sobre esta respuesta, puede instalar GNU Coreutils, que también incluye un comando de date . Incluye una serie de otras herramientas que puede que no necesite, pero también lo hace Cygwin.