I have a bash shell script where I take an inputDate string and then add some months to it to get a new date and then I format it as timestamp with milliseconds as below
START=2017_01_01;
i=1;
inputdate="${START//_/-}"
somedate=$(date -d "$inputdate + $i month" "+%Y-%m-%dT%H-%M-%S-%3NZ")
echo "somedate="$somedate
The output is - 2017-01-01T00:00:00.000Z
Is it possible to subtract a millisecond from this, so I get a previous date something like 2016-12-31T23:59:59.000Z for the above example?
Last dates are not fixed for the months, so I am trying to see if I can subtract a millisecond from a given month to get the last date with timestamp for the previous month. Any ideas?
I want to get the previous date in AIX environment.
But it seems date -d commands can't work. And I only found
yesterday=TZ=aaa24 date +%Y%m%d as solution.
However, this raise me the timezone problem and I don't know how to solve it.
And I tried these commands, but it gives wrong date in some time period. (e.g. today is 20190418 07:01:19, yesterday will be 20190416 23:01:19)
today=`date +%Y%m%d`
yesterday=`TZ=aaa24 date +%Y%m%d`
yesterday=`TZ=$TZ+24 date +%Y%m%d`
I echo my #TZ and it is HKG-8.I know that I can use +16 to force this become my answer. But it's not work if the timezone is changed.
Can anyone suggest any solutions for get previous date of a date value or simply any ways to solve time zone problems?
This sample script can help you:
date +%s|awk '{printf "%s\n", strftime("%c",$1-86400)}'
My File contains Date as Thu 03/26/2015
I need to get output as 03/26/2015 I don't need day part in my date
Use command
date /t
Output will gate date only
In my shell, when a date is given with YYYYmmddHHMM format, I would like to add an hour to the given date.
My current code is as below:
DATE=201502252310
NEXT_DATE=$(date +%Y%m%d%H%M -ud "$DATE +1 hour")
echo $NEXT_DATE
When the given date is 201502121010, the result is 2015021210100100.
In fact, I'd like to get 201502121110.
When the given date is 201502122310, the result I'd like to get is 201502130010.
But, it shows date: invalid date '201502122310 +1 hour'.
It seems my code is something wrong.
How can I do to add the specific hour to the given date correctly???
I have a shell script where I ask the system to return the year, 660days ago. On my Mac, I use this:
date -j -v-660d +"%Y"
If run today, that would return 2011.
I'm moving the script to an Ubuntu machine, and I receive an error, stating that the -j and -v options are invalid.
I've looked at the man page, looking for equivalent options, but haven't been able to find a solution.
Any help is appreciated.
This should work:
$ date -d '660 days ago' +%Y
2011
Sometimes, there are more than one version of utilities like date. Oftentimes, the version shipped on Linux is not the same as the version shipped on BSD or macOS.
I found the man page for date on FreeBSD, and it says this:
-j Do not try to set the date. This allows you to use the -f flag
in addition to the + option to convert one date format to an-
other. Note that any date or time components unspecified by the
-f format string take their values from the current time.
-v [+|-]val[y|m|w|d|H|M|S]
Adjust (i.e., take the current date and display the result of the
adjustment; not actually set the date) the second, minute, hour,
month day, week day, month or year according to val. If val is
preceded with a plus or minus sign, the date is adjusted forwards
or backwards according to the remaining string, otherwise the
relevant part of the date is set. The date can be adjusted as
many times as required using these flags. Flags are processed in
the order given.
For the GNU version of date shipped on Linux, typically, the only way to set the date is to pass -s. So we don't need the -j flag. The GNU version of date has -d display the date describe by a passed string.
In your case, you want:
$ date -d '660 days ago' +%Y
2021