linux中Shell日期转为时间戳的方法,linuxshell
linux中Shell日期转为时间戳的方法,linuxshell
这篇文章主要介绍了linux中Shell日期转为时间戳的方法,需要的朋友可以参考下
$ 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
24小时,就是相差一天,所以变化的只有天数。
我这里提供一个笨方法:
用date获取当前日期和时间,格式化成你的文件命名格式,用下面的字符串截取方法获得天数:
${varible:n1:n2}:截取变量varible从n1到n2之间的字符串
天数减1后,重新组织成新的字符串。
用循环比较 ls 出来的文件名与这个字符串。
代码试过后再放上来。
1、date.sh代码
#!/bin/sh
beg_date=`date -d "$1" +%s`
end_date=`date -d "$2" +%s`
if [[ -z $1 ]]||[[ -z $2 ]];then
echo "Usage: $0 YYYYMMDD YYYYMMDD"
exit 0;
fi
if [[ ${beg_date} > ${end_date} ]];then
echo "The end_date < beg_date ;Please input the right date,example: $0 20140101 20140301"
exit 0;
fi
for (( i=${beg_date};i<=${end_date};i=i+86400))
do
date -d @${i} +%Y%m%d
done
2、实例./date.sh 20140101 20140201
评论暂时关闭