I want to get the date +%m/%d/%Y a different number of days ago.
$(( $(date +%s)- 259200)) gives me the seconds of 3 days ago. The -d parameter doesn't accept the seconds to parse it in the format I want 05/06/2011 (error message: date: invalid date '1307284916').
Is there a way to get date to work?
To get date on linux to accept a simple timestamp for -d, prefix the number with the # sign!
~% date +%s
1307548153
~% date -d #1307548153
Wed Jun 8 16:49:13 IST 2011
On MacOS X, you can use date -r 1307284916 +%m/%d/%Y to get the result you want. If you are not on a BSD-derived system like MacOS X (e.g. if you are using Linux), there doesn't seem to be a simple analogue of date -r. And traditional versions of Unix usually don't even provide the facilities of GNU's date.
If you get really stuck, contact me (see my profile) for a program timestamp:
$ timestamp -T '%m/%d/%Y' 1307284916
1307284916 = 06/05/2011
$ timestamp -n -T '%m/%d/%Y' 1307284916
06/05/2011
$ timestamp -T '%m/%d/%Y' 1307284916 $(( $(date +%s)- 259200))
1307284916 = 06/05/2011
1307288704 = 06/05/2011
$
Related
I'm trying to calculate the number of seconds since the Epoch, using date on MacOS BSD.
I can get a one year ago date string:
$ date -v -1y
Tue Apr 21 10:44:47 EST 2020
...but I can't figure out how to convert it into seconds since Epoch. Any suggestions?
Add +%s to tell it to print the datetime as seconds since the epoch:
date -v -1y +%s
The + is a date option to set the output format, and %s is strftime format for "seconds since epoch".
Portability note: while the +%s part is pretty standard and portable (though the %s format is not actually required by POSIX), the -v -1y part is wildly nonportable. With GNU date (e.g. on most Linuxes), you'd use something like --date='1 year ago' instead. On NetBSD, -d '1 year ago' works. Check your local man page to see what your system supports.
I am trying to subtract 5 minutes from date but its giving unexpected output.
$ date -j -f "%Y/%m/%d %H:%M:%s" -v "-5M" "2021/03/01 09:11:14"
Thu Jan 1 05:25:14 IST 1970
Please suggest the correction.
Converting my comment to answer so that solution is easy to find for future visitors.
This date command should work on BSD date:
date -j -f "%Y/%m/%d %H:%M:%S" -v "-5M" "2021/03/01 09:11:14"
Issue in your command was use of .%s instead of .%S for the second component.
What I intent to get is
$ xxx 2019-10-11 <= insert your command
1570752000
The output is timestamp in Oct 11 00:00:00 UTC 2019. I find a good way to do this in gnu, but not in bsd
This should work:
date -j -f '%F %T %Z' '2019-10-11 00:00:00 U' '+%s'
-j is for dry-run; i.e it prevents date from changing system date and time,
-f is for specifying input format,
+%s is for converting given date to seconds since Epoch.
On NetBSD the following will work:
TZ=GMT0 date -d '2019-10-11 00:00:00' '+%s'
Note the use of the TZ environment variable to specify the input timezone instead of trying to have it parsed from the input (though it may be possible to have a more properly formatted timezone parsed from the input, though then that leaves the question of what timezone the output should be formatted in).
On MacOS you might try something similar to what Oguz suggested:
TZ=GMT0 date -j -f '%Y-%m-%d %H:%M:%S' '2019-10-11 00:00:00' '+%s'
In mac osx bsd, date -vmon will convert the current date to Monday's date and date +%w displays day of week.
Say, I have input date 9/14/16 in the format mm/dd/yy. How can I convert this string pattern to a date object so that I could perform date functions like -vmon and +%w on it.
I tried the following: startDate=$(date -jf "%m/%d/%y" "9/14/16") and then tried to perform $startDate +"%w" but it doesn't work. I doubt that the startDate is not date and actually String.
How can I convert the string to date so that I perform date manipulation on it?
Edit: The requirement for doing this is: Say an input date is given. Then corresponding to that date, I want to get the beginning and ending working dates of the week i.e Monday's date and Friday's date. Then I want to get next week's Monday's date and Friday's date. How can I do this?
You need to use date twice, once to convert from the string input to an 'epoch time' (the number of seconds since The Epoch — aka 1970-01-01 00:00:00 +00:00), and once to convert an epoch time to an output format of your choosing:
rdate="9/14/16"
epoch_time=$(date -j -f '%m/%d/%y' "$rdate" +'%s')
date -j -vmon -r "$epoch_time"
date -j -vfri -r "$epoch_time" +'%w'
That gave me the output:
Mon Sep 12 10:14:44 PDT 2016
5
Note that Friday is day 5 of the week, so the output is correct. The use of %w when you specify -vdow seems a little moot; you know that the output will be 5. But you can use any other format characters that you need too.
Note that the time portion is defaulted to the current time. You could add 00:00:00 to the converted date and %H:%M:%S to the conversion format (-f argument) to work with midnight, etc.
You can combine it into one command line:
$ date -j -vmon -r $(date -j -f '%m/%d/%y %H:%M:%S' "$rdate 00:00:00" +'%s') +'%Y-%m-%d %H:%M:%S %w %s'
2016-09-12 00:00:00 1 1473663600
$
The last example on the man page for date on Mac OS X mentions the %s format specifier, which is the key to getting this to work. Well, that and using date twice.
Note that the man page gives synopses:
date [-ju] [-r seconds] [-v [+|-]val[ymwdHMS]] ... [+output_fmt]
date [-jnu] [[[mm]dd]HH]MM[[cc]yy][.ss]
date [-jnu] -f input_fmt new_date [+output_fmt]
date [-d dst] [-t minutes_west]
The 'usage' message is less helpful:
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
The usage message doesn't show that you can't use -f with -r or -v, whereas the manual is clear that you cannot.
Tested: Mac OS X 10.11.6 El Capitan
In a Bash script, I want to print the current datetime in ISO 8601 format (preferably UTC), and it seems that this should be as simple as date -I:
http://ss64.com/bash/date.html
But this doesn't seem to work on my Mac:
$ date -I
date: illegal option -- I
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
And indeed, man date doesn't list this option.
Anyone know why this is, or any other (easy) way for me to print the date in ISO 8601 format? Thanks!
You could use
date "+%Y-%m-%d"
Or for a fully ISO-8601 compliant date, use one of the following formats:
date -u +"%Y-%m-%dT%H:%M:%SZ"
Output:
2011-08-27T23:22:37Z
or
date +%Y-%m-%dT%H:%M:%S%z
Output:
2011-08-27T15:22:37-0800
In GNU date date -I is the same as date +%F, and -Iseconds and -Iminutes also include time with UTC offset.
$ date +%F # -I or +%Y-%m-%d
2013-05-03
$ date +%FT%T%z # -Iseconds or +%Y-%m-%dT%H:%M:%S%z
2013-05-03T15:59:24+0300
$ date +%FT%H:%M # -Iminutes or +%Y-%m-%dT%H:%M%z
2013-05-03T15:59+0300
-u is like TZ=UTC. +00:00 can be replaced with Z.
$ date -u +%FT%TZ
2013-05-03T12:59:24Z
These are also valid ISO 8601 date or time formats:
20130503T15 (%Y%m%dT%M)
2013-05 (%Y%m)
2013-W18 (%Y-W%V)
2013-W18-5 (%Y-W%V-%u)
2013W185 (%YW%V%u)
2013-123 (%Y-%j, ordinal date)
2013 (%Y)
1559 (%H%M)
15 (%H)
15:59:24+03 (UTC offset doesn't have to include minutes)
These are not:
2013-05-03 15:59 (T is required in the extended format)
201305 (it could be confused with the YYMMDD format)
20130503T15:59 (basic and exteded formats can't be mixed)
A short alternative that works on both GNU and BSD date is:
date -u +%FT%T%z
The coreutils package provides GNU versions of tools. To install:
$ brew install coreutils
You can see what's provided:
$ brew list coreutils
Notice it comes with date:
$ brew list coreutils | grep date
This is the standard GNU date command so it'll take the -I switch:
$ gdate -I
2016-08-09
Just use normal date formatting options:
date '+%Y-%m-%d'
Edit: to include time and UTC, these are equivalent:
date -u -Iseconds
date -u '+%Y-%m-%dT%k:%M:%S%z'
Taking the other answers one step further, you could add a function to your ~/.bashrc or ~/.zshrc to add the date -I flag:
date() {
if [ "$1" = "-I" ]; then
command date "+%Y-%m-%dT%H:%M:%S%z"
else
command date "$#"
fi
}
It's not a feature of Bash, it's a feature of the date binary. On Linux you would typically have the GNU coreutils version of date, whereas on OSX you would have the BSD legacy utilities. The GNU version can certainly be installed as an optional package, or you can roll your own replacement - I believe it should be a simple one-liner e.g. in Perl.
There's a precompiled coreutils package for Mac OS X available at:
http://rudix.org/packages-abc.html#coreutils.
I regularly use 'date -I' in Linux when saving files. ex: touch x.date -I. While the equivalent in MacOS is 'date +%F', it is a bit awkward to type every time I save a file. So, I set an alias "alias dt='date +%F'" then touch x.dt gives me the date.