MacOS date
- convert date string to seconds since Epoch
date
command could understand months in the format of strings through the strptime(3)
libc function. I don't have macos so I can't test my answer, but according to macos date
man page:
date [-jRu] -f input_fmt new_date [+output_fmt]
[...]
-f 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).
Now you just need to understand how to format your input date from the man page of strptime(3)
:
%b or %B or %h
The month name according to the current locale, in abbreviated
form or the full name.
%d or %e
The day of month (1-31).
%Y The year, including century (for example, 1991).
So according to this, the input format should be: "%b/%e/%Y"
.
Next, you need to use the following flag to date
:
-j 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. Note that any date or time components unspecified
by the -f format string take their values from the current time.
And last, since you want to be able to substract the current time from your result, you need to ensure that your +output_fmt would be:
%s The number of seconds since the Epoch, 1970-01-01 00:00:00
+0000 (UTC). Leap seconds are not counted unless leap
second support is available.
So the result command that would transform your date format to epoch should be:
$ DATE="jan/01/2024"
$ date -jf "%b/%e/%Y" $DATE +%s
1704060000
Again, since I don't have macos I cannot test this to confirm. I'm also not sure if it would accept the month in lowercase, but if you have to capitalize the month, you could just replace $DATE
with ${DATE^}
Just for the sake of completeness, I'll also show how to perform this same operation with:
GNU date
- convert date string to seconds since Epoch
This is a bit easier, since GNU version of date
give more freedom
From its man page:
The --date=STRING is a mostly free format human readable date
string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29
16:21:42" or even "next Thursday". A date string may contain
items indicating calendar date, time of day, time zone, day of
week, relative time, relative date, and numbers. An empty string
indicates the beginning of the day. The date string format is
more complex than is easily documented here but is fully
described in the info documentation.
So from my test on my machine, you should just remove the slashes from your input for date
command to understand it:
$ DATE="jan/01/2024"
$ echo ${DATE//\// }
jan 01 2024
$ date --date "${DATE//\// }" +%s
1704060000
Substract from your current date
Now you have the Epoch time of your original date. Just store it into a variable. For instance, in MacOS:
$ ORIG_DATE=$(date -jf "%b/%e/%Y" $DATE +%s)
To see the current date in seconds since epoch:
$ date +%s
1704965965
So now to calculate the difference in seconds, it's trivial:
$ echo $(( $(date +%s ) - $ORIG_DATE ))
905967