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.
Related
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.
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)
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
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.
I will set one date variable(Say '08-JUN-2011') and I want to do some calculations based on that date namely,
1. Have to get the first day of the given day's month.
2. Previous date of the given date's month.
3. Last day of the given date's month.
All I know is manipulating using the current system date and time but don't know how to implement with user defined date. I need this to be achieved using Linux shell script.
Any help will be appreciated.
Thanks,
Karthik
Here's how to perform the manipulations using GNU date:
#!/bin/sh
USER_DATE=JUN-08-2011
# first day of the month
FIRST_DAY_OF_MONTH=$(date -d "$USER_DATE" +%b-01-%Y)
PREVIOUS_DAY=$(date -d "$USER_DATE -1 days" +%b-%d-%Y)
# last day of the month
FIRST_DAY_NEXT_MONTH=$(date -d "$USER_DATE +1 month" +%b-01-%Y)
LAST_DAY_OF_MONTH=$(date -d "$FIRST_DAY_NEXT_MONTH -1 day" +%b-%d-%Y)
echo "User date: $USER_DATE"
echo "1. First day of the month: $FIRST_DAY_OF_MONTH"
echo "2. Previous day: $PREVIOUS_DAY"
echo "3. Last day of the month: $LAST_DAY_OF_MONTH"
The output is:
User date: JUN-08-2011
1. First day of the month: Jun-01-2011
2. Previous day: Jun-07-2011
3. Last day of the month: Jun-30-2011
This is going to be convoluted in a shell script. You are better off using Date::Manip in perl, or something similar in another full-featured language. However, I can think of some ways to do this with the date command. First of all, you can use a --date parameter to set a starting point for date, like so:
$ date --date='08-JUN-2011'
Wed Jun 8 00:00:00 EDT 2011
You can get the previous date like this:
$ date --date='08-JUN-2011 -1 days'
Tue Jun 7 00:00:00 EDT 2011
For the last day of the month, I would just walking back from 31 until date does not fail. You can check $? for that
$ date --date='31-JUN-2011';echo $?
date: invalid date `31-JUN-2011'
1
$ date --date='30-JUN-2011';echo $?
Thu Jun 30 00:00:00 EDT 2011
0
For the first day of the month...that is usually 01 :)
As you're using Linux, hhopefully you have the GNU date utility available. It can handle almost any description of a relative date that you think of.
Here are some examples
date --date="last month" +%Y-%m-%d
date --date="yesterday" +%Y-%m-%d
date --date="last month" +%b
x=$(date --date "10 days ago" +%Y/%m/%d)
To learn more about it see GNU Date examples
Once you use it some, you can shortcut your information gathering with date --help, which shows all the basic options (but is sometimes is hard to interpret.)
I hope this helps.
$ date +%m/01/%Y
I came here looking for a way to get the first day of the current month.
Using dateutils' dround tool:
Current month:
$ dround today -1
2014-02-01
Previous month
$ dround today -31
2014-01-31
Last day of current month:
$ dround today +31
2014-02-28
Of course you can use a custom date instead of today, the idea is to round down or up to the desired day-of-the month, e.g. the next first-of-the-month given 2010-10-04:
$ dround 2010-10-04 +1d
2010-11-01