Skip to content

Instantly share code, notes, and snippets.

@larainema
Created July 12, 2014 01:34
Show Gist options
  • Save larainema/5b04eb85f10792da5c13 to your computer and use it in GitHub Desktop.
Save larainema/5b04eb85f10792da5c13 to your computer and use it in GitHub Desktop.
date use
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"` #获得指定日前加上一天的日前
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment