用shell将时间字符串与时间戳互转

来源:互联网 发布:微信运动可添加数据 编辑:程序博客网 时间:2024/06/05 11:42

源博客http://blog.csdn.net/runming918/article/details/7384828


date +%s   可以得到UNIX的时间戳;
用shell将时间字符串与时间戳互转:
      date -d "2010-10-18 00:00:00" +%s         输出形如:1287331200
而时间戳转换为字符串可以这样做:
      date -d @1287331200  "+%Y-%m-%d"    输出形如:2010-10-18
如果需要得到指定日期的前后几天,可以:
      1、seconds=`date -d "2010-10-18 00:00:00" +%s`       #得到时间戳
      2、seconds_new=`expr $seconds + 86400`                   #加上一天的秒数86400
      3、date_new=`date -d @$seconds_new "+%Y-%m-%d"`   #获得指定日前加上一天的日前

 

date 的具体用法可以查看另外一篇博文 

《shell date 命令详解》http://blog.csdn.net/runming918/article/details/7223520

0 0