I want to get the week number using bash.
The man entry for date shows the following:
%V ISO week number, with Monday as first day of week (01..53)
%W week number of year, with Monday as first day of week (00..53)
Which is the difference?
This is the output I get:
$ date "+%W"
48
$ date "+%V"
48
It's a bit more clear in the GNU docs (referenced in the man page):
https://www.gnu.org/software/coreutils/manual/html_node/Date-conversion-specifiers.html#Date-conversion-specifiers
‘%W’ week number of year, with Monday as first day of week (‘00’…‘53’). Days in a new year preceding the first Monday are in week zero.
‘%V’ ISO week number, that is, the week number of year, with Monday as the first day of the week (‘01’…‘53’). If the week containing January 1 has four or more days in the new year, then it is considered week 1; otherwise, it is week 53 of the previous year, and the next week is week 1. (See the ISO 8601 standard.)
Seems to depend on the year on this. I like JoseKilo's explanation.
$ date +%V
32
$ date +%W
31
$ date
Mon Aug 5 08:29:23 MDT 2019
$
$ date -v-2y +%W
31
$ date -v-2y +%V
31
$ date -v-2y
Sat Aug 5 08:29:22 MDT 2017
$
I would say there is a typo there:
$ date "+%W"
52
$ date "+%V"
52
$ date
Sat Dec 30 10:42:06 CET 2017
Best regards
Related
I am trying to get week numbers ( resetting at 1 for each month) as per ISO format for each month in 2019.For example I am interested in getting
All dates in July 2019: week 1 to 4,
All dates in Aug 2019 : week 1 to 4 and so on.
I first created the calculated field (Week_Number_ISO) to get the overall week number in year 2019.I used the following formula;
DATEPART('iso-week',[ Date]) which works as intended.
To get the monthly week number I used the following formula
INT((DATEPART('day',[Created Date])-DATEPART('iso-weekday',[Created Date])+7)/7)+1.
(Idea was to calculate the date of the first day of each week & then divide by 7 and take the integer part)
As per the ISO format, shouldn't July 29 to 31st be a part of week 4 for July?But the formula is showing it as week 5 for July 2019.I feel I am missing something in the formula or am missing something about ISO week number resetting at 1 for each month.
Can someone help me?
Here is an example of the dates in July 2019 and the associated week numbers.
Why would July 28th-July 31st 2019 be considered week 4?
I need to assign a variable with the value in the format YYYYMM e.g for today run the previous month values should be generated as
Var1 = 201607
Is there any in build method available? Could you share the steps to generate this?
The trick in these cases is to subtract one month from the day 15 of the current month:
$ date --date="$(date +%Y-%m-15) - 1 month"
Fri Jul 15 00:00:00 CEST 2016
Then it is just a matter of using the proper format:
$ date --date="$(date +%Y-%m-15) - 1 month" "+%Y%m"
201607
To store the value in a var, just use the common var=$(command) syntax.
From GNU Coreutils → 28.7 Relative items in date strings:
The fuzz in units can cause problems with relative items. For example,
‘2003-07-31 -1 month’ might evaluate to 2003-07-01, because 2003-06-31
is an invalid date. To determine the previous month more reliably, you
can ask for the month before the 15th of the current month. For
example:
$ date -R
Thu, 31 Jul 2003 13:02:39 -0700
$ date --date='-1 month' +'Last month was %B?'
Last month was July?
$ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!'
Last month was June!
Also, take care when manipulating dates around clock changes such as
daylight saving leaps. In a few cases these have added or subtracted
as much as 24 hours from the clock, so it is often wise to adopt
universal time by setting the TZ environment variable to ‘UTC0’ before
embarking on calendrical calculations.
Lets say I want to get number of days past 1st Jan 2016 in Unix (KSH)
Edit: To get number of days from 2016-01-01 till date, use
echo $(( ( $(date +'%s') - $(date -ud '2016-01-01 00:00:00' +'%s') )/60/60/24 ))
117
date utility is able to compute adjustement:
date -v +10d -j 0101000016
will computes the date 10 days after 01/01/2016.
Would it be possible to get same day of week last year using Excel? please below example:
Input: Monday 9 Nov 2015 | Output: 10 Nov 2014
Thanks
Simply subtract 52 full weeks with 7 days = 364 days. So if the date is in A1, the formula =A1-364 will get the date exactly 52 weeks before, which is the same day of week in the year before.
To show that it works even for leap years, try the following:
You see the formula date - 364 (=A2-364, =A3-364, ...) always gets the same day of week a year before. That is because it gets the day minus 52 full weeks (52 * 7 days) before. In leap years it gets a different day but the same day of week.
Try this:
=DATE(YEAR(A1)-1,MONTH(A1),DAY(A1))+WEEKDAY(A1)-WEEKDAY(DATE(YEAR(A1)-1,MONTH(A1),DAY(A1)))
It returns the closest date within a week. A1 is the cell with this year's date.
The MongoDB manual states the $week aggregation and %U operator in strftime work like this:
Returns the week of the year for a date as a number between 0 and 53.
Weeks begin on Sundays, and week 1 begins with the first Sunday of the
year. Days preceding the first Sunday of the year are in week 0. This
behavior is the same as the “%U” operator to the strftime standard
library function.
Does this conform to the ISO 8601 week of the year standard?
No. ISO 8601 does not have week 0.
According to the standard:
The ISO 8601 definition for week 01 is the week with the year's first Thursday in it.
...
If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01;
If 1 January is on a Friday, it is part of week 53 of the previous year;
If on a Saturday, it is part of week 52 (or 53 if the previous year was a leap year);
If on a Sunday, it is part of week 52 of the previous year.
There is an open ticket that suggests adding other options for week numeration; there's a suggestion for $isoweek in the comments:
https://jira.mongodb.org/browse/SERVER-7695