免费注册 查看新帖 |

Chinaunix

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

date命令,取得日期的前一天 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-24 17:13 |只看该作者 |倒序浏览
linux系统下可以显示:
#date --date ='a day ago'

但是,hp unix 11.系统怎样取得日期的前一天?

论坛徽章:
0
2 [报告]
发表于 2009-03-24 17:17 |只看该作者
[root@Greendays /]# date
Tue Mar 24 17:19:27 CST 2009
[root@Greendays /]# date |awk '{print $1,$2,$3-1,$4,$5,$6}'
Tue Mar 23 17:19:29 CST 2009

论坛徽章:
0
3 [报告]
发表于 2009-03-24 17:18 |只看该作者

回复 #1 lll0024 的帖子

try  没玩过HP的
date -d 'yesterday'
或者按你说的
date --date='yesterday'

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
4 [报告]
发表于 2009-03-24 17:20 |只看该作者

论坛徽章:
0
5 [报告]
发表于 2009-03-24 17:20 |只看该作者

回复 #2 greendays 的帖子

不是这么简单的。还要考虑月头1号的情况。

论坛徽章:
0
6 [报告]
发表于 2009-03-24 17:36 |只看该作者
原帖由 greendays 于 2009-3-24 17:17 发表
[root@Greendays /]# date
Tue Mar 24 17:19:27 CST 2009
[root@Greendays /]# date |awk '{print $1,$2,$3-1,$4,$5,$6}'
Tue Mar 23 17:19:29 CST 2009



二楼真强.这也能想到.受教,受教了...

论坛徽章:
0
7 [报告]
发表于 2009-03-24 17:40 |只看该作者

回复 #1 lll0024 的帖子

如果是某月的一号??那....

论坛徽章:
0
8 [报告]
发表于 2009-03-24 17:42 |只看该作者

回复 #5 我是DBA 的帖子

还要考虑月头1号的情况,
润年也要考虑。

论坛徽章:
0
9 [报告]
发表于 2009-03-24 17:45 |只看该作者

回复 #8 lll0024 的帖子

麻烦看一下版版发的连接。。。

论坛徽章:
0
10 [报告]
发表于 2009-03-24 17:46 |只看该作者
#!/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