请教各位,shell脚本获取当前日期是本年的第几周?

jimzhaoyun 2009-07-24 11:43:00
我想要得到当前日期是本年的第几周,以及上一周和下一周在本年是第几周?
我不知道如何用脚本去写,还希望哪位能指点一下,谢谢!
...全文
1536 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
brookmill 2009-07-25
  • 打赏
  • 举报
回复
楼主太客气了。
其实,本来我也不会,临时翻书查手册,现学现卖。
jimzhaoyun 2009-07-25
  • 打赏
  • 举报
回复
我没啥分,brookmill别介意啊,以后有问题我还会来找你的哦
jimzhaoyun 2009-07-25
  • 打赏
  • 举报
回复
brookmill这么晚还在哦,谢谢了。我是个菜鸟,一边在学习,一边奏合着做。恰遇到这个问题了。
brookmill 2009-07-25
  • 打赏
  • 举报
回复
这样也可以
PREV=$(($NOW - 1))
NEXT=$(($NOW + 1))
brookmill 2009-07-25
  • 打赏
  • 举报
回复
我不太会写shell,这个凑合着试试吧

#!/bin/bash
NOW=`date +%V`
PREV=`expr $NOW - 1`
NEXT=`expr $NOW + 1`
echo $PREV
echo $NOW
echo $NEXT
ShowMan 2009-07-25
  • 打赏
  • 举报
回复
shell:date 常用方式
在linux下获取时间字符串
命令 date
# 以yyyymmdd格式输出23天之前现在这个时刻的时间
$ date +%Y%m%d –date=’23 days ago’

$ date -u
Thu Sep 28 09:32:04 UTC 2006

$ date -R
Thu, 28 Sep 2006 17:32:28 +0800

# 测试十亿分之一秒
$ date +’%Y%m%d %H:%M:%S.%N’;date +’%Y%m%d %H:%M:%S.%N’;date +’%Y%m%d %H:%M:%S.%N’;date +’%Y%m%d %H:%M:%S.%N’
20060928 17:44:20.906805000
20060928 17:44:20.909188000
20060928 17:44:20.911535000
20060928 17:44:20.913886000
date 参考
$ date –help
Usage: date [OPTION]… [+FORMAT]
or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

-d, –date=STRING display time described by STRING, not `now’
# such as ‘n days ago |1 month ago|n years ago’
-f, –file=DATEFILE like –date once for each line of DATEFILE
-ITIMESPEC, –iso-8601[=TIMESPEC] output date/time in ISO 8601 format.
TIMESPEC=`date’ for date only,
`hours’, `minutes’, or `seconds’ for date and
time to the indicated precision.
–iso-8601 without TIMESPEC defaults to `date’.
-r, –reference=FILE display the last modification time of FILE
-R, –rfc-2822 output RFC-2822 compliant date string
-s, –set=STRING set time described by STRING
-u, –utc, –universal print or set Coordinated Universal Time
–help display this help and exit
–version output version information and exit

FORMAT controls the output. The only valid option for the second form
specifies Coordinated Universal Time. Interpreted sequences are:

%% 输出%符号 a literal %
%a 当前域的星期缩写 locale’s abbreviated weekday name (Sun..Sat)
%A 当前域的星期全写 locale’s full weekday name, variable length (Sunday..Saturday)
%b 当前域的月份缩写 locale’s abbreviated month name (Jan..Dec)
%B 当前域的月份全称 locale’s full month name, variable length (January..December)
%c 当前域的默认时间格式 locale’s date and time (Sat Nov 04 12:02:33 EST 1989)
%C n百年 century (year divided by 100 and truncated to an integer) [00-99]
%d 两位的天 day of month (01..31)
%D 短时间格式 date (mm/dd/yy)
%e 短格式天 day of month, blank padded ( 1..31)
%F 文件时间格式 same as %Y-%m-%d
%g the 2-digit year corresponding to the %V week number
%G the 4-digit year corresponding to the %V week number
%h same as %b
%H 24小时制的小时 hour (00..23)
%I 12小时制的小时 hour (01..12)
%j 一年中的第几天 day of year (001..366)
%k 短格式24小时制的小时 hour ( 0..23)
%l 短格式12小时制的小时 hour ( 1..12)
%m 双位月份 month (01..12)
%M 双位分钟 minute (00..59)
%n 换行 a newline
%N 十亿分之一秒 nanoseconds (000000000..999999999)
%p 大写的当前域的上下午指示 locale’s upper case AM or PM indicator (blank in many locales)
%P 小写的当前域的上下午指示 locale’s lower case am or pm indicator (blank in many locales)
%r 12小时制的时间表示(时:分:秒,双位) time, 12-hour (hh:mm:ss [AP]M)
%R 24小时制的时间表示 (时:分,双位)time, 24-hour (hh:mm)
%s 自基础时间 1970-01-01 00:00:00 到当前时刻的秒数 seconds since `00:00:00 1970-01-01 UTC’ (a GNU extension)
%S 双位秒 second (00..60); the 60 is necessary to accommodate a leap second
%t 横向制表位(tab) a horizontal tab
%T 24小时制时间表示 time, 24-hour (hh:mm:ss)
%u 数字表示的星期(从星期一开始 1-7)day of week (1..7); 1 represents Monday
%U 一年中的第几周星期天为开始 week number of year with Sunday as first day of week (00..53)
%V 一年中的第几周星期一为开始 week number of year with Monday as first day of week (01..53)
%w 一周中的第几天 星期天为开始 0-6 day of week (0..6); 0 represents Sunday
%W 一年中的第几周星期一为开始 week number of year with Monday as first day of week (00..53)
%x 本地日期格式 locale’s date representation (mm/dd/yy)
%X 本地时间格式 locale’s time representation (%H:%M:%S)
%y 两位的年 last two digits of year (00..99)
%Y 年 year (1970…)
%z RFC-2822 标准时间格式表示的域 RFC-2822 style numeric timezone (-0500) (a nonstandard extension)
%Z 时间域 time zone (e.g., EDT), or nothing if no time zone is determinable
jimzhaoyun 2009-07-25
  • 打赏
  • 举报
回复
当前是第几周这个我知道,不过下一周和上一周是本年的第几周怎么取呢?
brookmill 2009-07-24
  • 打赏
  • 举报
回复
%U week number of year, with Sunday as first day of week (00..53)

%V ISO week number, with Monday as first day of week (01..53)

%w day of week (0..6); 0 is Sunday

%W week number of year, with Monday as first day of week (00..53)
brookmill 2009-07-24
  • 打赏
  • 举报
回复
$ man date
......
%V ISO week number, with Monday as first day of week (01..53)
brookmill 2009-07-24
  • 打赏
  • 举报
回复
date +%V

23,124

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧