LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1442|回复: 13

請問如何顯示指定日期之前的1天?

[复制链接]
发表于 2006-3-27 10:54:04 | 显示全部楼层 |阅读模式
比如指定日期格式20050101

現在想得到這個日期之前的幾天

比如一天前,那么如何做呢?

我隻知道如何得到當前日期的


date -v-1d "%Y%m%d"

謝謝!
 楼主| 发表于 2006-3-27 11:40:35 | 显示全部楼层
總算解決叻

date -j -v-1d -f "%Y%m%d" 20040301
回复 支持 反对

使用道具 举报

发表于 2006-3-27 13:25:25 | 显示全部楼层
直接date -d 就是之前一天
回复 支持 反对

使用道具 举报

发表于 2006-3-27 13:32:38 | 显示全部楼层
在程序中可以这样指定

  1. $(date +%Y%m%d --date='X days ago' )

复制代码
回复 支持 反对

使用道具 举报

发表于 2006-3-27 14:22:57 | 显示全部楼层
date -d 是那种系统上的,?
我这里是hp的不好用。
回复 支持 反对

使用道具 举报

发表于 2006-3-27 14:23:22 | 显示全部楼层
date -d 是那种系统上的,?
我这里是hp的不好用。
回复 支持 反对

使用道具 举报

发表于 2006-3-27 14:27:09 | 显示全部楼层
######判断昨天是哪一天#########
current_year=`date +%Y`
current_month=`date +%m`
current_date=`date +%d`

Fun_yesterday()
{
        case "$current_month" in
                01)
                        if(test "$current_date" -eq 1) then
                                handle_year=`expr $current_year - 1`
                                handle_month=12
                                handle_date=31                  
                        else
                                handle_year=$current_year
                                handle_month=$current_month
                                handle_date=`expr $current_date - 1`
                                if(test "$handle_date" -lt 10) then
                                        handle_date=0$handle_date
                                fi
                        fi;;                    
                02|04|06|08|09|11)
                        if(test "$current_date" -eq 1) then
                                handle_date=31
                                handle_month=`expr $current_month - 1`
                                if(test "$handle_month" -lt 10) then
                                        handle_month=0$handle_month
                                fi                              
                        else
                                handle_date=`expr $current_date - 1`
                                if(test "$handle_date" -lt 10) then
                                        handle_date=0$handle_date
                                fi
                                handle_month=$current_month
                        fi
                        handle_year=$current_year;;
                03)
                        if(test "$current_date" -eq 1) then
                                value1=`expr $current_year % 4`
                                value2=`expr $current_year % 100`
                                value3=`expr $current_year % 400`            
                                if(test "$value1" -eq 0 -a $value2 -ne 0 -o $value3 -eq 0) then
                                        handle_date=29
                                else
                                        handle_date=28
                                fi
                                handle_month=02
                        else
                                handle_date=`expr $current_date - 1`
                                if(test "$handle_date" -lt 10) then
                                        handle_date=0$handle_date
                                fi
                                handle_month=$current_month
                        fi
                        handle_year=$current_year;;
                05|07|10|12)
                        if(test "$current_date" -eq 1) then
                                handle_date=30
                                handle_month=`expr $current_month - 1`
                                if(test "$handle_month" -lt 10) then
                                        handle_month=0$handle_month
                                fi                              
                        else
                                handle_date=`expr $current_date - 1`
                                if(test "$handle_date" -lt 10) then
                                        handle_date=0$handle_date
                                fi
                                handle_month=$current_month
                        fi
                        handle_year=$current_year;;
                *)
                        echo "ERROR1!";;
        esac
        if(test $# -eq 0) then
                echo $handle_year$handle_month$handle_date
        else   
                for argument in $*
                do
                        case "$argument" in
                                y)
                                        echo $handle_year;;
                                m)
                                        echo $handle_month;;
                                d)
                                        echo $handle_date;;
                                c)
                                        echo $handle_year | cut -b3-4;;
                                *)
                                        echo "ERROR2!";;
                        esac
                        shift
                done
        fi
}
Fun_echo_argument()
{
        if(test "${1}" = "path") then
                echo "${2}"
        else #####"head"        
                echo "${3}"
        fi
}
yy=`Fun_yesterday y`
mm=`Fun_yesterday m`
dd=`Fun_yesterday d`
month=$mm
day=$dd
########################################################################

                                                                                                            
v_time=`date +%Y%m%d%H%M`                                                                                                           
###'----------------------'##########
回复 支持 反对

使用道具 举报

发表于 2006-3-28 09:35:31 | 显示全部楼层
[root@lvs-loadbanlancer ~]# date -d yesterday
Mon Mar 27 09:33:43 CST 2006
gnu/linux
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-29 08:44:10 | 显示全部楼层
在我的freebsd4.8上麵也無法使用以上幾種格式
回复 支持 反对

使用道具 举报

发表于 2006-3-31 09:52:24 | 显示全部楼层
$ date -d '20050301 1 day ago' "+%Y%m%d"
20050228

GNU/Linux
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表