Am I doing something wrong? I can't believe date -d does not accept its own output when I use the ISO 8601 option with seconds. I either have to remove the 'T' or the timezone to get it to work.
> date -Iseconds
2014-01-14T11:07:57-0800
> date -d "2014-01-14T11:07:57-0800"
date: invalid date `2014-01-14T11:07:57-0800'
> date -d "2014-01-14 11:07:57-0800" # remove the 'T'
Tue Jan 14 11:07:57 PST 2014
> date -d "2014-01-14T11:07:57" # remove the timezone
Mon Jan 13 20:07:57 PST 2014
My current solution is to use date +"%Y-%m-%d %T %z" instead of date -Iseconds but I just thought that was strange.
Perhaps your locale is interfering with the date parsing?
Try specifying the time locale:
$ LC_TIME=C date -d "2014-01-14T11:07:57-0800"
Tue Jan 14 11:07:57 PST 2014
This works for me with GNU date 8.13.
GNU coreutils have only supported ISO 8601 dates as input since version 8.13 (released on 2011-09-08).
Thanks to #John1024 for the reference to
https://unix.stackexchange.com/questions/107750/how-to-parse-iso8601-dates-with-linux-date-command
Related
I know how to format a date in bash using the date command
date -d 20160304 +%Y%m%d
for example. But now I want to pass a date and time and return the hours and minutes. I know the output format I need is +%H%M, but I don't know how to format the date string and it is not in the man pages.
For example if I try any of these:
date -d 201801010500 +%H%M
date -d 20180101_0500 +%H%M
date -d 2018-01-01_0500 +%H%M
date -d 2018:01:01-05:00 +%H%M
I get an "invalid date" error. When I search google I always find answer referring to the output format, not the input format...
GNU date accepts these format, among others I'm sure
$ date -d '2018-02-16 12:34'
Fri Feb 16 12:34:00 EST 2018
$ date -d '2018-02-16T12:34:56'
Fri Feb 16 12:34:56 EST 2018
$ date -d '2018-02-16T12:34:56Z'
Fri Feb 16 07:34:56 EST 2018
In general, can't go wrong with ISO8601 time formats
I'm in Canada/Eastern time zone
The date utility is pretty impressive in making sense of different arguments for the -d option.
Here is just one example:
$ date -d "20180101 05:00:00"
Mon Jan 1 05:00:00 +07 2018
Note +07 is the local timezone.
Totally new to BASH. Apologies in advance.
Problem
I'd like to add X days to a specific date.
Code
I figured out that date in BASH retrieves the current date.
I also figured out that I can add X days to the current date in the following way,
expiration_date=$ date -v +1d
which gives,
Tue Sep 26 20:28:13 CEST 2017
which is indeed the date of writing plus X=1 days.
Question
In stead of date in the command line above, I'd like to insert a particular date to which X days will be added, e.g. 20/09/2017.
Don't care about the format of the particular date.
In other words: How do I make the following work,
expiration_date=$ '20/09/2017' -v +1d
Tried this answer, but doesn't do what I want.
Edit: Did not know things are different for OSX.
You can do this way:
dt='2017-09-20'
date -d "$dt +1 day"
Thu Sep 21 00:00:00 EDT 2017
date -d "$dt +2 day"
Fri Sep 22 00:00:00 EDT 2017
It seems OP is using OSX. You can use date addition this way:
s='20/09/2017'
date -j -v +1d -f "%d/%m/%Y" "$s"
Thu Sep 21 14:49:51 EDT 2017
You can do something like this:
date -d "Sun Sep 6 02:00:00 IST 2012+10 days"
time_var="6/23/2016 3:20:00 AM"
(this is in EDT)
We need to get unix timestamp for this variable after converting its value to GMT.
Just use the -u flag while passing the date with -d:
$ time_var="6/23/2016 3:20:00 AM"
$ date -d"$time_var EDT" -u
Thu Jun 23 07:20:00 UTC 2016
Note I also appended EDT to your date.
From man date:
-d, --date=STRING
display time described by STRING, not 'now'
-u, --utc, --universal
print or set Coordinated Universal Time
I am trying to convert a UTC time to GMT time in my small script, but it doesn't work:
TimestampUTC=$(date +"%s")
echo $TimestampUTC
dates=$(date -d #$TimestampUTC)
echo $dates
## 2 hours difference between UTC and GMT
Hours2=120
TimestampGMT=$((TimestampUTC - Hours2))
echo $TimestampGMT
diff=$((TimestampUTC - TimestampGMT))
echo $diff
dateGMT=$(date -d #$TimestampGMT)
echo $dateGMT
The displayed result for $dateGMT is the same as $dates.
Thanks in advance.
error in script.
Unix timestaps are given in seconds.
Hours2=120 means 120 seconds.
So your 2 timestaps are diverging by 2 minutes, not 2 hours.
This code is correct:
Hours2=7200
Also you claim having 2 hours between GMT and UTC, I'm sure you mean CET (central european time)
Note: there is nothing like a CET timestamp. It's just the normal unix timestamp displayed with a timezone offset. So independently of world location, the unix timestamp is always, worldwide, the same at the same instant.
You can replace all your code by just this
# get the timestamp 2 hours in the future from now
date2h=$(date -d "2 hours" +%s)
Which gives you the unix timestamp from the future. It is NOT the current timestamp in CET. The current CET timestamp is always the same as UTC.
How to get the time from UTC and CET? Set the environment variable TZ before the command.
$ TZ=UTC date
Mon Aug 17 11:44:05 UTC 2015
$ TZ=CET date
Mon Aug 17 13:44:05 CEST 2015
$ TZ=GMT date
Mon Aug 17 11:44:05 GMT 2015
but the timestap is always the same
$ TZ=UTC date +%s
1439812072
$ TZ=CET date +%s
1439812072
$ TZ=GMT date +%s
1439812072
GMT and UTC do not differ by 2 hours. In fact they don't differ at all. So displaying the dates of GMT and UTC will always show exactly the same number.
Also I don't know bash but I find it hard to believe that 2 hours is represented by 120 minutes. Normally when doing math with dates milliseconds are used.
In your favourite terminal use the following sequence
export TZ=GMT; date
date_format='+%d %B %Y %H:%M'
datatest="2021-11-21 12:00:00 UTC"
echo $(date -d "$datatest" "$date_format")
datatest="2021-11-21 12:00:00 CET"
echo $(date -d "$datatest" "$date_format")
datatest="2021-11-21 12:00:00 GMT"
echo $(date -d "$datatest" "$date_format")
Out:
21 November 2021 13:00
21 November 2021 12:00
21 November 2021 13:00
How can I convert UNIX time to date format?
Smth like
$> date ???? 1300000000
Mar 13 2011 07:06:40 GMT
Your date command might understand the # prefix. Try:
$ date -d #1300000000
Sun Mar 13 08:06:40 CET 2011
If -d doesn't work for you, thats probably because it only works like that for GNU date.
For BSD, OSX, etc. one would ordinarily use:
date -r 1300000000