活到老学到老  

记录遇到问题的点点滴滴。

Linux中Shell日期转为时间戳的方法

7年前发布  · 1268 次阅读
  date  shell 

shell中获取时间戳的方式为:date -d “$currentTime” +%s

$ date -d @1337743485671 "+%c" 
Sun 28 May 44361 12:41:11 PM CST



如果要将一个日期转为时间戳,方式如下:

1、得到当前时间

currentTime=`date “+%Y-%m-%d %H:%M:%S”` 



2、将日期转为时间戳

currentTimeStamp=`date -d “$currentTime” +%s` 
echo $currentTimeStamp 



3.字符串转换为时间戳可以这样做:

date -d "2010-10-18 00:00:00" +%s 



输出形如:

1287331200

其中,-d参数表示显示指定的字符串所表示的时间,+%s表示输出时间戳。

4.而时间戳转换为字符串可以这样做:

date -d @1287331200 



输出形如:

Mon Oct 18 00:00:00 CST 2010