How to get the current date and time in specific format in Shell - 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

Related

Why do I always get extra one day with date difference in bash?

I'm trying to calculate the seconds between a date from now, however, I always get extra one day when adding the seconds from now.
echo $(($(date -ud "2020-03-15 19:13" +'%s') - $(date +'%s')))
As of posting, the result is 1744204
Using this website to check, I gets 16 March, not 15 March as expected. Any idea why?
verify the result of your date commands and verify the timezone of both.
The first result of the command shows the timestamp in UTC, and the second one shows the timestamp using the timezone of the system.
Here is the difference:
$ date -ud "2020-03-15 19:13" +'%s'
1584299580
With UTC-3 in my system:
$ date -d "2020-03-15 19:13" +'%s'
1584310380
I hope that help you.

Why is the date command throwing an error despite successfully computing an answer?

For context I am working on a Windows machine with MINGW64. I have not had the chance to test this on a machine running Linux.
I am working with [date][1] in bash and trying to add days to a date represented as seconds since 1970-01-01 00:00:00, i.e. in format %s. I have the following Bash code:
DATE1=$(date --date=now "+%s")
echo "DATE1: ${DATE1}"
DATE2=$(date --date="$(date --date="${DATE1}" "+%s")+2 days" "+%d%b%Y %H:%M:%S")
echo "DATE2: ${DATE2}"
Which gives console output
DATE1: 156133763
date: invalid date '1561337963'
DATE2: 26Jun2019 10:59:24
In fact, DATE2 has the expected value down to the second. However the console still throws an error. Why might this error be occurring? Would it have something to do with running MINGW64?
An update:
I ran this on a Debian server I have access to. It computed the time with now like on MINGW, but returned a different error: ./test.sh: line 3: --date=1561342165: command not found.
Running this on the same server with a custom date:
DATE1=$(date --date="$(date --date="19Jun2019 11:35:46" "+%d%b%Y %H:%M:%S")" "+%s")
Likewise returns
DATE1: 1560908146
date: invalid date ‘1560908146’
DATE2: 26Jun2019 12:20:08
Which actually indicates it isn't adding to the defined date at all, rather it is just adding to today's date (it's the 24th of June where I am now). Clearly, something is quite wrong!
If you want to convert timestamp to a date then you have to use #:
date --date=#1561344591 '+%F %T'
2019-06-24 04:49:51
The reason why you got seemingly correct result even with the error message is because your DATE1 is now, so your inner date call produces an error and returns nothing and then the outer call is just --date=+2 days, which produces 2 days from now.
I don't think you can use date-time in timestamp format together with relative statements, it doesn't work even with #. You have to convert your timestamp to human readable date first but even then you have to be careful. You could think that this is enough:
date --date="$(date --date=#1561344591 "+%F %T") + 2 days" "+%F %T"
2019-06-25 04:49:51
As you can see it is still wrong and that's because +2 is parsed as a timezone. You fix it by explicitly including timezone in you conversion from timestamp:
date --date="$(date --date=#1561344591 "+%F %T%Z") + 2 days" "+%F %T"
2019-06-26 04:49:51
In general, using bash debugging set -x and date debugging date --debug ... helps with solving such problems.

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.

Print the standard time - eg 04:52:06 PM - 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"

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