Bash convert date to timestamp - bash

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

Related

How to add or subtract from date with format DDMMYYYY in bash?

This is working
date -d '2019-12-13 -5 day' +%d%m%Y
However, the following is throwing an error to me
date -d "13122019 -5 day" +%d%m%Y
From man date
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". [...] The date string format is more complex than is easily documented here but is fully described in the info documentation.
tl;dr: Seems like you cannot specify the format of the input. Use one of the known formats, for instance
date -d '2019-12-13 -5 day' +%d%m%Y
To automatically convert a date from DDMMYYYY format to YYYY-MM-DD you can use sed ...
date -d "$(sed -E 's/(..)(..)(.*)/\3-\2-\1/' <<< 13122019) -5 day" +%d%m%Y
... or bash ...
d=13122019
date -d "${d:4}-${d:2:2}-${d:0:2} -5 day" +%d%m%Y

Convert date string to a different format in bash

Getting the date in bash works something like this:
now=$(date)
echo "Current date: $now"
It returns:
Current date: Mon Nov 28 11:34:55 NZDT 2016
I need to store the date in a variable where the date is in a format like this:
20161128
How can I convert the date format into the format I need?
You use any format with +%
date +%Y%m%d
Output
20161127

Convert human readable time into EPOCH using shell script

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.

converting month number to date using KornShell

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

Convert Common Log Format (NCSA) to timestamp in bash/shell?

Is there a simple way to convert Common Log Format (NCSA) to timestamp ?
I found only C++ decision and some perl decisions. And wonder why i can't use standart unix function like date.
> date +"%d/%m/%Y:%H:%M:%S" -d "17/Oct/2013:16:52:28" +"%s"
"17/Oct/2013:16:52:28" -> 1382014348
For example:
Date in iso8601 to timestamp
> date -d "2013-10-17T18:07:39+04:00" +"%S"
1382018859
You can try this ( if you surely want to use date) ,
date -d "$(sed -e 's#/#-#g; s#:# #' <<< '17/Oct/2013:16:52:28')" '+%s'

Resources