shell date 时间与时间戳互转

date +%s 可以得到UNIX的时间戳;用shell将时间字符串与时间戳互转:

date -d "2010-10-18 00:00:00" +%s         输出形如:1287331200

而时间戳转换为字符串可以这样做:date -d @1287331200 "+%Y-%m-%d" 输出形如:2010-10-18date -d @1365931601 "+%Y-%m-%d %H:%M:%S"

2013-04-14 17:26:41如果需要得到指定日期的前后几天,可以:

1、seconds=`date -d "2010-10-18 00:00:00" +%s`       #得到时间戳2、seconds_new=`expr $seconds + 86400`                   #加上一天的秒数864003、date_new=`date -d @$seconds_new "+%Y-%m-%d"`   #获得指定日前加上一天的日前
shell date 时间与时间戳互转

相关文章:

你感兴趣的文章:

标签云: