Reputation: 83
I have a bash script, where I am trying to pass in the year and get the first day of the week of January 1st of that year.
I have tried using the date command:
yearsago=$(( 2021 - year))
date --date="$(date +'%Y-%m-01') - ${yearsago} year "
No luck. For example, if I enter in the year: 2020 as a variable. It should find the first day of that year, which is January 1st, and find the day of the week for January 1st, 2020.
Output would be if Monday, print "1", if Tuesday, print "2" Thank you.
Upvotes: 1
Views: 1154
Reputation: 3076
Please see the below code which will give you 1 first day of the year passed:
# !/usr/bin/bash
year=2020
firstday=`date -d "01 Jan $year" +'%A,%d'`
echo "First day of the year '$year' is : '$firstday'"
Output:
First day of the year '2020' is : 'Wednesday,01'
If you want to just get the day , then remove the %d option.
firstday=`date -d "01 Jan $year" +'%A`
Output :First day of the year '2020' is : 'Wednesday'
Upvotes: 1
Reputation: 140970
get the first day of the week of January 1st of that year.
Seems trivial:
date -d "$year-1-1" +%a
Upvotes: 1