问: given_day_of_the_week
的最新日期怎么样?
示例:例如,如果今天是 2016-04-21 ,我只想获取given_day_of_the_week
的日期,例如{{1这将是Monday
。如果代码明天运行,直到下一个2016-04-18
前一天仍然会返回此日期。
说明:代码应返回最新的given_day_of_the_week
(given_day_of_the_week
)日期,直到2016-04-24周日,然后运行相同的代码下周返回Monday
。
答案 0 :(得分:1)
需要GNU日期:
today_dow=$(date +%w)
days=(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
for ((dow=0; dow<7; dow++)); do
if ((dow < today_dow)); then
date -d "last ${days[dow]}"
else
date -d "${days[dow]}"
fi
done
Sun Apr 17 00:00:00 EDT 2016
Mon Apr 18 00:00:00 EDT 2016
Tue Apr 19 00:00:00 EDT 2016
Wed Apr 20 00:00:00 EDT 2016
Thu Apr 21 00:00:00 EDT 2016
Fri Apr 22 00:00:00 EDT 2016
Sat Apr 23 00:00:00 EDT 2016
所以我们可以这样做:
given_day_of_the_week() {
local days=(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
local today_dow=$(date +%w)
local dow datestr
for ((dow=0; dow<7; dow++)); do
if [[ "${days[dow],,}" == "${1,,}" ]]; then
if ((dow < today_dow)); then
datestr="last ${days[dow]}"
else
datestr="${days[dow]}"
fi
date -d "$datestr" "+%F"
fi
done
}
导致:
$ given_day_of_the_week tuesday
2016-04-19
$ given_day_of_the_week friday
2016-04-22
如果您使用其他语言环境,那么硬编码这样的工作日名称会给您带来问题
回应@ ryenus的评论:
$ given_day_of_the_week() {
local -A days=([sunday]=0 [monday]=1 [tuesday]=2 [wednesday]=3 [thursday]=4 [friday]=5 [saturday]=6)
local today_dow=$(date +%w)
local datestr=${1,,}
local dow=${days[$datestr]}
[[ -z "$dow" ]] && { echo "error: unknown day: '$1'" >&2; return 1; }
(( dow < today_dow )) && datestr="last $datestr"
date -d "$datestr" "+%F"
}
$ given_day_of_the_week friday
2016-04-22
$ given_day_of_the_week monday
2016-04-18
$ given_day_of_the_week FOO
error: unknown day: 'FOO'
答案 1 :(得分:0)
受@ glenn-jackman的回答启发,但更简单:
$(document).ready(function () {
var unsaved = false;
$(window).bind('beforeunload', function () {
if (unsaved) {
return "You have made some changes. Save your pending changes before existing the page";
}
});
// Monitor dynamic inputs
$(document).on('change', ':input', function () {
unsaved = true;
});
})
注意:如果在星期天使用,这个答案会给出明天的星期一日期。如果在星期一使用,它还会给出明天的星期二日期。不是100%确定这是你的想法。
如果相反的想法是总是与当前的星期日 - 星期六一起去,这将会这样做:
given_day_of_the_week=$1
f='%A %Y-%m-%d'
tomorrow=`date -d 'tomorrow' +"$f"`
today=`date -d 'today' +"$f"`
last5=`seq 1 5 | xargs -I{} date -d '{} day ago' +"$f"`
echo -e "$tomorrow\n$today\n$last5" |
grep -i $given_day_of_the_week |
cut -d' ' -f2
答案 2 :(得分:0)
# requires bash 4.x and GNU date
last_kday() {
local kday=$1
local -A numbers=([sunday]=0 [monday]=1 [tuesday]=2 [wednesday]=3
[thursday]=4 [friday]=5 [saturday]=6)
if [[ $kday == *day ]]; then
kday=${numbers[${kday,,}]}
elif [[ $kday != [0-6] ]]; then
echo >&2 "Usage: last_kday weekday"
return 1
fi
local today=$(date +%w)
local days_ago=$(( today - kday ))
if (( days_ago < 0 )); then let days_ago+=7; fi
date -d "$days_ago days ago" +%F
}