bash 在linux bash中将ISO日期转换为自纪元以来的秒数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21778251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:33:53  来源:igfitidea点击:

Convert an ISO date to seconds since epoch in linux bash

linuxbashdatetimezoneepoch

提问by Gianluca Ghettini

I have a date in the ISO format YYYY-MM-DDTHH:SS (e.g. 2014-02-14T12:30). I'd like to convert it in seconds since epoch using only the date command in linux bash.

我有一个 ISO 格式的日期 YYYY-MM-DDTHH:SS(例如 2014-02-14T12:30)。我想在 linux bash 中仅使用 date 命令在几秒钟内转换它。

All the dates refer to UTC locale.

所有日期均指 UTC 语言环境。

I know that this question is easily eligible for duplicate... there are billions of questions about converting dates from one format to another but I can't find my particular scenario

我知道这个问题很容易重复......关于将日期从一种格式转换为另一种格式的问题有数十亿,但我找不到我的特定场景

thank you...

谢谢你...

回答by kguest

With GNU date, specify the date to parse with -dand seconds since epoch with %s

使用 GNU 日期,指定要解析的日期-d和自纪元以来的秒数%s

$ date -d"2014-02-14T12:30" +%s
1392381000

回答by Mohammed Rafeeq

It is easier if you install gdateto deal with date strings that have timezones with nano second precision

如果您安装gdate处理具有纳秒精度的时区的日期字符串会更容易

install coreutilsand you will get gdatealong

install coreutils你会相处得gdate很好

on mac brew install coreutils

在 Mac 上 brew install coreutils

gdate --date="2010-10-02T09:35:58.203Z" +%s%N

This is particularly useful when inserting the time series value into influxdb

这在将时间序列值插入 influxdb 时特别有用

in a shell script variable = $(gdate --date="2010-10-02T09:35:58.203Z" +%s%N)

在 shell 脚本中 variable = $(gdate --date="2010-10-02T09:35:58.203Z" +%s%N)

echo $variable