How to Get Day of the Year in Shell

how to get day of the year in shell?

From the coreutils date manual:

%j     day of year (001..366)

How do I get the first day of the given year's day of the week in Bash Shell?

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'

Shell script: How to find what day of the year is today?

Try the %j format specifier:

$ date +%j
016

The standard says this about %j:

%j

Day of the year as a decimal number [001,366].

Get the date (a day before current time) in Bash

Sorry not mentioning I on Solaris system.
As such, the -date switch is not available on Solaris bash.

I find out I can get the previous date with little trick on timezone.

DATE=`TZ=MYT+16 date +%Y-%m-%d_%r`
echo $DATE

Get Month & Day from Date

In sh or bash:

D="2013/01/17"
DAY=$(date -d "$D" '+%d')
MONTH=$(date -d "$D" '+%m')
YEAR=$(date -d "$D" '+%Y')

echo "Day: $DAY"
echo "Month: $MONTH"
echo "Year: $YEAR"

shell script to get year, date and month from YYYY-MM-DD format

except for processing the string as text(with grep/sed/awk/cut/...), you could do with with date command:

kent$  date -d '2013-09-06' +%Y
2013

kent$ date -d '2013-09-06' +%m
09

kent$ date -d '2013-09-06' +%d
06


Related Topics



Leave a reply



Submit