bash 我们如何根据unix中的给定日期获得工作日

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46024251/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:25:58  来源:igfitidea点击:

How can we get weekday based on given date in unix

bashshelldatetimeunixunix-timestamp

提问by Divya S

From a given date in %m-%d-%Y format we should determine what day it is Please help me with the command

从 %m-%d-%Y 格式的给定日期,我们应该确定它是哪一天请帮我使用命令

like if i pass a date like 09-01-2017 output should be Friday

就像我传递一个日期,比如 09-01-2017 输出应该是星期五

回答by Rajeev Ranjan

DayOfWeek=$(date +%A)

This would yield the day of week monday-sundayIf your input date is strictly in the format MM-DD-YYYY, use the following

这将产生星期几monday-sunday如果您的输入日期严格按照格式MM-DD-YYYY,请使用以下内容

IFS='-' read -ra ADDR <<< "09-01-2017"
formattedDate=${ADDR[2]}-${ADDR[0]}-${ADDR[1]}
date -d  $formattedDate +%A

The first line tokenizes the components of the date and the second rearranges them

第一行标记日期的组成部分,第二行重新排列它们

回答by Juan Manuel García

If you have your date like this:

如果你的约会对象是这样的:

d="09-01-2017"

you need to reformat it to "YYYY-MM-DD"

您需要将其重新格式化为“YYYY-MM-DD”

date -d $(echo $d|awk -F- '{print  "-"  "-" }') +%A # DOW 

回答by nagendra547

Very simple. Just use the date command itself with correct options.

很简单。只需使用带有正确选项的 date 命令本身即可。

$ date -j -f '%m-%d-%Y' "09-01-2017" +'%A'
Friday

回答by stack0114106

You can pass it as %m/%d%Y which gets recognized by the date command.

您可以将它作为 %m/%d%Y 传递,它会被 date 命令识别。

$ date --date="`echo 09-01-2017| sed -e 's/-/\//g' `" +'%A'
Friday

To verify it, pass %F to get it in ISO format

要验证它,请传递 %F 以获取 ISO 格式

$ date --date="`echo 09-01-2017| sed -e 's/-/\//g' `" +'%A %F'
Friday 2017-09-01