Finding the Date for a Given Week Number

How do I calculate the week number given a date?

Pseudocode:

int julian = getDayOfYear(myDate)  // Jan 1 = 1, Jan 2 = 2, etc...
int dow = getDayOfWeek(myDate) // Sun = 0, Mon = 1, etc...
int dowJan1 = getDayOfWeek("1/1/" + thisYear) // find out first of year's day
// int badWeekNum = (julian / 7) + 1 // Get our week# (wrong! Don't use this)
int weekNum = ((julian + 6) / 7) // probably better. CHECK THIS LINE. (See comments.)
if (dow < dowJan1) // adjust for being after Saturday of week #1
++weekNum;
return (weekNum)

To clarify, this algorithm assumes you number your weeks like this:

S  M  T  W  R  F  S
1 2 3 <-- week #1
4 5 6 7 8 9 10 <-- week #2
[etc.]

getDayOfWeek() and getDayOfYear() are standard date-object operations in most languages. If yours doesn't have them, you can count-forward from some known date (Jan 1, 1970 is a common one), after looking up to see what day of the week it was.

If you're going to implement your own date counting routines, remember that years that are divisible by 100 are NOT leap years, unless they are also divisible by 400. So 1900 was not a leap year, but 2000 was. If you're going to work far back in time, you have to mess with Gregorian vs Julian calendars, etc., see Wikipedia for loads of info on that.

This link talks about date/time functions in Windows/C++ in greater detail.

Calculate date from week number in JavaScript

function getDateOfWeek(w, y) {
var d = (1 + (w - 1) * 7); // 1st of January + 7 days for each week

return new Date(y, 0, d);
}

This uses the simple week definition, meaning the 20th week of 2013 is May 14.

To calculate the date of the start of a given ISO8601 week (which will always be a Monday)

function getDateOfISOWeek(w, y) {
var simple = new Date(y, 0, 1 + (w - 1) * 7);
var dow = simple.getDay();
var ISOweekStart = simple;
if (dow <= 4)
ISOweekStart.setDate(simple.getDate() - simple.getDay() + 1);
else
ISOweekStart.setDate(simple.getDate() + 8 - simple.getDay());
return ISOweekStart;
}

Result: the 20th week of 2013 is May 13, which can be confirmed here.

Given a week number in C#, how can I gain start and end dates of that week?

It's not pretty, and Zohar Peled's comment is very valid, but this works for a "normal" (for the lack of a better word) calendar. (IE: No localization, nothing special) This should provide a sufficient base to go from.

public DateTime GetSaturdayDateOfWeek(int weekNumberInYear)
{
var myDate = new DateTime(DateTime.Now.Year, 1, 1);
myDate = myDate.AddDays((weekNumberInYear -1)* 7);
if (myDate.DayOfWeek < DayOfWeek.Saturday)
{
myDate = myDate.AddDays(DayOfWeek.Saturday - myDate.DayOfWeek);
}
if (myDate.DayOfWeek > DayOfWeek.Saturday)
{
myDate = myDate.AddDays(myDate.DayOfWeek - DayOfWeek.Saturday);
}
return myDate;
}

Week number based on Date

Assuming 30-07-18 is located @ A1. in B1, type in :

=WEEKNUM(A1,2)-WEEKNUM($A$1,2)+1

and drag it down. Glad that you'd tried. ( :

All the best.

How to calculate date based on week number in R

You can try this:

first.day <- as.numeric(format(as.Date("2014-01-01"), "%w"))
week <- 10
as.Date("2014-01-01") + week * 7 - first.day
# [1] "2014-03-09"

This assumes weeks start on Sundays. First, find what day of the week Jan 1 is, then, just add 7 * number of weeks to Jan 1, - the day of week Jan 1 is.

Note this is slightly different to what you get if you use %W when doing the reverse, as from that perspective the first day of the week seems to be Monday:

format(seq(as.Date("2014-03-08"), by="1 day", len=5), "%W %A %m-%d")
# [1] "09 Saturday 03-08" "09 Sunday 03-09" "10 Monday 03-10" "10 Tuesday 03-11"
# [5] "10 Wednesday 03-12"

but you can adjust the above code easily if you prefer the Monday centric view.

How to get the DATE, by supplying year, month, week number, weekday in excel

=DATE(A1,1,1)+7*(A3-1)+MATCH(A4,{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"},0)-WEEKDAY(DATE(A1,1,1)+7*(A3-1))

The above will give you a date of 16/05/11 which when check with WEEKNUM and WEEKDAY is the 20th week and 4th day of the week (Wednesday).

IF you want the date to be 16/05/18 which when checked with WEEKNUM and WEEKDAY is the 21st week and the 4th day of the week (Wednesday), then remove the -1 and just use A3.

Proof of concept

Sample Image

Get dates from a week number in T-SQL

Quassnoi's answer works, but kind of leaves you on the hook for cleaning up the dates if they are dates in the middle of the day (his start of week leaves you one day earlier than you need to be if you use a time in the middle of the day -- you can test using GETDATE()).

I've used something like this in the past:

SELECT 
CONVERT(varchar(50), (DATEADD(dd, @@DATEFIRST - DATEPART(dw, DATECOL), DATECOL)), 101),
CONVERT(varchar(50), (DATEADD(dd, @@DATEFIRST - DATEPART(dw, DATECOL) - 6, DATECOL)), 101)

A side benefit of this is that by using @@DATEFIRST you can handle nonstandard week starting days (the default is Sunday, but with SET @@DATEFIRST you can change this).

It seems crazy that simple date manipulation in SQL Server has to be this arcane, but there you go...

How is the week number calculated based on a date?

How are these numbers derived?

Well, that depends on what system you're using, but I suspect you're after the ISO-8601 week number, which is defined like this:

2.2.10 calendar week number

ordinal number which identifies a calendar week within its calendar year according to the rule that the first calendar week of a year is that one which includes the first Thursday of that year and that the last calendar week of a calendar year is the week immediately preceding the first calendar week of the next calendar year

Bear in mind that a week in ISO-8601 starts on Monday and finishes on Sunday - so another way of expressing the "first Thursday" rule is that the first week of the year is the first week containing at least four days of the new year.

Another vital point is that when you're expressing values using "week of week year", you need to use the "week year" itself as well, not the normal year. So for Sunday January 3rd 2010, you might express this as "Week 53, week year 2009, day-of-week Sunday". It's all too easy to use the "wrong type of year" and end up messing up values around the end of December and start of January.



Related Topics



Leave a reply



Submit