使用bash日期,我可以让它返回相对于当前时间的一周中的某一天.
date --d='last Sunday' #Returns date of the Sunday before today
Run Code Online (Sandbox Code Playgroud)
我还可以让它返回相对于其他日期的一天
date --d='02/1/2012 -2 days' #Returns date two days before Feb. 1, 2012
Run Code Online (Sandbox Code Playgroud)
但是,相对于某些非当前时间,我怎样才能让它返回星期几呢?我想做的是:
date --d='Sunday before 02/1/2012' #Doesn't work! I want Sunday before Feb. 1
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我甚至希望能够链接字符串,以便我可以引用新日期的相对日期:
# Should return 2 days before the Sunday before Feb. 1, 2012
date --d='Sunday before 02/1/2012 - 2 days'
Run Code Online (Sandbox Code Playgroud)
虽然这种链接并不重要.bash日期是否有某种方式可以根据一周的相对日期返回一天?
你可以使用一个小数字算术:
base="02/1/2012"
feb1_dayofweek=$( date -d $base +%w )
target_dayofweek=0 # sunday
date -d "$base - $(( (7 + feb1_dayofweek - target_dayofweek) % 7 )) days"
Run Code Online (Sandbox Code Playgroud)
结果:
Sun Jan 29 00:00:00 EST 2012
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8957 次 |
最近记录: |