Change Date Format in OSX Command - linux

I am storing a Curl command output in a variable which comes as a date. (any previous date)
date=`curl ...`
The output is:
03-22-2021
Now I want to change this date format to below
March 22, 2021
I am running below commands:
currDate="03-22-2021"
formattedDate=`date -d "${currDate}" +%B %d, %Y`
echo $formattedDate
But I am getting this error
date: illegal time format
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
is it possible to use such a date format? How to do it?

I have resolved my above question as per below for OSX
currDate="03-22-2021"
formattedDate=`date -jf "%m-%d-%Y" "${currDate}" +"%B %d, %Y"`
echo $formattedDate
Output is:-
March 22, 2021

is it possible to use such a date format?
No.
How to do it?
Convert the format to something GNU date can eat. Then pass it.
I typically look at man date:
DATE STRING
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". [...]
I recommend to use YYYY-MM-DD hh-mm-ss format and add also timezone information.
tmp=03-22-2021
date -d "${tmp:6:4}-${tmp:0:2}-${tmp:3:2} 12:00:00 UTC" "+%B %d, %Y"
Do not use backticks ` . Use $(..) instead.
Check your scripts with http://shellcheck.net
The format argument to date has to be one argument, not +%B %d, %Y three.
For explanation of ${tmp:5:4} expansion see variable expansion section in bash manual.
For anything more complicated with dates in shell, I recommend dateutils package with strptime command.
in OSX Command
On OSX install GNU date or install dateutils. The BSD date available on OSX is not capable of reading user input.

Related

How to echo time in format '2022-12-08T15:25:30.165965Z' in Linux bash command?

Currently I am working on a bash script which has hardcoded the following date:
2022-12-08T15:25:30.165965Z
I would like to generate that date in the script and I would like it to be up to date. What I have tried so far is:
date '+%d-%m-%Y %H:%M:%S.%M'
which outputs:
19-12-2022 12:40:13.40
I was wondering if someone knows how I could add the T and Z letters in the time?
You may use:
date -u '+%d-%m-%YT%H:%M:%S.%NZ'
Here:
-u will print UTC date-time
T before %H will print literal T
%N prints nanosecond value
Z after %N will print literal Z
$ date -u '+%FT%T.%6NZ'
2022-12-19T15:04:34.238733Z
You need GNU date for nanoseconds though.
With just bash using the EPOCHREALTIME variable and printf's %(fmt)T directive.
( IFS=.; TZ=UTC printf '%(%FT%T)T.%sZ\n' $EPOCHREALTIME )
# => 2022-12-19T14:28:33.343999Z
I'm using a subshell so that changing IFS does not affect the rest of the script.

Shell script to convert a date string to specific timestamp format

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"

Convert date to specified format linux

I am creating the logs and pushing to logstash. The date format of the logstash server is of the below destination form. In the Linux server, I can use the date command to convert the local date/time to log file. How to use the date command to get the below format in the Linux server. What is the format of the below command? I can set the format as TZ='CEST' date +"%field". I tried to set few format specifies but could not get the string 'th' in the date of the month. Also I don't understand the last field '221' in the date. Any help ?
destination server format: August 4th 2017, 22:30:45.221
try this code:
#!/bin/sh
DaySuffix() {
case `date +%d` in
1|21|31) echo "st";;
2|22) echo "nd";;
3|23) echo "rd";;
*) echo "th";;
esac
}
date "+%B %-d`DaySuffix` %Y, %T.%3N"
result for now: August 4th 2017, 12:04:49.750
help source: Formatting the date in unix to include suffix on day (st, nd, rd and th)

BASH date command: can't re-combine date and time

Linux v2.4/Bash v3.2/GNU utils/date command version 5.0
I'm struggling with the date command. In a BASH application, the user can set date and time separately, resulting in separate variables for date and time. Further on, these variables are re-combined but this appears not be palatable for the date command: I get a different date back. Time is the same, however. Testing code:
#!/bin/bash
dnow1="$(date)"
echo "1 $dnow1" # --> Sat Sep 14 16:31:48 EDT 2013
#split date and time
dldate="$(date -d "$dnow1" +"%d-%m-%Y")"
echo "2 $dldate" # --> 14-09-2013
dltime="$(date -d "$dnow1" +"%H:%M:%S")"
echo "3 $dltime" # --> 16:31:48
#try to re-combine date and time
string="${dldate} ${dltime}"
echo "4 $string" # --> 14-09-2013 16:31:48
dnow2="$(date -d "$string")"
echo "5 $dnow2" # --> Thu Mar 5 16:31:48 EST 2020
I must be missing something here. Can anyone enlighten me? Thanks!
Note:
I'm working an original XBOX that has few/low resources so there's no room for other solutions like Python. I'm a 'bashist' anyway so it must be BASH!
Edit: corrected time format. Thanks Mat.
As to "$(....)" I have made it a habit to double quote wherever possible.
When getting your date use this format instead:
#split date and time
dldate="$(date -d "$dnow1" +"%Y-%m-%d")"
From GNU date manual
The output of the date command is not always acceptable as a date string, not only because of the language problem, but also because there is no standard meaning for time zone items like ‘IST’. When using date to generate a date string intended to be parsed later, specify a date format that is independent of language and that does not use time zone items other than ‘UTC’ and ‘Z’.
First of all using -d won't work in 14-09-2013 fashion, you can easily set date and time with one command and put it into variable. eg, just try this below on shell and then you can put into shell script.
date --date="Feb 2 2014 13:12:10"
Sun Feb 2 13:12:10 PST 2014

Get current time in seconds since the Epoch on Linux, Bash

I need something simple like date, but in seconds since 1970 instead of the current date, hours, minutes, and seconds.
date doesn't seem to offer that option. Is there an easy way?
This should work:
date +%s
Just to add.
Get the seconds since epoch(Jan 1 1970) for any given date(e.g Oct 21 1973).
date -d "Oct 21 1973" +%s
Convert the number of seconds back to date
date --date #120024000
The command date is pretty versatile. Another cool thing you can do with date(shamelessly copied from date --help).
Show the local time for 9AM next Friday on the west coast of the US
date --date='TZ="America/Los_Angeles" 09:00 next Fri'
Better yet, take some time to read the man page
http://man7.org/linux/man-pages/man1/date.1.html
Pure bash solution
Since bash 5.0 (released on 7 Jan 2019) you can use the built-in variable EPOCHSECONDS.
$ echo $EPOCHSECONDS
1547624774
There is also EPOCHREALTIME which includes fractions of seconds.
$ echo $EPOCHREALTIME
1547624774.371210
EPOCHREALTIME can be converted to micro-seconds (μs) by removing the decimal point. This might be of interest when using bash's built-in arithmetic (( expression )) which can only handle integers.
$ echo ${EPOCHREALTIME/./}
1547624774371210
In all examples from above the printed time values are equal for better readability. In reality the time values would differ since each command takes a small amount of time to be executed.
So far, all the answers use the external program date.
Since Bash 4.2, printf has a new modifier %(dateformat)T that, when used with argument -1 outputs the current date with format given by dateformat, handled by strftime(3) (man 3 strftime for informations about the formats).
So, for a pure Bash solution:
printf '%(%s)T\n' -1
or if you need to store the result in a variable var:
printf -v var '%(%s)T' -1
No external programs and no subshells!
Since Bash 4.3, it's even possible to not specify the -1:
printf -v var '%(%s)T'
(but it might be wiser to always give the argument -1 nonetheless).
If you use -2 as argument instead of -1, Bash will use the time the shell was started instead of the current date. This can be used to compute elapsed times
$ printf -v beg '%(%s)T\n' -2
$ printf -v now '%(%s)T\n' -1
$ echo beg=$beg now=$now elapsed=$((now-beg))
beg=1583949610 now=1583953032 elapsed=3422
With most Awk implementations:
awk 'BEGIN {srand(); print srand()}'
This is an extension to what #pellucide has done, but for Macs:
To determine the number of seconds since epoch (Jan 1 1970) for any given date (e.g. Oct 21 1973)
$ date -j -f "%b %d %Y %T" "Oct 21 1973 00:00:00" "+%s"
120034800
Please note, that for completeness, I have added the time part to the format. The reason being is that date will take whatever date part you gave it and add the current time to the value provided. For example, if you execute the above command at 4:19PM, without the '00:00:00' part, it will add the time automatically. Such that "Oct 21 1973" will be parsed as "Oct 21 1973 16:19:00". That may not be what you want.
To convert your timestamp back to a date:
$ date -j -r 120034800
Sun Oct 21 00:00:00 PDT 1973
Apple's man page for the date implementation:
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/date.1.html
use this bash script (my ~/bin/epoch):
#!/bin/bash
# get seconds since epoch
test "x$1" == x && date +%s && exit 0
# or convert epoch seconds to date format (see "man date" for options)
EPOCH="$1"
shift
date -d #"$EPOCH" "$#"

Resources