bash 从一年中的某一天获取一年中的一周
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3237882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get week of year from day of year
提问by conandor
Given day of year, how can I get the week of yearby using Bash?
给定一年中的某一天,如何使用 Bash获得一年中的一周?
回答by Massimo
Using the date
command, you can show the week of year using the "%V" format parameter:
使用该date
命令,您可以使用“%V”格式参数显示一年中的第几周:
/bin/date +%V
You can tell date
to parse and format a custom date instead of the current one using the "-d" parameter:
您可以date
使用“-d”参数告诉解析和格式化自定义日期而不是当前日期:
/bin/date -d "20100215"
Then, mixing the two options, you can apply a custom format to a custom date:
然后,混合这两个选项,您可以将自定义格式应用于自定义日期:
/bin/date -d "20100215" +%V
回答by Paused until further notice.
If you're using GNU date
you can use relative dates like this:
如果您使用的是 GNU,date
您可以使用这样的相对日期:
$ doy=193
$ date -d "Jan 1 +$((doy -1)) days" +%U
28
This would give you a very simplistic answer, but doesn't rely on date
:
这会给你一个非常简单的答案,但不依赖于date
:
$ echo $((doy / 7))
which pays no attention to the day of week.
它不注意星期几。
Here's a demonstration of the week numbering systems:
以下是周编号系统的演示:
$ printf "\nDate\t\tDOW\tDOY\t%%U %%V %%W\n"; \
for d in "Jan "{1..4}" 2010" \
"Dec "{25..31}" 2010" \
"Jan "{1..4}" 2011"; \
do printf "%s\t" "$d"; \
date -d "$d" +"%a%t%j%t%U %V %W"; \
done
Date DOW DOY %U %V %W
Jan 1 2010 Fri 001 00 53 00
Jan 2 2010 Sat 002 00 53 00
Jan 3 2010 Sun 003 01 53 00
Jan 4 2010 Mon 004 01 01 01
Dec 25 2010 Sat 359 51 51 51
Dec 26 2010 Sun 360 52 51 51
Dec 27 2010 Mon 361 52 52 52
Dec 28 2010 Tue 362 52 52 52
Dec 29 2010 Wed 363 52 52 52
Dec 30 2010 Thu 364 52 52 52
Dec 31 2010 Fri 365 52 52 52
Jan 1 2011 Sat 001 00 52 00
Jan 2 2011 Sun 002 01 52 00
Jan 3 2011 Mon 003 01 01 01
Jan 4 2011 Tue 004 01 01 01