I need to convert this date format to epoch : 03/Apr/2016 14:22:59
the command
date -d "03/Apr/2016 14:22:59" +"%s"
will return :
date: invalid date ‘03/Apr/2016 14:22:59
Anyone can help me format it in a way it become recognizable by date -d ?
Thanks in advance.
Perl to the rescue:
perl -MTime::Piece -e 'print Time::Piece
->strptime("03/Apr/2016 14:22:59", "%d/%b/%Y %H:%M:%S")
->epoch'
Info page for date input formats can be shown with following command:
info date "Date input formats"
Unfortunately your date format is not supported by date. You can however convert your string into format that is supported for example like this:
date -d "$(echo '03/Apr/2016 14:22:59' | tr -s "/" "-")" +"%s"
To provide information about which kind of input strings can be used for date command I will write here a short summary:
Show date from epoch
date -d "#1513936964"
Words like today, tomorrow, month names (January), AM/PM, time zone names are supported
date -d "tomorrow 10:00:30PM"
date -d "03 April 2016 14:22:59"
Calendar formats
date -d "04/03/2016 14:22:59"
date -d "03-Apr-2016 14:22:59"
date -d "2016-04-03 14:22:59.00"
Timezones
date -d "PDT now"
Which day was Christmas last year?
date -d "$(date -d "2017-12-24") -1 year" +"%A"
Using Python:
python -c 'from time import strftime, strptime;print strftime("%s", strptime("03/Apr/2016 14:22:59", "%d/%b/%Y %H:%M:%S"))'
Related
I want the previous month & previous year in the format: "December2020".
But while executing the below Linux command I'm getting July2020.
echo `date -d "2021-01-08" '+%B' -d 'last month'``date -d '1 year ago' +%Y
The date hardcoded parameter is in format: YYYY-MM-DD (2021-01-08)
date accepts only one -d option. In your command date -d "2021-01-08" '+%B' -d 'last month' the first -d is ignored. Only the -d "last month" applies. Because of that, and since we have August right now, the output is July.
You probably wanted to use
date -d '2021-01-08 - 1 month' +%B%Y
which prints December2020.
If you really wanted to concat the previous month and previous year together, you could use
echo "$(date -d'2021-01-08 - 1 month' +%B)$(date -d'2021-01-08 - 1 year' +%Y)"
but that would give rather strange results:
2021-01-08 → December2020 # 1 month before input date
2021-04-30 → March2020 # 13 months before input date
For conversion of any given date into yyyymmdd, we can do like date -d " Tue Apr 9 16:19:48 IST 2019 " "+%Y%m%d". But how can we convert the list of dates into yyyymmdd format using for loop in bash?
for i in `cat create_time`;do date -d " $i " "+%Y%m%d"; done
create_time is the file contains date.
for i in `cat create_time`;do date -j -f " $i " "+%Y%m%d"; done
The result should be like this format 20190409 for all the given dates.
date -f create_time "+%Y%m%d" prints that format for every date line in the file.
I'm trying to display a date before and after a given date stored in a variable.
I can do that without a variable like so
thetgerwie:~ $ date -d '2019-09-09 1 day ago' +'%F' result: 2019-09-08 it works fine
but if I use a variable dte="2019-09-09"
thetgerwie:~ $ date -d '(echo "$dte") day ago' +'%F' result: 2019-03-04 I get the current date
Any idea how to fix that?
Thanks
You don't need to use echo to concatenate the $dte variable.
Using double quotes should suffice in this case:
dte='2019-09-09'
date -d "$dte 1 day ago" +'%F'
date command supports arithmetic operations
example:
➜ ~ date -d '2019-09-09 + 1 day' +'%F'
2019-09-10
➜ ~ date -d '2019-09-09 - 1 day' +'%F'
2019-09-08
UPDATE
please use double quotes
➜ ~ dte="2019-09-09"
➜ ~ echo $dte
2019-09-09
➜ ~ date -d "$dte day ago" +'%F'
2019-09-08
Here's a good resource:
https://linuxcommando.blogspot.com/2009/11/fun-with-date-arithmetic.html
I hope this is what you're looking for
Cheers!
The following command generates the date for the next day:
date -d "20150615 12:00 +1 day" +%Y%m%d
20150616
I would like to specify my own INPUT date format, such as:
2015_06_15
But the date command does not like this format and complains about invalid date:
date: invalid date '2015_06_15 12:00 +1 day'
Is it possible to use such a date format? And if so how could I do this.
A workaround:
x="2015_06_15"
date -d "${x//_/} 12:00 +1 day" +%Y%m%d
Output:
20150616
I have a string in the format "yyyymmdd". It is a string in bash and I want to get it converted into a date so that all other date functions can be used on it.
"20121212" string into "20121212" date with format "%Y%m%d".
This worked for me :
date -d '20121212 7 days'
date -d '12-DEC-2012 7 days'
date -d '2012-12-12 7 days'
date -d '2012-12-12 4:10:10PM 7 days'
date -d '2012-12-12 16:10:55 7 days'
then you can format output adding parameter '+%Y%m%d'
We can use date -d option
1) Change format to "%Y-%m-%d" format i.e 20121212 to 2012-12-12
date -d '20121212' +'%Y-%m-%d'
2)Get next or last day from a given date=20121212. Like get a date 7 days in past with specific format
date -d '20121212 -7 days' +'%Y-%m-%d'
3) If we are getting date in some variable say dat
dat2=$(date -d "$dat -1 days" +'%Y%m%d')
date only work with GNU date (usually comes with Linux)
for OS X, two choices:
change command (verified)
#!/bin/sh
#DATE=20090801204150
#date -jf "%Y%m%d%H%M%S" $DATE "+date \"%A,%_d %B %Y %H:%M:%S\""
date "Saturday, 1 August 2009 20:41:50"
http://www.unix.com/shell-programming-and-scripting/116310-date-conversion.html
Download the GNU Utilities from Coreutils - GNU core utilities (not verified yet)
http://www.unix.com/emergency-unix-and-linux-support/199565-convert-string-date-add-1-a.html
just use the -d option of the date command, e.g.
date -d '20121212' +'%Y %m'