Print the standard time - eg 04:52:06 PM - Bash - bash

I have the
$ date +"%c"
command
which displays the date and time, however I only wish to display the time in full non military, eg: 04:52:06 PM
I have tried
date +"%T"
However that returns the Military Date, not standard.

Try making it from "scratch" using all the options together. You can have as many in the format as you like.
]$ date +"%I:%M:%S %p"
05:26:22 PM

Your system might accept date +%r that is an "alias" for "%I:%M:%S %p"

I would display it in this way:
date +"%I:%M:%S %p"

Related

How to get last Week's date in bash SunOS

Here is my issue: I have a backup bash script that needs to access a folder with a date in its name for example : backup_01072022 .
I used date=`TZ=GMT+24 date +%d%m%Y` when i needed to access the backup folder of yesterday.
Now I want to access the backup folder of last week :
date=`TZ=GMT+168 date +%d%m%Y` , it doesn't work , it show today's date.
I read that TZ doesn't work for a value above +144.
Is there any other way of manipulating dates in SunOS 6.8 ?
Notes :
SunOS 6.8
version of the date util : 8.5
version of bash : 4.1.11(2)-release
This'll depend on the version of date on your system.
With GNU date (v 8.26):
$ TZ=GMT date '+%d%m%Y'
06072022 # today
$ TZ=GMT date '+%d%m%Y' -d 'last week'
29062022
$ TZ=GMT date '+%d%m%Y' -d '7 days ago'
29062022
NOTE: I'll leave it up to OP to determine if the explicit TZ setting should be adjusted (or used at all)
I seem to recall SunOS comes with Perl, so if you don't have a date that supports --date="...", you should be able to do:
date=$(perl -MPOSIX -e '
print POSIX::strftime "%d%m%Y", localtime time-(60*60*24*7)
')
Thanks to the helpful commments and answers I was able to make it work using :
/usr/gnu/bin/date -d "last week" '+%d%m%Y'
It turns out I was not using the GNU date util until I specified it explicitly, and that's neither --date nor -d was working for me.
I still can't figure out what date util I was using by default if not GNU date.

How to get the current date and time in specific format in Shell

I just want to get the system current time using below format in shell
Expected Format:
2019-02-14T08:08:12.300Z
I tried below piece of script but It returned something different.
CURRENTDATE=`date +"%Y-%m-%d %T"`
echo $CURRENTDATE
Output
2019-02-27 01:22:57
I just ran date command in my linux box and got below response back:
-bash-3.2$ date
Wed Feb 27 01:43:26 PST 2019
We are passing the above output as input JSON file. But this output is not accepted by our JSON. So I just want to pass the date and time format in the above specified format (Expected Format).
You may use:
dt=$(date '+%Y-%m-%dT%T.%zZ')
echo "$dt"
2019-02-27T04:35:56.-0500Z
Also note use of --iso-8601=seconds:
date --iso-8601=seconds
2019-02-27T04:37:29-05:00
You can also use this one like
currentDate=$(date +"%Y-%m-%dT%H:%M:%SZ")
echo $currentDate
OutPut :-
2022-07-12T18:05:27Z

Subtract 20 month from user provided date in yymm in unix

oldest_year_month_temp=201602
NUM_PART_RETAIN=20
oldest_year_month=`date --date="$(oldest_year_month_temp +%Y%m) - $NUM_PART_RETAIN month" "+%Y%m"`
Date is not coming as expected.
One easy way to do it would be to simply append a 01 to your input of yymm to provide a format date -d could read as the starting date, then simply subtract 20 months and output the resulting date in %y%m format. For example, if you provide the date 9910 (Oct. 1999), you can do:
$ date -d "991001 - 20 months" +%y%m
9802
Which returns Feb. 1998 (20 months earlier)
(note: the $ above just indicates a command by a normal user as opposed to # indicating a command by the super user (e.g. root))
Inside the $(...) there must be a command, e.g. $(date ...).
This should have been obvious from the error message you got, which was probably oldest_year_month_temp: no such command.
When reading from a variable, you must write a $ before its name.

Calculate 15 minutes ago in shell

I have a time-stamp like 7:00:00, which means 7am.
I would like to write a short command that returns 06:45:00, or simply 06:45, preferably using date command so that I can avoid long shell script. Do you have any elegant solution?
I'm also looking for a 24h format. For example, 12:00:00 - 15 minutes = 11:45:00.
With GNU date, use 7:00:00 AM - 15 minutes as d (--date) string :
% date -d '7:00:00 AM - 15 minutes' '+%H:%M'
06:45
+%H:%M sets the output format as HH:MM.
On BSD variants Date has a -v flag which can be used to take the current timestamp and display the result of a positive or negative adjustment.
This will subtract 15mins from the current timestamp:
date -v -15M

Subtracting time from file creation/modification date in OSX

I am trying to shift the dates of a series of files by 9 hours. I've reached as far as this:
for i in *.MOV; do touch -r "$i" -d "-9 hours" "$i"; done
This should work in recent systems, but the touch command in OSX seems to be a bit outdated and not to support the -d switch.
I'm using Snow Leopard. Any idea on the best option for doing this with a single line command? I don't want to create a script for this.
Ok, sorted it out. OSX comes with a gtouch command, that knows the -d switch. It's part of GNU coreutils. See the comments below for information regarding availability on specific MacOS versions.
For more information on using relative dates with the -d switch see the manual.
Looking at the Wikipedia Page for Touch, it appears you're accustomed to the GNU version of Touch. Which MacOS isn't using.
For what you want to do, look into the "SetFile" command, which gets installed with XCode tools. You have -d and -m options, which reset the Created and Modified dates & times respectively.
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/SetFile.1.html
Donno OS X, but it should be easy enough to
get curr time stamp on the file
convert it to seconds
subtract 9 hours (9*60*60 secs) from it
convert it back to the format accepted by touch's -t option
run touch command
All this of course can be done in a single for loop on command line.
Here are simple examples from WikiPedia showing back and forth conversion.
# To convert a specific time stamp to Unix epoch time (seconds since 1970-01-01):
date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009"
# 1240596879
# To convert Unix epoch time (seconds since 1970-01-01) to a human readable format:
date -d "UTC 1970-01-01 1240596879 secs"
# Fri Apr 24 13:14:39 CDT 2009
# Or:
date -ud #1000000000
# Sun Sep 9 01:46:40 UTC 2001
# or: Haven't tested this but should work..
date -d #1000000000 +%y%m%d%%H%M%S
# 010909014640

Resources