Convert an ISO date to seconds since epoch in linux bash

我只是一个虾纸丫 提交于 2019-11-30 08:02:40

问题


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.

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

thank you...


回答1:


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

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



回答2:


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

install coreutils and you will get gdate along

on 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

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

echo $variable


来源:https://stackoverflow.com/questions/21778251/convert-an-iso-date-to-seconds-since-epoch-in-linux-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!