How to convert a date in yyyy-mm-dd hh:mm:ss to unix epoch time

為{幸葍}努か 提交于 2019-12-12 03:35:51

问题


I have date format like 2016-02-26 14:12:36., I have to convert it into unix epoch time. I have the option to convert current time to epoch time using command : date +%s, however how to convert a specific date to epoch unix time


回答1:


On the command line you convert the date provided to the UNIX epoch time like this:

date -j -f "%Y-%m-%d %H:%M:%S" "2016-02-26 14:12:36" "+%s"

This is on os x. The manual for date have some examples and strftime have all the formatting needed.




回答2:


You can get seconds since the epoch from date like this ,

$ SECFROMEPOCH=`date --date="2016-02-26 14:12:36" +%s`

And then you can check the value that date gave back to you like this,

$ echo $SECFROMEPOCH | awk '{print strftime("%c",$1)}'


来源:https://stackoverflow.com/questions/35654377/how-to-convert-a-date-in-yyyy-mm-dd-hhmmss-to-unix-epoch-time

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