0

How to get second day(date) of current month (e.g 2 August) in HPUX.

date -d doesn't work in HPUX Unix.

1
  • 3
    You mean what day of week is it - Monday, Tuesday, etc? Aug 25 at 15:37
1

With GNU and busybox implementation:

$ date -d '2020-08-02' "+%A"
Sunday

With FreeBSD:

$ date -j -f "%d %m %Y" "02 08 2020" "+%A"
Sunday

If you want to get the 2nd day of all months in a year you can use this days-in-a-year.sh script:

#!/usr/bin/env sh

year=2021

month=1
while [ $month -le 12 ]; do
    # GNU and busybox
    date -d "$year-$month-02" "+%d %B %Y: %A"

    # FreeBSD
    # date -j -f "%d %m %Y" "02 $month $year" "+%d %B %Y: %A"

    month=$(( month + 1 ))
done

Output:

$ ./days-in-a-year.sh
02 January 2021: Saturday
02 February 2021: Tuesday
02 March 2021: Tuesday
02 April 2021: Friday
02 May 2021: Sunday
02 June 2021: Wednesday
02 July 2021: Friday
02 August 2021: Monday
02 September 2021: Thursday
02 October 2021: Saturday
02 November 2021: Tuesday
02 December 2021: Thursday

And BTW, there is a nifty utility called cal which is available in FreeBSD and on Linux as a part of util-linux package and can display calendar in the command line:

$ cal
     August 2021
Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
1
1

Using what I believe is a POSIX solution, which should work on HPUX as required, you can pick out the second day of any month from cal

year=2021 month=8    # Omit entirely for current year and month

cal $month $year |
    sed -nE 's/ 2( .*|)$//p' |
    awk '{ split("Sun Mon Tue Wed Thu Fri Sat",h); print h[length($0)/3+1]}'

Output

Mon

Explanation

The cal command prints a calendar:

cal

    August 2021
Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

We look for a pattern {space} 2 {space} or a {space} 2 at the end of a line, and delete everything past it. We then divide the length of the resulting line by three (there are three characters per column of the week), and add 1 to index the array of weekdays.

(The sed could have been folded into the awk but I felt the additional complexity wasn't worth the effort.)

From here it's then straightforward to iterate across the months in the year, which it seems you might want:

year=2021
for month in January February March April ...
do
    day=$(
        cal "$month" "$year" |
        sed -nE 's/ 2( .*|)$//p' |
        awk '{
            split("Sunday Monday Tuesday Wednesday Thursday Friday Saturday", h);
            print h[length($0)/3+1]
        }'
    )
    printf "%02d %s %d: %s\n" 2 "$month" "$year" "$day"
done
0
cu=`date +%d`

 date --date="$(date --date='1 month ago') -$cu days +2days" +%A" "%Y-%m-%d

Friday 2021-07-02



date --date="$(date --date='1 month') -$cu days +2days" +%A" "%Y-%m-%d 
Thursday 2021-09-02
2
  • 3
    date --date is identical to date -d. It still doesn't work on HPUX
    – roaima
    Aug 25 at 19:46
  • 1
    Hi Praveen Kumar,. Your command is working in Linux however I have Hpux server so it's not working there. Usually for calculating yesterday day we use below syntax.datecalc Today -1 day pic dd-mm-yyy. Could u pls help on this Aug 26 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.