How to get time in specific format - Linux - linux

I need to write a simple script of in bash. How do i get the current time in Linux in the specific format of
HH:MM am or pm, eg 05:30 a.m.

Use date formatter
$date + "%I:%M %P"

Related

Converting Dates - What format is 20130606T083000.093660900? [duplicate]

This question already has an answer here:
How to convert string format date yyyymmddThhmmss.sss to date in Bash?
(1 answer)
Closed 6 months ago.
Bit of an issue with date formatting:
Looking to format this date format " 20130606T083000.093660900 "
2013 06 06 T 08 30 00. 093660900
YYYY MM DD T HH MM SS NANOSECS
Formatting this into Epoch (which I believe is the second time given below without decimals) time would be great, I don't have much experience working with date time and changing them.
My goal is to realistically subtract
20130606T083000.093660900 FROM 1370507400093660900
Any help with even telling me what format the first date is, as it is apparently NOT ISO 8601, thanks!
You can use sed to convert the input to something GNU date can parse, then use GNU date to convert it to the format of the other date. Then compare:
din='20130606T083000.093660900'
dcmp='1370507400093660900'
dstr=$(sed 's/\(..\)\(..\)T\(..\)\(..\)/-\1-\2T\3:\4:/' <<<"$din")
timestamp=$(TZ=UTC0 date +%s%N --date="$dstr")
diff=$(( dcmp - timestamp ))
# diff is now: 0
I have forced timezone to avoid localisation concerns.

Adding Hours to Date & Time in Linux

I am trying to add 5 hours to a date & time in Linux (Ubuntu 18.04.03) using the 'date' command. I get different results if I specify a date vs if I don't.
Example 1: I want to add 5 hours to a specific date:
[12:45:25 ~]$ date -d "2019-12-08 12:16:45 +5 hours"
Sun Dec 8 03:16:45 EST 2019
But the result is 9 hours BEFORE the specified date and time. I was expecting to have a date of 12/8 and time of 17:16:45.
Example 2: I add 5 hours to the system date:
[12:45:37 ~]$ date -d "+5 hours"
Sun Dec 8 17:46:02 EST 2019
This result is correct and as I expected.
I want to use this in a bash script and can't determine what I am doing incorrectly that I get these different results.
Any help will be appreciated.
After playing around a bit it seems like specifying the timezone with no spaces achieves what you're looking for:
date -d "2019-12-08 11:16:45EST+5hours"
This command outputs Sun Dec 8 16:16:45 EST 2019
Hope this helps!
GNU date very strangely parses the date, which makes it unreliable. The most reliable thing is to convert to seconds since epoch.
Convert the date to seconds.
Add 5 hours of seconds.
Convert the data to your favourite format.
date -d "#$(( $(date -d "2019-12-08 12:16:45" %s) + (5 * 60 * 60) ))"
date -d "2019-12-08 12:16:45 +5 hours"
I guess the +5 get's parsed as the timezone. So if you are in a timezone +5 and want EST, then you need to substact 5 hours for the timezone and 4 hours for current EST time. The hours get's ignored.
Add EST to the date & time string will give the correct answer.
I found it worthwhile to get the timezone and offset like so:
dateInfo=$(date +"%Z %z")
dateTZ=${dateInfo:0:3}
dateOffset="+"${dateInfo:5:2}
Then use them in the conversion code:
DATE=$(date -d "$CDATE1 $dateTZ $dateOffset hours" +%Y%m%d)
(where CDATE1 was parsed from a file's creation date as date & time)

Calendar months variables in BASH script

I am writing a BASH script that calls an API that presents metrics for specific time-frames. I plan to run the script on a cron job on the 1st of each month, the API call needs to contain the start and end time and be in a epoch format with milliseconds. Milliseconds should be ok to set to 000 as it doesnt need to be that specific but the API requires it.
How can I code the script to look at the current human readable time then look at the exact same time exactly 1 calendar month before, then convert both outputs to epoch, and then enter the epoch times in the curl command as a variable?
Example:
Script runs at 1am on July 1st, script then understands a full calendar month before was June 1st, converts both to epoch, places them into the curl command using variables.
I understand how to get a human readable date for 1 month ago, however I am unsure how best to convert these dates to epoch.
date --date="1 month ago" +"%d%m%Y"
To create a variable using date in epoch times with milliseconds:
ENDDATE=`date +%s%N | cut -b1-13`
STARTDATE=`date --date="1 month ago" +%s%N | cut -b1-13`
Use these variables in the cURL command.

Linux date objects behaving differently when hour/minute/second are specified

Why does adding one day work differently with the variables $d and $dt below?
Linux version: CentOS 6.8
If there is some kind of hidden timezone conversion going on with $dt but not with $d? If so, how do I suppress it?
d="2019-04-01"
dt="2019-04-01 01:00:00"
date --date="$d +1 days" +'%Y-%m-%d'
Output: 2019-04-02
date --date="$dt +1 days" +'%Y-%m-%d %H:%M:%S'
Output: 2019-04-01 17:00:00
Just so this question is answered (I couldn't find a duplicate) it was pointed out in the comments that the +1 in +1 days was being interpreted as a UTC offset appended to the date.
The solution is to terminate the date string with a time zone specifier such as Z or UTC so it's recognized as a complete date.
See info '(coreutils) date invocation' for more details on date specifications.

shell scripts to increment time in --rfc-3339 format

I want to increment time in -rfc-3339 format and have used following command :
`date --date="(date --rfc-3339=seconds) + 5 minutes"`
but after increment current time by 5 minutes, it shows the incremented system time in standard format :
Thu Feb 2 20:06:30 IST 2017.
I want the desired output in --rfc-3339 format i.e
2017-02-02 20:06:30+05:30.
Thanks in advance.
This is pretty simple. The --rfc-3339 flag tells the date command how to format its output, but you're putting it inside the specifier for the input date — what date to format.
In your command
date --date="(date --rfc-3339=seconds) + 5 minutes"
the --date="stuff" part tells the command which date you want to show (as opposed to the default of "right now"). It looks like you might be doing some sort of math with "take the current date and add five minutes", but actually the part in () is not valid input and is ignored. You could say date --date="(colorless green ideas sleep furiously) + 5 minutes", and you'll get the same results — or just date --date="+5 minutes".
But of course, that's in the standard output format. To get it in RFC 3339 format, simply add that flag, outside of the input date string:
date --date="+5 minutes" --rfc-3339=seconds
and there you go.

Resources