您的位置:首页 > 运维架构 > Linux

linux的date的几个例子

2013-08-01 14:26 288 查看
shell脚本为test.sh:

input=$1

echo "sdfa:${input}"

echo ${input}

echo "dfadf"${input}

echo dfasdf{input}

echo $(date -d "${input}" +%Y)

echo $(date -d "${input}" +%m)

echo $(date -d "${input}" +%V)

./test.sh 2013-07-15 的执行结果为

sdfa:2013-07-15

2013-07-15

dfadf2013-07-15

dfasdf{input}

2013

07

29#第29周

%U周 (00-53) 星期日是一周的第一天
%u周 (00-53) 星期一是一周的第一天
%V周 (01-53) 星期日是一周的第一天,与 %X 使用
%v周 (01-53) 星期一是一周的第一天,与 %x 使用
注意date -d 命令中

1、空格的位置,还有起各个符号与参数间是否有空格

2、日期可以不加引号,date命令中的变量也可以不加引号

dateinput="2013-01-31"#等号前后不能有空格

date -d "2013-07-31" +%V

date -d "${dateinput}" +%Y

date -d "${dateinput}" +%V

date -d "${dateinput} -9 weeks" +%Y%m%d

dateinput=$1#注意等号前后不能有空格

year=$(date -d "${dateinput}" +%Y)

month=$(date -d "${dateinput}" +%m)

first_day_of_month=$(date -d "${dateinput}" +%Y%m01)

first_day_of_next_month=$(date -d "${dateinput} +1 month" +%Y%m01)

last_day_of_month=$(date -d "${first_day_of_next_month} -1 days" +%Y%m%d)

#echo "the last day:${last_day_of_month}"

dateinput2=$2

begin_date=$(date -d "${dateinput} +${dateinput2} month" +%Y%m01)

next_month_day=$(date -d "${begin_date} +1 month" +%Y%m%d)

end_date=$(date -d "${next_month_day} -1 days" +%Y%m%d)

echo "begin_date:${begin_date}"

echo "end_date:${end_date}"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: