免费注册 查看新帖 |

Chinaunix

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

再显示前一天时间时,修改时间显示2005-3-7为2005-03-07 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-03-08 16:27 |只看该作者 |倒序浏览
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
      01|03|05|07|08|10|12) day=31;;
      04|06|09|11) day=30;;
      02)
        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.
data="$year-$month-$day"
day="$year$month$day"
echo $data

论坛徽章:
0
2 [报告]
发表于 2005-03-08 17:07 |只看该作者

再显示前一天时间时,修改时间显示2005-3-7为2005-03-07

虽然很长,虽然很罗嗦,不过在某些时候还是有点作用的。

论坛徽章:
0
3 [报告]
发表于 2005-06-07 09:36 |只看该作者

再显示前一天时间时,修改时间显示2005-3-7为2005-03-07

这个shell有问题,如果是10号的前一天,只会输出9,而不是09
日和月都要加这个判断
if [ $day -lt 10 ]
then
     day=0$day
fi
if [ $month -lt 10 ]
    then
        month=0$month
    fi

论坛徽章:
0
4 [报告]
发表于 2005-06-07 09:40 |只看该作者

再显示前一天时间时,修改时间显示2005-3-7为2005-03-07

如果有-d,直接写成这样
date -d "-yesterday" +%Y-%m-%d

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
5 [报告]
发表于 2005-06-07 14:46 |只看该作者

再显示前一天时间时,修改时间显示2005-3-7为2005-03-07

又是一个月经贴
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP