Calculating Days of Week Given a Week Number

Calculating days of week given a week number

PHP

$week_number = 40;
$year = 2008;
for($day=1; $day<=7; $day++)
{
echo date('m/d/Y', strtotime($year."W".$week_number.$day))."\n";
}


Below post was because I was an idiot who didn't read the question properly, but will get the dates in a week starting from Monday, given the date, not the week number..

In PHP, adapted from this post on the PHP date manual page:

function week_from_monday($date) {
// Assuming $date is in format DD-MM-YYYY
list($day, $month, $year) = explode("-", $_REQUEST["date"]);

// Get the weekday of the given date
$wkday = date('l',mktime('0','0','0', $month, $day, $year));

switch($wkday) {
case 'Monday': $numDaysToMon = 0; break;
case 'Tuesday': $numDaysToMon = 1; break;
case 'Wednesday': $numDaysToMon = 2; break;
case 'Thursday': $numDaysToMon = 3; break;
case 'Friday': $numDaysToMon = 4; break;
case 'Saturday': $numDaysToMon = 5; break;
case 'Sunday': $numDaysToMon = 6; break;
}

// Timestamp of the monday for that week
$monday = mktime('0','0','0', $month, $day-$numDaysToMon, $year);

$seconds_in_a_day = 86400;

// Get date for 7 days from Monday (inclusive)
for($i=0; $i<7; $i++)
{
$dates[$i] = date('Y-m-d',$monday+($seconds_in_a_day*$i));
}

return $dates;
}

Output from week_from_monday('07-10-2008') gives:

Array
(
[0] => 2008-10-06
[1] => 2008-10-07
[2] => 2008-10-08
[3] => 2008-10-09
[4] => 2008-10-10
[5] => 2008-10-11
[6] => 2008-10-12
)

Get Date of days of a week given year, month and week number (relative to month) in Javascript / Typescript

If you want Monday as the first day of the week, and the first week of a month is the one with the first Thursday, then you can use a similar algorithm to the year week number function.

So get the start of the required week, then just loop 7 times to get each day. E.g.





/* Return first day of specified week of month of year
**
** @param {number|string} year - year for required week
** @param {number|string} month - month for required week
** Month is calendar month number, 1 = Jan, 2 = Feb, etc.
** @param {number|string} week - week of month
** First week of month is the one with the first Thursday
** @returns {Date} date for Monday at start of required week
*/
function getMonthWeek(year, month, week) {
// Set date to 4th of month
let d = new Date(year, month - 1, 4);
// Get day number, set Sunday to 7
let day = d.getDay() || 7;
// Set to prior Monday
d.setDate(d.getDate() - day + 1);
// Set to required week
d.setDate(d.getDate() + 7 * (week - 1));
return d;
}

// Return array of dates for specified week of month of year
function getWeekDates(year, month, week) {
let d = getMonthWeek(year, month, week);
for (var i=0, arr=[]; i<7; i++) {

// Array of date strings
arr.push(d.toDateString());

// For array of Date objects, replace above with
// arr.push(new Date(d));

// Increment date
d.setDate(d.getDate() + 1);
}
return arr;
}

// Week dates for week 1 of Jan 2020 - week starts in prior year
console.log(getWeekDates(2020, 1, 1));
// Week dates for week 5 of Jan 2020 - 5 week month
console.log(getWeekDates(2020, 1, 5));
// Week dates for week 1 of Oct 2020 - 1st is a Thursday
console.log(getWeekDates(2020, 10, 1));
// Week dates for week 1 of Nov 2020 - 1st is a Sunday
console.log(getWeekDates(2020, 11, 1));

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;
}

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.

jquery/javascript- calculate days on this week given week number and year number

If you remake the code from this question you will get something like this:

function getDays(year, week) {
var j10 = new Date(year, 0, 10, 12, 0, 0),
j4 = new Date(year, 0, 4, 12, 0, 0),
mon = j4.getTime() - j10.getDay() * 86400000,
result = [];

for (var i = -1; i < 6; i++) {
result.push(new Date(mon + ((week - 1) * 7 + i) * 86400000));
}

return result;
}

DEMO: http://jsfiddle.net/TtmPt/

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