How I can convert date to following format in Shell? - shell

I am passing a date as an argument and needed to convert it in given format in shell.
Pasting the sample code here
echo "20190101120001" | xargs date +"%Y%m%d%H%M%S" -u
And this should display output as
Tue Jan 01 12:00:01 UTC 2019

try this:
#!/bin/bash
str="20190101120001"
date -d "${str:0:8} ${str:8:2}:${str:10:2}:${str:12:2}" -u

Related

How I can format a date in shell

I want to convert a custom date format DD-%%%-YYYY to a standard one: YYYYMMDD
Possible values of %%% are:
Jan Fev Mar Avr Mai Jun Jui Aou Sep Oct Nov Dec
Assuming the input is a bash variable, how do I transform it to the standard format?
Example:
$ fr_date='09-Aou-2018'
$ # [transformation]
# sql_date should now contain 20180809
$ echo "$sql_date"
20180809
You can use the date utility.
fr_date='09-Aug-2018'
sql_date="$(date --date=$fr_date +%Y%m%d)"
echo $sql_date
20180809
Please also refer to the date man page for more information.
Additionally, date does not support custom locales, format must be locale independent. Try to store dates as simple Unix epoch.
Solution 1: Rewrite the french months into english, then use date to read and format it:
Pure bash:
tmp=${fr_date/Fev/Feb} tmp=${tmp/Avr/Apr} tmp=${tmp/Mai/May}
tmp=${tmp/Jui/Jul} tmp=${tmp/Aou/Aug}
sql_date=$(date +%Y%m%d -d "$tmp")
With sed:
tmp=$(sed 's/Fev/Feb;s/Avr/Apr;s/Mai/May;s/Jui/Jul;s/Aou/Aug' <<<"$fr_date")
sql_date=$(date +%Y%m%d -d "$tmp")
Solution 2: Assign to each month its corresponding number:
#!/bin/bash
# Requires bash 4 for associative arrays
declare -A month_map=(
[Jan]=01 [Fev]=02 [Mar]=03 [Avr]=04 [Mai]=05 [Jun]=06
[Jui]=07 [Aou]=08 [Sep]=09 [Oct]=10 [Nov]=11[Dec]=12
)
IFS=- read -r day month year <<<"$fr_date"
sql_date=$year${month_map[$month]}$day

How to pass hours and minutes to date command through -d option

I know how to format a date in bash using the date command
date -d 20160304 +%Y%m%d
for example. But now I want to pass a date and time and return the hours and minutes. I know the output format I need is +%H%M, but I don't know how to format the date string and it is not in the man pages.
For example if I try any of these:
date -d 201801010500 +%H%M
date -d 20180101_0500 +%H%M
date -d 2018-01-01_0500 +%H%M
date -d 2018:01:01-05:00 +%H%M
I get an "invalid date" error. When I search google I always find answer referring to the output format, not the input format...
GNU date accepts these format, among others I'm sure
$ date -d '2018-02-16 12:34'
Fri Feb 16 12:34:00 EST 2018
$ date -d '2018-02-16T12:34:56'
Fri Feb 16 12:34:56 EST 2018
$ date -d '2018-02-16T12:34:56Z'
Fri Feb 16 07:34:56 EST 2018
In general, can't go wrong with ISO8601 time formats
I'm in Canada/Eastern time zone
The date utility is pretty impressive in making sense of different arguments for the -d option.
Here is just one example:
$ date -d "20180101 05:00:00"
Mon Jan 1 05:00:00 +07 2018
Note +07 is the local timezone.

`Date` command formatting in Solaris Unix?

How to the below output using date formatting..!?
Date command output should look like:
Mon Oct 24 12:01:32 SGT 2016:
date +%a" "%b" "%d" "%T" "%Z" "%Y
This will provide the expected output..!

Convert date String to number on Solaris shell script gives No such file or directory

I have a date in the format "Thu Sep 22 3:50 2016", and I want to convert it to format: "2016-09-22"
I tried the following shell script, which works fine for 'date', but gives error for user specified string: (I am working on Solaris platform). Any inputs will be helpful.
Input:
#!/usr/bin/sh
mydate="Thu Sep 22 3:50 2016"
echo `date +"%Y-%m-%d"`
echo `$mydate +"%Y-%m-%d"`
Output
./testShell.sh
**2016-09-22**
./testShell.sh[6]: Thu: not found **[No such file or directory]**
Any pointers please?
Under Solaris 11, many GNU utilities are available under the /usr/gnu/bin directory so you just need to slightly modify your script to either use the full path the the GNU variant :
#!/bin/sh
mydate="Thu Sep 22 3:50 2016"
date +"%Y-%m-%d"
/usr/gnu/bin/date -d "$mydate" +"%Y-%m-%d"
or use the already existing symlink prefixed by g (for GNU):
gdate -d "$mydate" +"%Y-%m-%d"
or set your PATH to look at /usr/gnu/bin first and keep your script unchanged.
PATH=/usr/gnu/bin:$PATH
You can try something like this;
#!/bin/bash
mydate="Thu Sep 22 3:50 2016"
date +"%Y-%m-%d"
date -d "$mydate" "+%Y-%m-%d"

How to convert local date-time string to Unix timestamp (GMT)?

time_var="6/23/2016 3:20:00 AM"
(this is in EDT)
We need to get unix timestamp for this variable after converting its value to GMT.
Just use the -u flag while passing the date with -d:
$ time_var="6/23/2016 3:20:00 AM"
$ date -d"$time_var EDT" -u
Thu Jun 23 07:20:00 UTC 2016
Note I also appended EDT to your date.
From man date:
-d, --date=STRING
display time described by STRING, not 'now'
-u, --utc, --universal
print or set Coordinated Universal Time

Resources