Comparing date with csh - linux

Is there a way to compare the input date with current date if the input date has the following format?
Sat Oct 10 00:00:55 2015
Right now im checking if the month is not in NOV, DEC and the year is still 2015.
Is there a better way?

The gnu corutils date command, standard under Linux, has a spiffy -d option that accepts a date in a variety of formats and then reformats it under the given string.
Convert your two dates into a format friendly for what you want:
set date_then_Y = "`date -d "$input_date" '+%Y'`";
set date_then_m = "`date -d "$input_date" '+%m'`";
eval "date '+set date_now_Y = '%Y'; set date_now_m = '%m';"
Now it is a matter of using the consistent variables to test what you want.
(have not run this, final debugging is left as an exercise to the student)

Related

Sum number of days to specific date in dd/mm/yyyy format in bash

This might be a simple problem but I'm having some trouble getting trough it:
I need to sum a number of weeks to a specific date that is in the format dd/mm/yyyy.
I know I can sum number of weeks to the current day using date -d "+1 week" +"%d%m%Y", but I'm having trouble when I try to do this to a specific date in the format I mentioned above (29/06/2019, for example).
Would anyone be able to explain how I can perform this operation?
$ date -d '20190629+1 week' +"%d%m%Y"
06072019
$ date -d '06/29/2019+1 week' +"%d%m%Y"
06072019
without delimiters it's not a valid date format though.

How to extract the UTC offset of a specific date and time using bash?

To be specific I do not want date +%z or date -R for the current local time, but I need something very similar that should work for any date in the past. For example, something like:
command 2018-04-01 12:33:45
should return the UTC offset (assume Chicago as my local time zone) at 12:33:45 on 1st April 2018 local time, Chicago time for example.
I searched extensively and there, probably, is no question close to this one, everyone wants current offset not date-time specific one, therefore it is not a duplicate.
Thanks very much
Update:
I have found something here, that asnwers how to get past dates using date command, then I have combined it with -R to get something close to what I want:
date -d "35 days ago" -R
I can go 35 days back and get the UTC offset.
To convert a particular date time to UTC you can use below command
for example date you provided in your question
2018-04-01 12:33:45
It would be something like below
date -u --date=#$(date "+%s" --date="2018-04-01 12:33:45")
which would have output similar to Sun Apr 1 10:33:45 UTC 2018
If you would like to achieve command date here then you can either create a command alias for above command or use above command in your script providing the date value to convert as an argument
I am answering my own question based on
My own update just after submitting the question.
Nahuel Fouilleul's comment.
Quoting from my own "edit":
"I have found something here, that answers how to get past dates using date command, then I have combined it with -R to get something close to what I want:
date -d "35 days ago" -R
I can go 35 days back and get the UTC offset."
here comes Nahuel's comment, "what about date -d 2018-04-01T12:33:45 -R"
That is in line with what I wrote in my "edit". Nahuel's solution works perfectly for me.
also Usman Malik's answer perfectly provides the solution.
So to summarize, my (not entirely though) answer will be:
date -d specific_past_date -R
where the specific_past_date is the date on which I need the UTC offset, using the date and time that I mentioned in my question, if I do:
date -d 2018-04-01T12:33:45 -R
I get Sun, 01 Apr 2018 12:33:45 -0500 from Chicago, that means Chicago time was 5 hours behind UTC on 1st April 2018 at 12:33:45.

Use variable within unix date command

OS: CentOs 6 Final 64 Bit
I'm trying to write a shell script which gets the date a given number of days ago. The number of days is dynamic and the code I'm trying to use is this...
date +%d-%m-%Y -d '$days days ago'
The response I get back is...
date: invalid date `$days days ago'
Can anyone suggest to me the correct format to code this as I'm pulling my hair out and soon my teeth!!
Cheers
Paul
Change single quote ' to double quote ". Otherwise the string $days will be sent instead of its value.
Try:
[matias#lappie ~]$ date +%d-%m-%Y -d "now - $days days"
04-02-2015

Get Current month using `date` in Linux

I am trying to get current month based on date.
Till now it was working fine with below command -
cur_mon=`date --date="$report_date" '+%m' -d 'now'`
cur_year=`date --date="$report_date" '+%y' -d 'now'`
echo cur_mon and cur_year =${cur_mon} and ${cur_year}
$report_date is now 30th Sep 2014.
It is giving me output -
cur_mon and cur_year =10 and 14
Let me know if anyone knows the reason as well solution.
Also this can also create problem in year. If date is 31st Dec, 2014.
I think I see your problem, you are trying to describe your date twice. Once with --date="$report_date" and second time with -d now. Only the latter one is in effect. That's why you see 10 instead of 9.
Try awk '{print $2}' <(date) it should select the second element delimited by spaces in the output of date which is nominally month.

how to get day of the year in shell?

How can I get the day of the year in shell?
date '+%V' will give me the week of the year, which is 15 for today; but I need to find the day of the year!
From the coreutils date manual:
%j day of year (001..366)
Use the date command and the %j option...
doy=$(date +%j)
POSIX mandates the format string %j for getting the day of the year, so if you want it for today's date, you are done, and have a portable solution.
date +%j
For getting the day number of an arbitrary date, the situation is somewhat more complex.
On Linux, you will usually have GNU date, which lets you query for an arbitrary date with the -d option.
date -d '1970-04-01' +%j
The argument to -d can be a fairly free-form expression, including relative times like "3 weeks ago".
date -d "3 weeks ago" +%j
On BSD-like platforms, including MacOS, the mechanism for specifying a date to format is different. You can ask it to format a date with -j and specify the date as an argument (not an option), and optionally specify how the string argument should be parsed with -f.
date -j 04010000 +%j
displays the day number for April 1st 00:00.
The string argument to specify which date to examine is rather weird, and requires the minutes to be specified, and then optionally allows you to prefix with ((month,) day, and) hour, and optionally allows year as a suffix (sic).
date -j -f "%Y-%m-%d" 1970-04-01 +%j
uses -f format date to pass in a date in a more standard format, and prints the day number of that.
There's also the -v option which allows you to specify relative times.
date -j -v -3w +%j
displays the day number of the date three weeks ago.
If you are looking for a proper POSIX-portable solution for getting the day number of arbitrary dates, the least unattractive solution might be to create your own program. If you can rely on Python or Perl (or GNU Awk) to be installed, those make it relatively easy, though it's still a bit of a chore using only their default libraries.

Resources