Метка времени (unixtime) и формат даты на batch
Готовый скрипт, который выводит форматированную дату и поддерживает метку времени unixtime:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
@echo off setlocal ENABLEDELAYEDEXPANSION :::: :: :: Format date :: Author - Ts.Saltan https://tssaltan.top/1985.batch-unixtime-timestamp/ :: :::: Format :::: :: Year :: %yy : A two digit representation of a year : 18 :: %yyyy : A full numeric representation of a year : 2018 :: :: Month :: %m : Number of the month without leading zeros : 7, 12 :: %mm : Number of the month with leading zeros : 07, 12 :: %mmm : A short textual representation of a month : Jul, Dec :: %mmmm : A full textual representation of a month : July, December :: :: Day of month :: %d : Day of the month without leading zeros : 1, 20 :: %dd : Day of the month with leading zeros : 01, 20 :: :: Hour :: %h : 24-hour format of an hour without leading zeros : 1, 20 :: %hh : 24-hour format of an hour with leading zeros : 01, 20 :: :: Minute :: %i : Minutes without leading zeros : 1, 10 :: %ii : Minutes with leading zeros : 01, 10 :: :: Seconds :: %s : Seconds without leading zeros : 1, 10 :: %ss : Seconds with leading zeros : 01, 10 :: :: Microseconds :: %ms : 25 :: :: Unixtime :: %u : 1546300800 :: :::: :: :: For example: :: %%yyyy-%%mm-%%dd %%hh:%%ii:%%ss.%%ms - 2019-07-10 13:52:43.21 :: %%yy-%%mmm-%%d %%h:%%i:%%s - 19-Jul-10 13:3:8 :: :::: :: :: call from another script :: :: for /F "Tokens=*" %%i in ('timestamp %%yyyy-%%mm-%%dd-%%hh-%%ii-%%ss') Do Set timestamp=%%i :: echo %timestamp% :: :::: if "%*" == "" ( set Format=%%u ) else ( set "Format=%*" ) :: year set yyyy=%date:~6,4% set yy=%date:~8,4% set Format=!Format:%%yyyy=%yyyy%! set Format=!Format:%%yy=%yy%! :: day set dd=%date:~0,2% set d=1%date:~0,2% set /a d=%d%-100 set Format=!Format:%%dd=%dd%! set Format=!Format:%%d=%d%! :: hour :: replace space symbol, if hour < 10 set hour=%time:~0,2% set hh=%hour: =0% set h=1%hh% set /a h=%h%-100 set Format=!Format:%%hh=%hh%! set Format=!Format:%%h=%h%! :: minutes set ii=%time:~3,2% set i=1%ii% set /a i=%i%-100 set Format=!Format:%%ii=%ii%! set Format=!Format:%%i=%i%! :: seconds set ss=%time:~6,2% set s=1%ss% set /a s=%s%-100 set Format=!Format:%%ss=%ss%! set Format=!Format:%%s=%s%! :: microseconds set ms=%time:~9,2% set Format=!Format:%%ms=%ms%! :: month set a=0 for %%m in ( January February March April May June July August September October November December ) do ( set /a a+=1 set month[!a!]=%%m ) set mm=%date:~3,2% set m=1%mm% set /a m=%m%-100 set mmmm=!month[%m%]! set mmm=!mmmm:~0,3! set Format=!Format:%%mmmm=%mmmm%! set Format=!Format:%%mmm=%mmm%! set Format=!Format:%%mm=%mm%! set Format=!Format:%%m=%m%! :: unixtime set /a ut = 0 set /a yd = %yyyy%-1970 set /a ut = %yd%*36525/100*24*60*60 set a=0 for %%b in ( 0 31 59 90 120 151 181 212 243 273 304 334 ) do ( set /a a+=1 set monthdays[!a!]=%%b ) set /a ut = %ut%+(!monthdays[%m%]!*24*60*60) set /a ut = %ut%+%d%*24*60*60 set /a ut = %ut%+%h%*60*60 set /a ut = %ut%+%i%*60 set /a ut = %ut%+%s% set Format=!Format:%%u=%ut%! echo %Format% |
Пример вызова из другого скрипта:
1 2 3 |
@echo off for /F "Tokens=*" %%i in ('timestamp %%yyyy-%%mm-%%dd-%%hh-%%ii-%%ss') Do Set timestamp=%%i echo %timestamp% |
Поддерживаемые форматы времени
Год
- %yy – Год, 2 знака (19)
- %yyyy – Год, 4 знака (2019)
Месяц
- %m – Месяц без ведущего нуля (7, 12)
- %mm – Месяц c ведущим нулём (07, 12)
- %mmm – Месяц, краткая запись (Jul, Dec)
- %mmmm – Месяц, полное название (July, December)
День
- %d – День месяца без ведущего нуля (7, 12)
- %dd – День месяца c ведущим нулём (7, 12)
Час
- %h – 24-часовой формат часа без ведущего нуля (7, 12)
- %hh – 24-часовой формат часа c ведущим нулём (7, 12)
Минута
- %i – Минута без ведущего нуля (7, 12)
- %ii – Минута c ведущим нулём (7, 12)
Секунда
- %s – Секунда без ведущего нуля (7, 12)
- %ss – Секунда c ведущим нулём (7, 12)
Миллисекунды
- %ms (10, 55)
Unixtime
- %u (1546300800)