I have date format like 2016-02-26 14:12:36., I have to convert it into unix epoch time. I have the option to convert current time to epoch time using command : date +%s, however how to convert a specific date to epoch unix time
On the command line you convert the date provided to the UNIX epoch time like this:
date -j -f "%Y-%m-%d %H:%M:%S" "2016-02-26 14:12:36" "+%s"
This is on os x. The manual for date have some examples and strftime have all the formatting needed.
You can get seconds since the epoch from date like this ,
$ SECFROMEPOCH=`date --date="2016-02-26 14:12:36" +%s`
And then you can check the value that date gave back to you like this,
$ echo $SECFROMEPOCH | awk '{print strftime("%c",$1)}'
Related
I have an .sh script on my Linux server.
I need the date in milliseconds in Java, but everything I find on the net is giving me Unix Timestamp.
Like this: date=$(date -d 'today 00:00:00' "+%s")
I need java milliseconds, like here: https://www.fileformat.info/tip/java/date2millis.htm.
How do I get that easily without writing a long java code?
There has to be an easy solution.
It's just unix epoch in milliseconds instead of seconds, so simply replace +%s with +%s%3N.
epoch_milli=$( date -d 'today 00:00:00' +%s%3N )
$ date +%s; date +%s%3N
1666798614
1666798614357
Conversion using your site
Assuming your date supports this format
%N nanoseconds (000000000..999999999)
so you can do something like
date +%s.%N
to get nanoseconds, then you can do the math if you want millies.
1633036680022 , This is epoch result i got from elasticsearch.
if i tried to convert this epcho to human-readable date,
So i used epochconverter
And i used bash command to convert this in my terminal,
$ date -d #1633036680022
Tuesday 15 November 53718 05:30:22 PM IST
This output from terminal say the Year 53718, because the epoch '1633036680022' is in milliseconds.
All i want is ,epoch in seconds.
You can divide by 1000 and convert to timestamp.
date -d #"$(echo "1633036680022/1000" | bc)"
Strip milliseconds with bash (output only first 10 digits):
x="1633036680022"
date -d "#${x:0:10}"
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.
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'
What is the most efficient way to get a unix timestamp for a given ISO 8601 date and vice versa?
There are several third party websites to do this. However I am looking for a simple command that can be executed at the Linux prompt
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.