如何获取当月3.星期三的日期

时间:2018-10-16 22:55:18

标签: bash date

我需要一个代码示例来获取每月3.周三的日期。 我根本无法解决这个问题-上周三很简单:

cal | awk '/^ *[0-9]/ { d=$4 } END { print d }'

最终,我需要脚本返回“下一个3.周三”-就像我们已经通过了本月的3.周三一样,返回下个月的3.周三。

5 个答案:

答案 0 :(得分:2)

这是一种简单的方法,在大多数地区都可以正常使用:

#!/bin/bash

export LC_TIME=C
thismonth=$(date +%m)       # this month like "10"
thisyear=$(date +%Y)        # this year like "2018"
firstdayofweek=$(date -d "${thisyear}-${thismonth}-1" +%w)
# calculates the day of week of the 1st day of the month
# returns the number between 0 and 6, where 0=Sun, 1=Mon, ...

wed1=$(( (10 - $firstdayofweek) % 7 + 1 ))
# calculates the day of month of the 1st Wednesday
wed3=$(( $wed1 + 14 ))
# the day of the 3rd Wednsday

echo $wed3

从今天开始会产生“ 17”。
修改上面的代码以实现下一个目标很容易。

答案 1 :(得分:1)

如果您有\uffffffffff

// Match \u0123 substrings (note this will match invalid codepoints such as \u123456789).
final RegExp r = RegExp(r'\\\\u([0-9a-fA-F]+)');

// Sample string to parse.
final String source = r'\\u0414\\u043B\\u044F \\u043F\\u0440\\u043E\\u0434\\u0430\\u0436\\u0438 \\u043D\\u0435\\u0434\\u0432\\u0438\\u0436\\u0438\\u043C\\u043E\\u0441\\u0442\\u0438';

// Replace each \u0123 with the decoded codepoint.
final String decoded = source.replaceAllMapped(r, (Match m) {
  // Extract the parenthesised hex string. '\\u0123' -> '123'.
  final String hexString = m.group(1);

  // Parse the hex string to an int.
  final int codepoint = int.parse(hexString, radix: 16);

  // Convert codepoint to string.
  return String.fromCharCode(codepoint);
});

ncal格式是cal的转置,对行的处理更容易。第三个星期三是第四个字段。

ncal

例如11月

$ ncal | awk '/We/{print $4}'

答案 2 :(得分:0)

这不是很漂亮,但是在GNU / Linux系统上,您可以在每月的每一天进行循环并计数:

#!/bin/bash
month="$1"   # e.g. 2018-10
number="$2"  # e.g. 3
weekday="$3" # e.g "Wednesday"

export LC_ALL=C TZ=UTC
date -d today > /dev/null 2>&1 || {
  echo "You are not using GNU date"
  exit 1
}

n="$number"
for day in {1..31}
do
  wd=$(date -d "$month-$day" +"%A" 2>&1) || continue
  if [[ $wd == "$weekday" ]]
  then
    (( --n )) || break
  fi
done

if ! (( n ))
then
  echo "The $weekday number $number in $month is $month-$day"
else
  echo "$month does not have a $weekday number $number"
fi

但是,请注意,日期很难,并且这种简单的方法可能无法正确解释各种好奇心,例如萨摩亚由于时区偏移而跳过2011-12-30星期五。

以下是GNU系统上的一些示例:

$ ./finddate 2018-10 3 Wednesday
The Wednesday number 3 in 2018-10 is 2018-10-17

$ ./finddate 2018-10 5 Thursday
2018-10 does not have a Thursday number 5

以下是来自非GNU系统(例如MacOS)的示例:

$ ./finddate 2018-10 3 Wednesday
You are not using GNU date

答案 3 :(得分:0)

这是我想出的:

#!/bin/bash -xv

CURRENTDAY=$(date +%e)
CURRENTMONTH=$(date +%m)
NEXTMONTH=$(( ${CURRENTMONTH}+1 ))
CURRENTYEAR=$(date +%Y)

WEDNESDAYSFORTHEMONTH=$(cal ${CURRENTMONTH} ${CURRENTYEAR} | awk 'NF <= 7 { print $4 }' | grep -v "^$"| grep -v We)

# I would normally start at 0
COUNTER=1 

#Get this months 3rd Wednesday
for WEDNESDAY in ${WEDNESDAYSFORTHEMONTH}
do
    if [[ ${COUNTER} -eq 3 ]] 
    then
        echo "This months third wednesday is: ${WEDNESDAY}"
        THISMONTHSTHIRDWEDNESDAY=${WEDNESDAY}
    fi
    COUNTER=$(( ${COUNTER}+1 ))

done

# Get the 3rd Wednesday for the next month

WEDNESDAYSFORTHEMONTH=$(cal ${NEXTMONTH} ${CURRENTYEAR} | awk 'NF <= 7 { print $4 }' | grep -v "^$"| grep -v We)
COUNTER=1

for WEDNESDAY in ${WEDNESDAYSFORTHEMONTH}
do
    if [[ ${COUNTER} -eq 3 ]]
    then
        echo "Next months third wednesday is: ${WEDNESDAY}"
        NEXTMONTHSTHIRDWEDNESDAY=${WEDNESDAY}
    fi
    COUNTER=$(( ${COUNTER}+1 ))
done

#Compare the current date with this months 3rd Wednesday
if [[ ${CURRENTDAY} -le ${THISMONTHSTHIRDWEDNESDAY} ]]
then
    echo "Wednesday to use in your script is: ${THISMONTHSTHIRDWEDNESDAY} ${CURRENTMONTH} ${CURRENTYEAR}"
else
    echo "Wednesday to use in your script is: ${NEXTMONTHSTHIRDWEDNESDAY} ${NEXTMONTH} ${CURRENTYEAR}"
fi

答案 4 :(得分:0)

# Third Wednesday of this month
thirdWednesday=$( cal | cut -c 10-12 | sed '/^\s*$/d' | sed -n '5p' )

# Today
today=$( date +%d )

# If already passed
if [ $today -gt $thirdWednesday ]; then
    # Next month
    nextMonth=$( date +"%m %Y" -d "next month" )

    # Get third Wednesday of next month
    thirdWednesday=$( cal $nextMonth | cut -c 10-12 | sed '/^\s*$/d' | sed -n '5p'  )
fi

# Result
echo $thirdWednesday