击转换日期为时间戳 [英] Bash convert date to timestamp

查看:128
本文介绍了击转换日期为时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在格式'YYYYMMDDHHMMSS'日期,我需要将其转换为Unix时间戳。

I have a date in format 'YYYYMMDDHHMMSS' and I need to convert it to Unix timestamp.

我试图日期-d20140826225834,但我得到无效的日期错误。我asume,我将不得不转换我有什么( 20140826225834 ),以接受日期,然后将其转换为时间戳?
编辑:我的sed这个日期从 2014-08-21_23.03.07 - 也许它会更容易在此日期类型转换

I tried to date -d '20140826225834' but I get 'invalid date' error. I asume that I would have to convert what I have ( 20140826225834 ) to accepted date and then convert it to timestamp? I have sed this date from 2014-08-21_23.03.07 - maybe it would be easier to convert this date type

推荐答案

您或许应该改变你得到的日期格式,让日期可以处理它。我将其更改为 YYYY / MM / DD HH:MM:SS 与格式 SED

You should probably change the format of the date you get, so that date can handle it. I change it to a YYYY/MM/DD HH:MM:SS format with sed.

$ date -d"$(sed -r 's#(.{4})(.{2})(.{2})(.{2})(.{2})#\1/\2/\3 \4:\5:#' <<< "20140826225834")" "+%s"
1409086714

这是件:

$ sed -r 's#(.{4})(.{2})(.{2})(.{2})(.{2})#\1/\2/\3 \4:\5:#' <<< "20140826225834"
2014/08/26 22:58:34

$ date -d"2014/08/26 22:58:34"
Tue Aug 26 22:58:34 CEST 2014

$ date -d"2014/08/26 22:58:34" "+%s"
1409086714

这篇关于击转换日期为时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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