Add the specific hour to a given date in shell script - shell

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???

Related

bash shell script to subtract milliseconds from a given timestamp

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?

How to get previous date in aix with given value?

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)}'

How can I output the date of the last "day"?

I want to print the day of the last Monday for example
using the date command in a terminal
Provided I do not know what day/ date is today but I can use date to find out the date and then use it to find out what date the first previous monday was.
If you are using gnu date, you can try the following command:
date -d "last monday"

Print current date to csv with bash

I'm having the hardest time trying to do the most simple thing. I want to print the current date with Year-month-day-hour-minute-seconds to a .csv file
I want the formatting of the .csv file to look like this:
Year Month Day Hour Minute Second
2017 03 08 17 52 23
And i cannot figure out for the life of me what to do. I have tried figuring it out with printf and using "," as the delimiter but the formatting will not work. It does not end up in the columns like i want.
Could someone point me in the right direction?
It's a simple date formatting problem. Try the following :
echo "Year,Month,Day,Hour,Minute,Second" && date +"%Y,%m,%d,%H,%M,%S" > myfile.csv
Here is a link to date formatting syntax.

Invalid date options in Ubuntu

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

Resources