I have a human readable time as
08-18-2016 09:18:25
I want it to be converted into epoch time using shell script.
I tried with date "+%s" but I am getting the error
date: invalid date `08-18-2016 09:32:42'
The canonical way to convert a datetime into epoch is to use:
date "+%s" # for this moment's date
date -d" some date" "+%s" # for a specific date
However, in this case the format is not valid:
$ date -d"08 18 2016 09:18:25" "+%s"
date: invalid date ‘08 18 2016 09:18:25’
You need, then, to massage the string a bit before passing it to date -d.
This converts the two first spaces into slashes:
$ sed 's# #/#;s# #/#' <<< "08 18 2016 09:18:25"
08/18/2016 09:18:25
So this works:
$ date -d"$(sed 's# #/#;s# #/#' <<< "08 18 2016 09:18:25")" "+%s"
1471504705
Or using variables:
$ nice_date=$(sed 's# #/#;s# #/#' <<< "08 18 2016 09:18:25")
$ date -d"$nice_date" "+%s"
1471504705
Thanks for the explanation fedorqui. But 1471511905 is the epoch time
for 08 18 2016 09:18:25, not 1471504705. – Mohit Rane
date -u … will print Coordinated Universal Time.
Related
I have a date in format 'YYYYMMDDHHMMSS' and I need to convert it to Unix timestamp.
I tried to date -d '20140826225834' but I get 'invalid date' error. I asume that I would have to convert what I have ( 20140826225834 ) to accepted date and then convert it to timestamp?
Edit: I have sed this date from 2014-08-21_23.03.07 - maybe it would be easier to convert this date type
You should probably change the format of the date you get, so that date can handle it. I change it to a YYYY/MM/DD HH:MM:SS format with sed.
$ date -d"$(sed -r 's#(.{4})(.{2})(.{2})(.{2})(.{2})#\1/\2/\3 \4:\5:#' <<< "20140826225834")" "+%s"
1409086714
By pieces:
$ sed -r 's#(.{4})(.{2})(.{2})(.{2})(.{2})#\1/\2/\3 \4:\5:#' <<< "20140826225834"
2014/08/26 22:58:34
$ date -d"2014/08/26 22:58:34"
Tue Aug 26 22:58:34 CEST 2014
$ date -d"2014/08/26 22:58:34" "+%s"
1409086714
You could use PHP, since PHP's strtotime() can parse your input format:
#!/bin/bash
input="20140826225834"
output=$(php -r 'echo strtotime("'"$input"'");')
echo "$output" # 1409086714
Say I have the following format outputted for my date:
date --utc +%d.%m.%Y,\ %H:%M\ UTC
# Outputs: 12.06.2014, 09:03 UTC
How can I display the outputted date above, in another date call, in another format? I tried:
date --utc --date="12.06.2014, 09:03 UTC" +%d.%m.%Y,\ %H:%M\ UTC
but with no success (it says invalid date).
I am primarily trying to do this in order to be able to tell from an outputted date how many hours have passed (or days, or whatever time measuring unit).
Here is what the man date page says about format for the --date option:
The --date=STRING is a mostly free format human readable date string such as
"Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next
Thursday". A date string may contain items indicating calendar date, time of day,
time zone, day of week, relative time, relative date, and numbers. An empty
string indicates the beginning of the day. The date string format is more
complex than is easily documented here but is fully described in the info
documentation.
Hence you can use, for example:
date --date "2014-06-12 09:03 UTC" --utc +%d.%m.%Y,\ %H:%M\ UTC
# Output: 12.06.2014, 09:03 UTC
to get what you desire.
You could get this second form easily from your first output with a sed line as follows:
sed 's/\([0-9]\{2\}\)\.\([0-9]\{2\}\)\.\([0-9]\{4\}\), \(.*\)/\3-\2-\1 \4/'
<<< '12.06.2014, 09:03 UTC'
# Output: 2014-06-12 09:03 UTC
Note that it would probably be faster to output date at ISO 8601 format in the first time for reuse, e.g. with:
date --utc +%F\ %H:%M\ UTC
# Output: 2014-06-12 10:12 UTC
I think you cannot specify input format, so you'll have to change it with another command like this:
date --utc --date="$(echo "12.06.2014, 09:03 UTC" | sed -r 's/(..).(..).(....), (..):(..) UTC/\3-\2-\1 \4:\5 UTC/')"
Also if you want to make arithmethic on this you could use +%s:
DATE1=$(date "+%s" --date="$(echo "12.06.2014, 09:03 UTC" | sed -r 's/(..).(..).(....), (..):(..) UTC/\3-\2-\1 \4:\5 UTC/')")
DATE2=$(date "+%s" --date="$(echo "17.06.2014, 08:30 UTC" | sed -r 's/(..).(..).(....), (..):(..) UTC/\3-\2-\1 \4:\5 UTC/')")
DIFF_IN_SECONDS=$(($DATE2-$DATE1))
DIFF_IN_RAW_DAYS=$(( ($DATE2-$DATE1)/86400 ))
DIFF_IN_DATES=$(( (($DATE2/86400) - ($DATE1/86400)) ))
I am getting date as an input parameter which is IST from some source but I have to convert it into UTC format i.e., I have to substract 5 hours 30 minutes to the date which I am receiving.
echo "Date format DD/MM/YYYY HH:SS"
read $input
I can do this writing small function which will grep HH and SS from the $input and then add which checking conditions which is tedious task, instead I want to know is there any way to do this ?
I found date command option which works like this:
date -d "15/05/2014 10:12 -5 hours -30 minutes"
not working
date -d "15/05/2014 10:12 5 hours ago 30 minutes ago" also not working.
Assuming you are getting your IST date in $date variable you can do this to get GMT date:
date="15/05/2014 10:15"
As a 1st step convert your dd/mm/yyyy date to mm/dd/yyyy:
date=$(awk 'BEGIN{FS=OFS="/"} {print $2, $1, $3}' <<< "$date")
Convert to GMT:
( export TZ='GMT' && date -d 'TZ="Asia/Kolkata" '"$date" )
Thu May 15 04:45:00 GMT 2014
To get EPOCH GMT value:
( export TZ='GMT' && date -d 'TZ="Asia/Kolkata" '"$date" '+%s' )
1400129100
I need to somehow use the date command in bash or another utility to print out the date and time, 5 minutes before and 5 minutes after a given value.
For example:
input:
Thu Dec 19 14:10
output:
Thu Dec 19 14:05 Thu Dec 19 14:10 Thu Dec 19 14:15
I see that the date command can be used to do this on the current date/time, can it be used with a passed value, i.e. not the current date/time?
You can achieve this, for the current time, by typing.
$ date --date='5 minutes ago'; date; date --date='5 minutes'
Qui Dez 19 16:09:17 BRST 2013
Qui Dez 19 16:14:17 BRST 2013
Qui Dez 19 16:19:17 BRST 2013
To use a specific date (ex 1978/01/10).
$ date --date='1978-01-10 + 5 minutes'
Ter Jan 10 00:05:00 BRT 1978
With GNU date, you can do a simple form of date/time arithmetic with the argument to the --date option:
$ date --date 'now + 5 minutes'
With BSD date (at least, the version that ships with Mac OS X), the -v option allows you to do something similar:
$ date -v +5M
$ date -v -5M
If you're using bash under linux, you can use the -d parameter to perform date manipulation on an existing date:
Get the EPOCH time for the date in question:
EPOCH=$(date -d 'Thu Dec 19 14:10' '+%s')
This gives you the time, in seconds, since the EPOCH (typically 01/01/1970)
Now you can use simple math to subtract or add 5 minutes (in seconds) to the EPOCH time
NEW_EPOCH=$(($EPOCH - 300))
obviously, there are 300 seconds in 5 minutes
Now convert this NEW_EPOCH back into a human readable date
NEW_DATE=$(date -d "1970-01-01 ${NEW_EPOCH} sec")
NOTE that this only works on unix systems which support the date -d option (i.e. Linux)
If you want to do this for the current time +/-5 minutes and you use Bash 4.2 or newer, you can do it without external tools:
$ printf -v now '%(%s)T'
$ echo "$now"
1516161094
$ f='%a %b %d %R'
$ printf "%($f)T %($f)T %($f)T\n" "$((now-300))" "$now" "$((now+300))"
Tue Jan 16 22:46 Tue Jan 16 22:51 Tue Jan 16 22:56
The %(datefmt)T formatting string of printf allows to print date-time strings. If the argument is skipped (like here) or is -1, the current time is used.
%s formats the time in seconds since the epoch, and -v now stores the output in now instead of printing it.
f is just a convenience variable so I don't have to repeat the formatting string for the output three times.
Since the argument for this usage of printf has to be in seconds since the epoch, you're stuck with external tools to convert an input string like Thu Dec 19 14:10 into that format and you'd replace
printf -v now '%(%s)T'
with, for example, any of
now=$(date '+%s' -d 'Thu Dec 19 14:10') # GNU date
now=$(date -j -f '%a %b %d %T' 'Thu Dec 19 14:10' '+%s') # macOS date
I am trying to convert month number to name, but it is giving output as current month instead of the date given in the variable.
KornShell (ksh) Code:
datep= 2013-10-22
echo $datep |printf "%(%B)T\n"
printf doesn't read from standard input, so it is assuming today's date as the default argument for the %T format; you need to provide the date as an argument instead.
printf "%(%B)T\n" "$datep"
Do it like this:
$ datep="2013-10-22"
$ date -d"$datep" "+%B"
October
As per man date,
-d, --date=STRING
display time described by STRING, not 'now'
So we get:
$ date -d"$datep"
Tue Oct 22 00:00:00 CEST 2013
Then you say you want the %B, that is, also from man date:
%B
locale's full month name (e.g., January)
So it is just a matter of using the format at the end of the string.
Other examples:
$ date -d"$datep" "+%Y" #year
2013
$ date -d"$datep" "+%F" #date
2013-10-22
$ date -d"$datep" "+%T" #time (if not given, gets the default)
00:00:00