Command to get milliseconds by static time given - bash

I found command like this $(date +%s%3N) to convert current time to milliseconds, but example I have time like this 2018-25-08 12:00 PM. How to convert it to millisecond in bash?

Finally, I found this command to convert static date to milliseconds
date -d 'date -d '08/23/2018 23:59:59' +"%s%3N"

Related

How to get Date in java.util.Date instead of Unix Timestamp in bash script?

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.

remove millisecond from echo time bash

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}"

How to convert a date in yyyy-mm-dd hh:mm:ss to unix epoch time

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)}'

Bash: How to pass a variable to a command

I need to substract 10 minutes from a given time.
As I understand from previous answers, there are solutions only for given constant input, for example:
date --date '20140202 11:45:30 UTC -10 min'
But in my script, a given date is read from file and I have it as argument, for example:
myDate="20140202 11:45:30"
How can I pass this argument (myDate)to the command above?
myDate="20140202 11:45:30"
date --date "${myDate} UTC -10 min"

How to get 10 days back date by giving one date as parameter to the shell script

If i give the date "20130828"(not a current date) in YYYYMMDD format, how can i get 10 days back date using shell script i.e. 20130818
Thanks in advance!
Try
date +%Y%m%d --date="20130818 -10 day"
or even
date +%Y%m%d --date="20130818 10 days ago"
+%Y%m%d is the format of your date (YYYYmmdd), and through --date you can provide a string (in a very human readable format) to specify when you want this date.
The following:
date --date="20130828 - 10 days" +"%Y%m%d"
Outputs
20130818
if you have gnu date, you can just give 10 days ago to date's -d option:
kent$ date -d'10 days ago 20130828' +%Y%m%d
20130818

Resources