歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> linux中Shell日期轉為時間戳的方法

linux中Shell日期轉為時間戳的方法

日期:2017/3/1 17:58:17   编辑: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
Copyright © Linux教程網 All Rights Reserved