怎么把`date +%Y/%m/%d`改成昨天的?

ryuxy 2007-08-03 11:31:51
假如今天是8/2,用echo `date +%Y/%m/%d`,得到的是2007/08/02,现在我想得到2007/08/01,要怎么改?多谢!
...全文
3537 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
FreeBSD下取得昨天日期的方法
http://blog.chinaunix.net/u/4206/showart_104921.html

方法一:
linux下的date加参数直接就可以得到昨天的日期,FreeBSD也可以的。
date -v -1d +%Y%m%d
将得到昨天的日期,显示年月日
具体可以man date
方法二:
#!/bin/sh
#昨天日期shell版
now=$(date +%Y%m%d%H%M)
echo $now
yesterday=`TZ=GMT+16;export TZ;date +%Y%m%d%H%M`
echo $yesterday
注:此处的16是24-8得来的,8是当前时区与GMT的差值,北京时间是8
这个方法似乎只能得到7天前的日期,再多就不行了,我实验了半天,好像最多最多能到167,也就是说只能得到7天前7小时的时间。大于167得到的时间就跟现在一模一样了。
QUOTE:
这个方法实际上是“歪打正着”,本来是用来取得其它某个时区的当前时间的方法,却被用来获得本地之前的某个时间了。全球的时区一共才24个,也就是说,如果使用这种方法的话,其实“小时”的数值只有这么一点点合法的范围。

【POSIX】:


5860 offset Indicates the value added to the local time to arrive at
5861 Coordinated Universal Time. The offset has the form:
5862 hh[:mm[:ss]]
5863 The minutes (mm) and seconds (ss) are optional. The hour (hh)
5864 shall be required and may be a single digit. The offset following
5865 std shall be required. If no offset follows dst, the alternative time
5866 is assumed to be one hour ahead of standard time. One or more
5867 digits may be used; the value is always interpreted as a decimal
5868 number. The hour shall be between zero and 24, and the minutes
5869 (and seconds)—if present—between zero and 59. The result of
5870 using values outside of this range is unspecified. If preceded by
5871 a '-', the timezone shall be east of the Prime Meridian;
5872 otherwise, it shall be west (which may be indicated by an
5873 optional preceding '+').

按照上面的说法,使用超范围数值的结果是未定义的。在实验中之所以还能撑到167个小时,应该是和具体实现有关了,但这个数值早就已经不合法了。

方法三:
用gawk的strftime函数,这个方法可以得到很多天前的,不像方法一的那么多限制,但是首先需要安装gawk
cd /usr/ports/japanese/gawk
make install clean
rehash
gawk 'BEGIN{print strftime("%Y%m%d%H%M",systime());print strftime("%Y%m%d%H%M",systime()-1*24*3600)}'
会返回当前的时间和昨天日期的时间
当然你可以把systime()-1*24*3600改成你希望的天数,只要改那个1就成了。
方法四:
#!/bin/sh
# 取得昨天的日期
YEAR=`date +%Y`
DAY=`date +%d`
MONTH=`date +%m`
DAY=`expr $DAY - 1`
if [ $DAY -eq 0 ]; then
MONTH=`expr $MONTH - 1`
if [ $MONTH -eq 0 ]; then
MONTH=12
YEAR=`expr $YEAR - 1`
fi
LASTLINE=`cal $MONTH $YEAR | grep "[0-9]" | tail -1`
DAY=`echo $LASTLINE | sed "s/.* \([0-9][0-9]*\)/\1/"`
fi
if [ $DAY -lt 10 ]; then
DAY=0$DAY
fi
YESTERDAY="$YEAR$MONTH$DAY"
echo $YESTERDAY
方法五:
来源于http://bbs.chinaunix.net/viewthread.php?tid=743902&extra=page%3D1
#!/bin/sh
#昨天日期shell版
yy=`date +%Y`;mm=`date +%m`;dd=`date +%d`
d0=`echo "$dd"|awk '{printf"%02d\n",$1-1}'`
m0=`echo "$mm"|awk '{printf"%02d\n",$1-1}'`
y0=`echo "$yy"|awk '{printf"%02d\n",$1-1}'`
case $d0 in
00) if [ $m0 = 00 ]; then
ym=`cal 12 $y0|xargs|awk '{print $NF}'`
y0=$y0;m0=12;d0=$ym
else
ym=`cal $m0 $yy|xargs|awk '{print $NF}'`
d0=$ym;m0=$m0;y0=$yy
fi
;;
*) d0=$d0;m0=$mm;y0=$yy;;
esac
echo "$y0$m0$d0"

iisbsd 2007-08-04
  • 打赏
  • 举报
回复
linux的:
date -d "-1 day"
roadingyh 2007-08-03
  • 打赏
  • 举报
回复
改一下时间:
date -s 2007-8-1 就可以了...
mymtom 2007-08-03
  • 打赏
  • 举报
回复
FreeBSD下可以用 -v 选项,Solaris 和 Linux似乎都不支持
mymtom@:src/shell$ date ; date -v-1d
Fri Aug 3 17:17:30 CST 2007
Thu Aug 2 17:17:30 CST 2007
====
-v Adjust (i.e., take the current date and display the result of the
adjustment; not actually set the date) the second, minute, hour,
month day, week day, month or year according to val. If val is
preceded with a plus or minus sign, the date is adjusted forwards
or backwards according to the remaining string, otherwise the
relevant part of the date is set. The date can be adjusted as
many times as required using these flags. Flags are processed in
the order given.
Date 对象属性constructor 返回对创建此对象的 Date 函数的引用。prototype 使您有能力向对象添加属性和方法。Date 对象方法getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。getFullYear() 从 Date 对象以四位数字返回年份。getHours() 返回 Date 对象的小时 (0 ~ 23)。getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。getMonth() 从 Date 对象返回月份 (0 ~ 11)。getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。getTime() 返回 1970 年 1 月 1 日至今的毫秒数。getTimezoneOffset() 返回本地时间与格林威治标准时间 (GMT) 的分钟差。getUTCDate() 根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。getUTCDay() 根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。getUTCFullYear() 根据世界时从 Date 对象返回四位数的年份。getUTCHours() 根据世界时返回 Date 对象的小时 (0 ~ 23)。getUTCMilliseconds() 根据世界时返回 Date 对象的毫秒(0 ~ 999)。getUTCMinutes() 根据世界时返回 Date 对象的分钟 (0 ~ 59)。getUTCMonth() 根据世界时从 Date 对象返回月份 (0 ~ 11)。getUTCSeconds() 根据世界时返回 Date 对象的秒钟 (0 ~ 59)。getYear() 已废弃。 请使用 getFullYear() 方法代替。parse() 返回1970年1月1日午夜到指定日期(字符串)的毫秒数。setDate() 设置 Date 对象中月的某一天 (1 ~ 31)。setFullYear() 设置 Date 对象中的年份(四位数字)。setHours() 设置 Date 对象中的小时 (0 ~ 23)。setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)。setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)。setMonth() 设置 Date 对象中月份 (0 ~ 11)。setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)。setTime() setTime() 方法以毫秒设置 Date 对象。setUTCDate() 根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。setUTCFullYear() 根据世界时设置 Date 对象中的年份(四位数字)。setUTCHours() 根据世界时设置 Date 对象中的小时 (0 ~ 23)。setUTCMilliseconds() 根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。setUTCMinutes() 根据世界时设置 Date 对象中的分钟 (0 ~ 59)。setUTCMonth() 根据世界时设置 Date 对象中的月份 (0 ~ 11)。setUTCSeconds() setUTCSeconds() 方法用于根据世界时 (UTC) 设置指定时间的秒字段。setYear() 已废弃。请使用 setFullYear() 方法代替。toDateString() 把 Date 对象的日期部分转换为字符串。toGMTString() 已废弃。请使用 toUTCString() 方法代替。toISOString() 使用 ISO 标准返回字符串的日期格式。toJSON() 以 JSON 数据格式返回日期字符串。toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串。toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串。toLocaleString() 据本地时间格式,把 Date 对象转换为字符串。toString() 把 Date 对象转换为字符串。toTimeString() 把 Date 对象的时间部分转换为字符串。toUTCString() 根据世界时,把 Date 对象转换为字符串。UTC() 根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。valueOf() 返回 Date 对象的原始值。

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧