I have a date time string like this 17-Mar-2020.22:22 -0300
.I want to get the output as 2020-03-17 22:12
.
The timezone offset is causing an issue as without that I am able to convert
For example DATE=17-Mar-2020.22:22 date -jf "%d-%b-%Y.%H:%M" $DATE "+date \\"%Y-%m-%d %H:%M\\""
But this timezone offset is causing an issue.Can someone please help?
Because the input is in an unusual format, it needs to be first formatted to a format date
can eat. I use the format indicated in man page "2004-02-29 16:21:42"
or "Sun, 29 Feb 2004 16:21:42 -0800"
. GNU date allows for very flexible input format - don't trust it.
Below I used sed
to match the input and reformat it.
input='17-Mar-2020.22:22 -0300'
input2="$(sed 's/\([0-9]*\)-\([^-]*\)-\([^\.]*\)\.\([0-9]*\):\([0-9]*\)/\1 \2 \3 \4:\5:00/' <<<"$input")"
# input2="17 Mar 2020 22:22:00 -0300"
date --date="$input2" +"%Y-%m-%d %H:%M"
# 2020-03-18 02:22
date -jf
GNU date differs a lot from BSD date . They are completely different. BSD date supports the -j
and -f
options, GNU date does not.
There are dateutils and they include strptime
utility that allows to do what you wanted to do with date:
strptime -e -f "%Y-%m-%d %H:%M\n" -i "%d-%b-%Y.%H:%M %Z" "17-Mar-2020.22:22 -0300"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.