13

I would like to use the date command on a Mac to find out the day corresponding to a certain date.

On Debian, the command below works:

date --date "Jul 20 1999" +%A

But the same command does not work when I run it on macOS' terminal.

What can be an alternative to achieve the output of the above command on a Mac?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Mohammad Jumah
  • 405
  • 1
  • 4
  • 14

2 Answers2

16

This works:

date -j -vJulm -v20d -v1999y '+%A'

According to the OSX date manual page (alternate link):

-v
Adjust (i.e., take the current date and display the result of the adjustment; not actually set the date) the second, minute, hour, month day, week day, month or year according to val. If val is preceded with a plus or minus sign, the date is adjusted forwards or backwards according to the remaining string, otherwise the relevant part of the date is set. The date can be adjusted as many times as required using these flags.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • 1
    The link is dead and was never recorded on archive.org. https://ss64.com/osx/date.html does not list the `-v` option, but https://www.unix.com/man-page/mojave/1/date/ does. – Socowi Jan 25 '23 at 16:22
1

Another way that may work:

date -j -f "%b %d %Y" "Jul 20 1999" "+%A"

It uses the format:

date -j -f "input_format" "datestring" "output_format"

where

  • -j
    • need to use it to tell date not to set the time and to use the -f flag which allows you to parse a date string and format it however you want
    • Man Page
      • "Do not try to set the date. This allows you to use the -f flag in addition to the + option to convert one date format to another."
  • -f
    • this allows you to define the format
    • Man Page
      • "Use input_fmt as the format string to parse the new_date provided rather than using the default [[[mm]dd]HH]MM[[cc]yy][.ss] format. Parsing is done using strptime(3)."

Some formatting options

  • %b for abbreviated Month name: Jul
  • %B for full Month name: July
  • %a for abbreviated weekday name: Tue
  • %A for full weekday name: Tuesday
  • %d for day of month: 20
  • %Y for 4 character year: 1999
    • Man Page
      • "year. This is normally at least four characters, but it may be more. Year ‘0000’ precedes year ‘0001’, and year ‘-001’ precedes year ‘0000’."