Converting date with timezone in UNIX timestamp Shell/Bash
You don't need to call date
twice. Just call it once with TZ
set to the timezone you want for that variable.
timesydney=$(TZ=Australia/Sydney date -d "$dateYMD" +%s)
timenyc=$(TZ=US/Eastern date -d "$dateYMD" +%s)
How to convert timestamps to dates in Bash?
On systems with GNU Coreutils >= 5.3.0, e.g. Linux you can use:
date -d @1267619929
Commandline to convert ISO8601 time to unix timestamp
Convert ISO Date/Time to Unix timestamp
date -d 'date' +"%s"
Example
bash-# date -d 'Fri Dec 8 00:12:50 UTC 2017' +"%s"
bash-# 1512691970
man -a date
-d, --date=STRING
display time described by STRING, not 'now'
%s seconds since 1970-01-01 00:00:00 UTC
Convert Unix timestamp to ISO Date/Time
date -Iseconds -d @<unix timestamp>
Example
bash-# date -Iseconds -d @1512711426
bash-# 2017-12-07T21:37:06-0800
man -a date
-d, --date=STRING
display time described by STRING, not 'now'
-I[TIMESPEC], --iso-8601[=TIMESPEC]
output date/time in ISO 8601 format.
TIMESPEC='date' for date only (the default), 'hours',
'minutes', 'seconds', or 'ns' for date and time to the
indicated precision.
How to convert DATE to UNIX TIMESTAMP in shell script on MacOS
man date
on OSX has this example
date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
Which I think does what you want.
You can use this for a specific date
date -j -f "%a %b %d %T %Z %Y" "Tue Sep 28 19:35:15 EDT 2010" "+%s"
Or use whatever format you want.
Bash: get date and time from another time zone
You could use
TZ=America/New_York date
or if you want to do date arithmetic you could use
date -d "+5 hours"
Converting Date and time to UNIX date format
Convert string datetime to datetime obj, and then retrieve timestamp.
from datetime import datetime
t = "2021/12/04 11:10:00.000"
dt = datetime.strptime(t, "%Y/%m/%d %H:%M:%S.%f")
dt.timestamp()
Output:
1638637800.0
SH: how to convert the date Nov 26 23:59:00 2022 GMT to timestamp?
The version of the date command that I have on my system (YMMV) will show us what it is doing if we add the --debug
flag to our invocation...
$ date -d "Nov 26 23:59:00 2022 GMT" +"%s" --debug
date: parsed date part: (Y-M-D) 2020-11-26
date: parsed time part: 23:59:00
date: parsed number part: year: 2022
date: parsed zone part: UTC+00
date: input timezone: parsed date/time string (+00)
date: using specified time as starting value: '23:59:00'
date: starting date/time: '(Y-M-D) 2022-11-26 23:59:00 TZ=+00'
date: '(Y-M-D) 2022-11-26 23:59:00 TZ=+00' = 1669507140 epoch-seconds
date: timezone: system default
date: final: 1669507140.000000000 (epoch-seconds)
date: final: (Y-M-D) 2022-11-26 23:59:00 (UTC)
date: final: (Y-M-D) 2022-11-27 07:59:00 (UTC+08)
1669507140
The debug log shows us the %s
format will result in displaying seconds since epoch. Alternatively, nanosecond precision can be achieved using a modified format string (I added a .001
in the date string to prove it works):
$ date -d "Nov 26 23:59:00.001 2022 GMT" +"%s.%N" --debug
date: parsed date part: (Y-M-D) 2020-11-26
date: parsed time part: 23:59:00
date: parsed number part: year: 2022
date: parsed zone part: UTC+00
date: input timezone: parsed date/time string (+00)
date: using specified time as starting value: '23:59:00'
date: starting date/time: '(Y-M-D) 2022-11-26 23:59:00 TZ=+00'
date: '(Y-M-D) 2022-11-26 23:59:00 TZ=+00' = 1669507140 epoch-seconds
date: timezone: system default
date: final: 1669507140.001000000 (epoch-seconds)
date: final: (Y-M-D) 2022-11-26 23:59:00 (UTC)
date: final: (Y-M-D) 2022-11-27 07:59:00 (UTC+08)
1669507140.001000000
We can use a pipe to awk to round up to milliseconds for our final form (note I used .0005
in the date string here to show rounding):
$ date -d "Nov 26 23:59:00.0005 2022 GMT" +"%s.%N" | awk '{ printf("%d\n", int($0 * 1000) + (int($0 * 2000) % 2)) }'
1669507140001
One further note.... The following is the version of the date command I am using:
$ date --version
date (GNU coreutils) 8.30
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by David MacKenzie.
Related Topics
How to Dynamic Load The Library with Same Name But in Different Directory in Linux
Gradle 1.3: Build.Gradle Not Building Classes
Install Opencl(Amd Sdk Kit) on Linux Without Root Privilege
Linux Nasm Move a Value in Al Up to Ax
How to Add Multilanguage Support to a Yocto Build
Accidentally Deleted Symlink Libc.So.6 in Centos 6.4. How to Get Sudo Privilege to Re-Create It
Windows .Crl to .Pem for Nginx
How to Store Multiple Pats/Passwords for Use by Git
Oracle Query - Ora-01652: Unable to Extend Temp Segment But Only in Some Versions of Sql*Plus
Git - How to Remove Branch from Checkout Autocomplete
Windows <Sys/File.H> Equivalent
Perl System Calls When Running as Another User Using Sudo
Forcing a Context Switch from The Userland on Linux
Sending Realtime Signal from a Kernel Module to User Space Fails
Linux - Run Android Emulator on Nouveau Driver
Dynamically Pick The User Gui and UId Who's Running Docker at The Host from Entrypoint