在linux bash中将ISO日期转换为纪元以来的秒数 [英] Convert an ISO date to seconds since epoch in linux bash

查看:19
本文介绍了在linux bash中将ISO日期转换为纪元以来的秒数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

All the dates refer to UTC locale.

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

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

谢谢...

推荐答案

使用 GNU 日期(来自 GNU coreutils 包),用 -d 指定要解析的日期,用 %s

With GNU date (from the GNU coreutils package), specify the date to parse with -d and seconds since epoch with %s

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


请注意,这会将要解析的日期解释为您的本地时区.如果您希望 date 使用特定时区,则必须通过变量 TZ(更改 date 的默认时区)或在日期中指定细绳.对于 UTC:


Note that this will interpret the date to be parsed as being in your local time zone. If you want date to use a specific time zone, you must specify that, either via the variable TZ (which changes the default time zone for date), or in the date string. For UTC:

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

或在字符串中,根据 ISO 8601:

or in the string, according to ISO 8601:

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

有关如何在日期字符串中指定其他时区,请参阅 维基百科上的 ISO 8601.

See ISO 8601 on Wikipedia for how to specify other time zones in the date string.

这篇关于在linux bash中将ISO日期转换为纪元以来的秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆