This converts the given datetime to epoch time (seconds)
date -d"2015-07-24 11:29:00" +%s
// gives 1437762540
Now I want to do the same thing, but subtract a day. Normally, this is as simple as adding "- 1 day". However, instead of subtracting a day, it actually adds a day.
date -d"2015-07-24 11:29:00 - 1 day" +%s
// gives 1437848940 (notice, this value is great than the one above)
If I take away the time portion from my timestamp it works great. The time portion seems to break it however. I know I can do this in two separate steps and avoid this problem. However, I was hoping to do it in one command. Is this possible?
There is some ambiguity in the date command about how to interpret the - 1 token in your date string. It resolves it as a time zone specification
$ date -d "2015-07-24 11:29:00 -1"
Fri Jul 24 08:29:00 EDT 2015
$ date -d "2015-07-24 11:29:00 UTC-1"
Fri Jul 24 08:29:00 EDT 2015
$ date -d "2015-07-24 11:29:00 - 2"
Fri Jul 24 09:29:00 EDT 2015
$ date -d "2015-07-24 11:29:00 UTC-2"
Fri Jul 24 09:29:00 EDT 2015
$ TZ=UTC date -d "2015-07-24 11:29:00 -1"
Fri Jul 24 12:29:00 UTC 2015
(your results may vary depending on your TZ setting)
The day part is then interpreted to mean add one day
$ date -d "2015-07-24 11:29:00 - 1 day"
Sat Jul 25 12:29:00 EDT 2015
$ date -d "2015-07-24 11:29:00 UTC-1 + 1 day"
Sat Jul 25 12:29:00 EDT 2015
Add a timezone spec to your date string, as #amdixon suggests, to resolve the ambiguity and get the expected results.
For reference, the GNU coreutils info page for "Date input formats": https://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html
I don't have a precise answer. I do know that free-form datetime parsing is hard. Playing around with date:
$ date -d"2015-07-24 11:29:00 - 1 day" "+%F %T"
2015-07-25 08:29:00
$ date -d"2015-07-24 - 1 day" "+%F %T"
2015-07-23 00:00:00
$ date -d"11:29:00 - 1 day" "+%F %T"
2015-07-25 08:29:00
$ date -d"2015-07-24 - 1 day 11:29:00" "+%F %T"
2015-07-23 11:29:00
So, subtracting a day from a date seems to work, but subtracting a day from a time is problematic.
Related
I was studying the command find, and I saw:
-cmin n
File's status was last changed n minutes ago.
-ctime n
File's status was last changed n*24 hours ago.
but when n is zero, comes the difference, is there a sound explanation for this behavior?
$ date
Thu 23 Jul 2020 02:16:17 PM CST
$
$ touch a
$ find -name a -cmin 0
$ find -name a -cmin 1
./a
$ find -name a -ctime 0
./a
$ find -name a -ctime 1
$
$ date
Thu 23 Jul 2020 02:16:18 PM CST
$
Is there any command to get Timestamp as 00:00:00
Basically i need Current Date with Time stamp as 00:00:00 in unix
I tried
$(date '+%Y-%m-%d' 00:00:00)
But i need actual command in unix for it instead of appending 0
If you want a string in the format 2020-02-05 00:00:00 then how about just using echo to print the date output of the date command and then appending the time?
Like
echo "$(date '+%Y-%m-%d') 00:00:00"
This is rather esoteric. But I have a test which tests the string of a formatted timestamp, so it's bugging me.
date's man page indicates that
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
However, on Fedora 20:
$ date
Mon 27 Oct 22:44:22 AEDT 2014
$ date '+%p %P'
pm pm
$ TZ=Europe/Madrid date '+%p %P'
pm pm
The %p is not uppercase as it should be.
On Ubuntu 14.04 the behaviour is correct:
$ date
Mon Oct 27 12:20:08 CET 2014
$ date '+%p %P'
PM pm
$ TZ=Australia/Melbourne date '+%p %P'
PM pm
They both have the same version (8.21). Any suggestions on where to look next?
My colleagues managed to track it down to the language setting:
$ LANG=en_AU.UTF-8 date '+%p %P'
AM am
$ LANG=en_GB.UTF-8 date '+%p %P'
am am
Now to figure out where to file a bug report...
I have the problem illustrated in the following code snippet:
$ echo "2014-10-26 23:24:38.3123123" | date -d -
Sun Oct 26 00:00:00 EDT 2014
$ date -d "2014-10-26 23:24:38.3123123"
Sun Oct 26 23:24:38 EDT 2014
As you can see, the hour/min/seconds information is not picked up when I pipe in data with echo, but it is picked up when I use it as a command line argument. I am sure that there is something dumb I am not noticing, but if anyone can enlighten me on what that is it would be much appreciated!
When you write:
$ echo "2014-10-26 23:24:38.3123123" | date -d -
The space character between 2014-10-26 and 23:24:38.3123123 is treated like an argument separator and the date command takes the date string as two different arguments.
You can simply escape this space character:
$ echo "2014-10-26\ 23:24:38.3123123" | date -d -
and it works;
Sun Oct 26 23:24:38 EDT 2014
we are using hp-ux servers
we need to get 2 hours ago datetime value in bash shell script ?
how can i do that any experiences ?
date -d -2hours; date --version
Thu Apr 14 02:38:08 CEST 2011
date (GNU coreutils) 7.4
I'm not a shell script expert, but you may want to check this site at unix.com. They provide this example for subtracting from dates:
# subtract from any date
date --date "$dte 3 days 5 hours 10 sec ago"
date --date "$dte -3 days -5 hours -10 sec"