Last active
June 14, 2020 00:24
-
-
Save 221V/9009a2c656c9b4492a3906264c41ec99 to your computer and use it in GitHub Desktop.
date to week number, and week number to date (monday), in js and erlang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% there are 52-53 weeks in the year | |
% http://www.whatweekisit.org/calendar-2020.html | |
% date to week number in year | |
> calendar:iso_week_number({2020,1,1}). | |
{2020,1} | |
> calendar:iso_week_number({2019,12,31}). | |
{2020,1} | |
% get monday (date) of the week, for any day (date) from that week | |
% Date = {Year, Month, Day} | |
monday_in_the_week_of(Date) -> | |
V1 = calendar:date_to_gregorian_days(Date) - (calendar:day_of_the_week(Date) - 1), | |
calendar:gregorian_days_to_date(V1). | |
% test | |
> m1:monday_in_the_week_of({2020,1,1}). | |
{2019,12,30} | |
> m1:monday_in_the_week_of({2020,1,2}). | |
{2019,12,30} | |
> m1:monday_in_the_week_of({2020,1,3}). | |
{2019,12,30} | |
> m1:monday_in_the_week_of({2020,1,4}). | |
{2019,12,30} | |
> m1:monday_in_the_week_of({2020,1,5}). | |
{2019,12,30} | |
> m1:monday_in_the_week_of({2020,1,6}). | |
{2020,1,6} | |
% get monday (date) of the week, by week number and year | |
% Week_Num = 1 .. 53 | |
% check 53th week in year exists = if date for 53th week is equal to 52th week = means max week is 52th | |
monday_in_the_week_of_year(Week_Num, Year) when Week_Num > 0, Week_Num < 54 -> | |
Month = if Week_Num < 8 -> 1; | |
true -> | |
V1 = erlang:floor(Week_Num / 4), | |
if V1 > 12 -> 12; true -> V1 end | |
end, | |
Day = 1, | |
?MODULE:monday_in_the_week_of_year_helper({Year, Month, Day}, Week_Num, Year). | |
monday_in_the_week_of_year_helper({Year, Month, Day}=Date, Week_Num, Year) -> | |
Date2 = calendar:gregorian_days_to_date( calendar:date_to_gregorian_days(Date) - (calendar:day_of_the_week(Date) - 1) ), | |
{Year3, Week_Num2} = calendar:iso_week_number(Date2), | |
if Year3 =:= Year, Week_Num2 =:= Week_Num -> | |
Date2; | |
true -> | |
Max_Day = calendar:last_day_of_the_month(Year, Month), | |
Day3 = Day + 7, | |
if (Day3 > Max_Day) -> | |
case Month of | |
12 -> calendar:gregorian_days_to_date( calendar:date_to_gregorian_days({Year, 12, Max_Day}) - (calendar:day_of_the_week({Year, 12, Max_Day}) - 1) ); | |
_ -> ?MODULE:monday_in_the_week_of_year_helper({Year, Month + 1, Day3 - Max_Day}, Week_Num, Year) | |
end; | |
true -> ?MODULE:monday_in_the_week_of_year_helper({Year, Month, Day3}, Week_Num, Year) | |
end | |
end. | |
% test | |
> m1:monday_in_the_week_of_year(1, 2020). | |
{2019,12,30} | |
> m1:monday_in_the_week_of_year(2, 2020). | |
{2020,1,6} | |
> m1:monday_in_the_week_of_year(3, 2020). | |
{2020,1,13} | |
> m1:monday_in_the_week_of_year(25, 2020). | |
{2020,6,15} | |
> m1:monday_in_the_week_of_year(52, 2020). | |
{2020,12,21} | |
> m1:monday_in_the_week_of_year(53, 2020). | |
{2020,12,28} | |
> m1:monday_in_the_week_of_year(5, 2019). | |
{2019,1,28} | |
> m1:monday_in_the_week_of_year(52, 2019). | |
{2019,12,23} | |
> m1:monday_in_the_week_of_year(53, 2019). | |
{2019,12,30} | |
> m1:monday_in_the_week_of_year(1, 2021). | |
{2021,1,4} | |
> m1:monday_in_the_week_of_year(2, 2021). | |
{2021,1,11} | |
> m1:monday_in_the_week_of_year(3, 2021). | |
{2021,1,18} | |
> m1:monday_in_the_week_of_year(50, 2021). | |
{2021,12,13} | |
> m1:monday_in_the_week_of_year(51, 2021). | |
{2021,12,20} | |
> m1:monday_in_the_week_of_year(52, 2021). | |
{2021,12,27} | |
> m1:monday_in_the_week_of_year(53, 2021). | |
{2021,12,27} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// there are 52-53 weeks in the year | |
//http://www.whatweekisit.org/calendar-2020.html | |
//date to week number in year | |
function getWeekNumber(d) { | |
d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate())); | |
d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7)); | |
var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1)); | |
var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7); | |
return [d.getUTCFullYear(), weekNo]; | |
} | |
// test | |
getWeekNumber(new Date('2020/1/1')) | |
[2020,1] | |
getWeekNumber(new Date('2019/12/31')) | |
[2020,1] | |
// week number and year to week's date (monday) | |
function getDateOfMondayWeek(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; | |
} | |
// test | |
getDateOfMondayWeek(1,2020); | |
Date Mon Dec 30 2019 00:00:00 GMT+0200 (за східноєвропейським стандартним часом) | |
getDateOfMondayWeek(52,2019); | |
Date Mon Dec 23 2019 00:00:00 GMT+0200 (за східноєвропейським стандартним часом) | |
getDateOfMondayWeek(2,2020); | |
Date Mon Jan 06 2020 00:00:00 GMT+0200 (за східноєвропейським стандартним часом) | |
getDateOfMondayWeek(51,2021) | |
Date Mon Dec 20 2021 00:00:00 GMT+0200 (за східноєвропейським стандартним часом) | |
getDateOfMondayWeek(52,2021) | |
Date Mon Dec 27 2021 00:00:00 GMT+0200 (за східноєвропейським стандартним часом) | |
getDateOfMondayWeek(53,2021) | |
Date Mon Jan 03 2022 00:00:00 GMT+0200 (за східноєвропейським стандартним часом) | |
getDateOfMondayWeek(1,2022) | |
Date Mon Jan 03 2022 00:00:00 GMT+0200 (за східноєвропейським стандартним часом) | |
//check for 53th week of year exists -- compare with 1st week of next year -- if the same = no 53th week | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment