bash shell script to subtract milliseconds from a given timestamp - bash

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?

Related

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.

Add the specific hour to a given date in shell script

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

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

Cshell Script date issue

Is there a way to add date in the name of the file... we can add current date in this manner date '+%Y%m%d' but i want to add "filename_date_1-2-2011_thru_31-2-2011.txt" Is it possible to do that??????????
If you have a sufficiently advanced version of the date command and you know a Unix timestamp for the start and end dates, then you can use:
(MacOS X) date -r 1234567890 "+%d-%m-%Y" to obtain 13-02-2009.
(GNU) date -d 2/13/2009 "+%d-%m-%Y" to obtain 13-02-2009 again.
If you don't want the leading zeroes on the day of month, then you need to use '%e` instead of '%d' on Linux (but that puts a space in place of the zero). It is not clear that there's a format specifier for day-of-month without a leading zero on MacOS X; nor is it clear that there's a way to format month of year as a single-digit number for January to September on either platform.
You get the format into your C shell script using back-ticks around the date commands.
Consider reading Csh Programming Considered Harmful and heeding its advice.

Resources