Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: How to find/display out last Friday's date of the month

  1. #1
    Join Date
    Aug 2011
    Beans
    15

    How to find/display out last Friday's date of the month

    Hello,

    Can you please help me find/display out last Friday's date of the month using command in Unix/Linux

  2. #2
    Join Date
    May 2009
    Location
    Fareham, UK
    Beans
    1,524
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: How to find/display out last Friday's date of the month

    Yes
    Code:
    date --date="last Fri"
    This will accept arguments found in the man date page, for example
    Code:
    mark@desktop ~/ $ date --date="last Fri" +%x
    15/03/13
    Last edited by CaptainMark; March 21st, 2013 at 08:55 AM.
    Catch me on Freenode - imark

  3. #3
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: How to find/display out last Friday's date of the month

    This will show the last Friday counting from now. What the OP wants is something like what you get in ksh93 with
    Code:
    ksh93 -c 'printf "%(%x)T\n" "last fri in apr 2013"'
    or in perl with
    Code:
     perl -MDate::Manip -e 'print UnixDate(ParseDate("last fri in apr 2013"), "%F"),"\n"'
    Last edited by schragge; March 21st, 2013 at 08:42 PM.

  4. #4
    Join Date
    Aug 2011
    Beans
    15

    Re: How to find/display out last Friday's date of the month

    Quote Originally Posted by CaptainMark View Post
    Yes
    Code:
    date --date="last Fri"
    This will accept arguments found in the man date page, for example
    Code:
    mark@desktop ~/ $ date --date="last Fri" +%x
    15/03/13
    This shows previous friday's date...not last friday's date of the month.
    I already tried this option...The output date should be 29 march....

  5. #5
    Join Date
    Aug 2011
    Beans
    15

    Re: How to find/display out last Friday's date of the month

    Hi schragge.

    Appreciating your help..
    this logic works for me.

    Is there any method that we don't have to manually change "last fri in apr 2013" every time..just execute the command in any month and will show last day's date of month.

  6. #6
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: How to find/display out last Friday's date of the month

    In ksh93, you can simply change the example to
    Code:
    ksh93 -c 'printf "%(%x)T\n" "last fri in this month"'
    This doesn't work in perl, better read up the documentation and experiment a bit.

    Or you can do it like this:
    Code:
    cal -h|cut -c16,17|sed '/^ *$/d'|tail -1
    Last edited by schragge; March 21st, 2013 at 10:03 AM.

  7. #7
    Join Date
    Sep 2009
    Location
    Freiburg/Germany
    Beans
    1,112
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: How to find/display out last Friday's date of the month

    Code:
     cal -N |awk '/^Fr/ {print $(NF)}'
    prints just the day of month.
    Use something like
    Code:
    date +"%Y-%m-$(cal -N |awk '/^Fr/ {print $(NF)}')"
    to get a full date.
    ClassicMenu Indicator - classic GNOME menu for Unity
    Unsettings - configuration program for the Unity
    Privacy Indicator - easily switch privacy settings in Unity
    Arronax - create and modify app starters

  8. #8
    Join Date
    May 2007
    Location
    albuquerque
    Beans
    581
    Distro
    Kubuntu 20.04 Focal Fossa

    Re: How to find/display out last Friday's date of the month

    Quote Originally Posted by sunnysthakur View Post
    The output date should be 29 march....
    Quote Originally Posted by diesch View Post
    Use something like
    Code:
    date +"%Y-%m-$(cal -N |awk '/^Fr/ {print $(NF)}')"
    to get a full date.
    So then:

    Code:
    $ date +"$(cal -N |awk '/^Fr/ {print $(NF)}') %B"
    29 March

  9. #9
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: How to find/display out last Friday's date of the month

    Bit of a brute force approach using the "nth day" syntax of date: find the Nth day of week in the future that is still the right month:
    Code:
    thismonth=$(date +%m)
    for i in {5..0};
    do 
        trymonth=$(date -d "$i friday" +%m)
        if [[ $trymonth == $thismonth ]] 
        then 
            goodone=$i 
            break
        fi
    done
    [[ -z $goodone ]] || date -d "$goodone friday"
    Of course, plenty of interesting things happen if you are on or past that last day ($goodone empty

  10. #10
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: How to find/display out last Friday's date of the month

    Why would $goodone be empty on the last Friday? Isn't -d "0 friday" aka -d "this friday" supposed to return the current month number on that day?

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •