免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 23707 | 回复: 36
打印 上一主题 下一主题

如何用date获得前一天的日期,忘了  关闭 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2002-04-02 09:55 |只看该作者 |倒序浏览
好象有date 有-24 有+%y%m%d等,谢了。
还有有办法获得3天前的日期吗?

论坛徽章:
0
2 [报告]
发表于 2002-04-02 10:18 |只看该作者

如何用date获得前一天的日期,忘了

唉,在b shell没有这个用法了,要想用纯shell做几天前后的运算,只有自己想办法了,+%y%m%d用于控制输出格式,作用有限,要想做的话,参见这个帖子大家的发言:
http://www.chinaunix.net/cgi-bin/bbs/topic.cgi?forum=11&topic=91&show=30

论坛徽章:
0
3 [报告]
发表于 2002-04-02 10:28 |只看该作者

如何用date获得前一天的日期,忘了

我的确在哪儿看到用一条命令就可以取昨天或明天的日期,
参数中有-24或+24。没那么复杂

论坛徽章:
0
4 [报告]
发表于 2002-04-02 10:52 |只看该作者

如何用date获得前一天的日期,忘了

好象用到了参数TZ=-24;export TZ什么的

论坛徽章:
0
5 [报告]
发表于 2002-04-02 11:03 |只看该作者

如何用date获得前一天的日期,忘了

改变时区,应该可以实现,确实是个好方法。

论坛徽章:
0
6 [报告]
发表于 2002-04-02 12:28 |只看该作者

如何用date获得前一天的日期,忘了

可现在我却实现不了?

论坛徽章:
0
7 [报告]
发表于 2002-04-02 16:09 |只看该作者

如何用date获得前一天的日期,忘了

不会吧,你说的俺都明白了,你自己还不懂?

date命令的显示是与环境变量TZ有关的
看以下的命令操作,你应该便明白了,

$#看当前时区
$echo $TZ
CST-8
$#显示当前时间
$date
Mon Apr  2 15:48:36 CST 2002
$#改变当前时区,
TZ=CST+16;export TZ
$#显示当前时间(中间未改变系统时间,但date命令的显示已为昨天)
Mon Apr  1 15:48:33 CST 2002

论坛徽章:
0
8 [报告]
发表于 2002-04-03 08:57 |只看该作者

如何用date获得前一天的日期,忘了

是啊,我已经明白了,忘了告诉斑竹一声
wolfer 该用户已被删除
9 [报告]
发表于 2002-04-03 09:52 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
10 [报告]
发表于 2002-06-20 14:24 |只看该作者

如何用date获得前一天的日期,忘了

#!/bin/sh

# ydate: A Bourne shell script that
# prints yestarday's date
# Output Form: Month Day Year
# From Focus on Unix: http://unix.about.com

# Set the current month day and year.
month=`date +%m`
day=`date +%d`
year=`date +%Y`

# Add 0 to month. This is a
# trick to make month an unpadded integer.
month=`expr $month + 0`

# Subtract one from the current day.
day=`expr $day - 1`

# If the day is 0 then determine the last
# day of the previous month.
if [ $day -eq 0 ]; then
  
  # Find the preivous month.
  month=`expr $month - 1`  

  # If the month is 0 then it is Dec 31 of
  # the previous year.
  if [ $month -eq 0 ]; then
    month=12
    day=31
    year=`expr $year - 1`  

  # If the month is not zero we need to find
  # the last day of the month.
  else
    case $month in
      1|3|5|7|8|10|12) day=31;;
      4|6|9|11) day=30;;
      2)
        if [ `expr $year % 4` -eq 0 ]; then
          if [ `expr $year % 400` -eq 0 ]; then
            day=29
          elif [ `expr $year % 100` -eq 0 ]; then
            day=28
          else
            day=29
          fi
        else
          day=28
        fi
      ;;
    esac
  fi
fi

# Print the month day and year.
echo $month $day $year
exit 0
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP