bash - 如何在os x中将日期的字符串格式转换为毫秒?
问题描述
我想2020-01-17T07:41:53.000Z
在 os X 中更改为毫秒格式。
如何实现?
解决方案
在 Mac OS 下转换日期
date '2020-01-17T07:41:53.000Z' date: illegal time format usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
然后,从man date
:
-j Do not try to set the date. This allows you to use the -f flag in addition to the + option to convert one date format to another.
重组$str
:
str="2020-01-17T07:41:53.000Z"
IFS='-T:.Z' read _ccyy _mm _dd _HH _MM _ss _mil <<<"$str"
date -j +%c $_mm$_dd$_HH$_MM$_ccyy.$_ss
Fri Jan 17 07:41:53 2020
看起来工作正常!
epoch=$(env TZ=UTC date -j +%s $_mm$_dd$_HH$_MM$_ccyy.$_ss)
printf -v milliseconds "%s%03.0f" $epoch $milli
echo $milliseconds
1579246913000
...而且只是:关心TZ
!
推荐阅读
- azure - 授予应用程序权限以读取我的活动目录的图形 api
- c# - TimeTrigger 函数中的错误
- python - Django模板如果选择字段
- python - PyTorch 自定义损失函数
- class - 如何避免在 Delphi 中对代码使用 inc 文件
- jenkins - 仅压缩目录内容(文件和子目录)
- azure - 从 Azure DevOps 部署期间是否可以管理 Verizon Premium CDN 的规则引擎?
- php - 一般错误:1366 字符串值不正确:'\xF0\x9F\x8D\xB8 !...'
- eclipse - 尽管使用了浅色主题,但 Eclipse 内容辅助浮出控件在黑色上显示不可读的深色
- ios - iOS 的 Ionic 构建失败