I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

What weekdays will Christmas and New Year

From Rosetta Code
What weekdays will Christmas and New Year is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task


Determine programatically and show on this page on what weekday Christmas Day, 2021 and New Year's Day, 2022 will fall or did fall.

ALGOL W[edit]

Re-using code from the Day of the week task.

begin % find the day of the week 25/12/2021 and 01/01/2022 fall on %
string(10) array dayName( 0 :: 6 );
integer procedure Day_of_week ( integer value d, m, y );
begin
integer j, k, mm, yy;
mm := m;
yy := y;
if mm <= 2 then begin
mm := mm + 12;
yy := yy - 1;
end if_m_le_2;
j := yy div 100;
k := yy rem 100;
(d + ( ( mm + 1 ) * 26 ) div 10 + k + k div 4 + j div 4 + 5 * j ) rem 7
end Day_of_week;
dayName( 0 ) := "Saturday"; dayName( 1 ) := "Sunday"; dayName( 2 ) := "Monday";
dayName( 3 ) := "Tuesday"; dayName( 4 ) := "Wednesday"; dayName( 5 ) := "Thursday";
dayName( 6 ) := "Friday";
write( "25th of December 2021 is a ", dayName( Day_of_week( 25, 12, 2021 ) ) );
write( " 1st of January 2022 is a ", dayName( Day_of_week( 1, 1, 2022 ) ) )
end.
Output:
25th of December 2021 is a Saturday
 1st of January  2022 is a Saturday

AWK[edit]

 
# syntax: GAWK -f WHAT_WEEKDAYS_WILL_CHRISTMAS_AND_NEW_YEAR.AWK
BEGIN {
fmt = "%Y-%m-%d is a %A"
print(strftime(fmt,mktime("2021 12 25 0 0 0")))
print(strftime(fmt,mktime("2022 01 01 0 0 0")))
print("")
fmt = "%d %b %Y is %A"
print(strftime(fmt,mktime("2021 12 25 0 0 0")))
print(strftime(fmt,mktime("2022 01 01 0 0 0")))
exit(0)
}
 
Output:
2021-12-25 is a Saturday
2022-01-01 is a Saturday

25 Dec 2021 is Saturday
01 Jan 2022 is Saturday

F#[edit]

 
// Show day of week for Christmas Day, 2021 and New Year's Day, 2022. Nigel Galloway: December 1st., 2021
printfn $"Christmas Day 2021 is a %s{string(System.DateTime(2021,12,25).DayOfWeek)}. New Years Day 2022 is a %s{string(System.DateTime(2022,1,1).DayOfWeek)}."
 
Output:
Christmas Day 2021 is a Saturday. New Years Day 2022 is a Saturday.

Factor[edit]

Works with: Factor version 0.99 2021-06-02
USING: calendar calendar.english calendar.holidays.us formatting
kernel sequences ;
 
CONSTANT: msg
"In %d, New Years is on a %s, and Christmas is on a %s.\n"
 
: day-name ( ts -- str ) day-of-week day-names nth ;
: christmas ( n -- str ) christmas-day day-name ;
: new-years ( n -- str ) new-years-day day-name ;
: holidays ( n -- ny ch ) [ new-years ] [ christmas ] bi ;
: .holidays ( seq -- ) [ dup holidays msg printf ] each ;
 
{ 1578 1590 1642 1957 2020 2021 2022 2242 2245 2393 } .holidays
Output:
In 1578, New Years is on a Sunday, and Christmas is on a Monday.
In 1590, New Years is on a Monday, and Christmas is on a Tuesday.
In 1642, New Years is on a Wednesday, and Christmas is on a Thursday.
In 1957, New Years is on a Tuesday, and Christmas is on a Wednesday.
In 2020, New Years is on a Wednesday, and Christmas is on a Friday.
In 2021, New Years is on a Friday, and Christmas is on a Saturday.
In 2022, New Years is on a Saturday, and Christmas is on a Sunday.
In 2242, New Years is on a Saturday, and Christmas is on a Sunday.
In 2245, New Years is on a Wednesday, and Christmas is on a Thursday.
In 2393, New Years is on a Friday, and Christmas is on a Saturday.

Julia[edit]

See also https://docs.julialang.org/en/v1/stdlib/Dates/#Dates.format-Tuple{TimeType,%20AbstractString}

using Dates
 
for year in [1578, 1590, 1642, 1957, 2020, 2021, 2022, 2242, 2245, 2393]
println("Year $year, New Years's Day: ", Dates.format(DateTime(year, 1, 1), "E, U d, Y"),
" and Christmas: ", Dates.format(DateTime(year, 12, 25), "E, U d, Y"))
end
 
Output:
Year 1578, New Years's Day: Sunday, January 1, 1578 and Christmas: Monday, December 25, 1578
Year 1590, New Years's Day: Monday, January 1, 1590 and Christmas: Tuesday, December 25, 1590
Year 1642, New Years's Day: Wednesday, January 1, 1642 and Christmas: Thursday, December 25, 1642
Year 1957, New Years's Day: Tuesday, January 1, 1957 and Christmas: Wednesday, December 25, 1957
Year 2020, New Years's Day: Wednesday, January 1, 2020 and Christmas: Friday, December 25, 2020
Year 2021, New Years's Day: Friday, January 1, 2021 and Christmas: Saturday, December 25, 2021
Year 2022, New Years's Day: Saturday, January 1, 2022 and Christmas: Sunday, December 25, 2022
Year 2242, New Years's Day: Saturday, January 1, 2242 and Christmas: Sunday, December 25, 2242
Year 2245, New Years's Day: Wednesday, January 1, 2245 and Christmas: Thursday, December 25, 2245
Year 2393, New Years's Day: Friday, January 1, 2393 and Christmas: Saturday, December 25, 2393

ooRexx[edit]

/* REXX */
Call wkd 2021,12,25
Call wkd 2022,01,01
Exit
wkd:
Parse Arg y,m,d
wd=.Array~of('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')
dt=.DateTime~new(y,m,d)
say d'.'m'.'y 'is a' wd[dt~weekday]
Return
Output:
25.12.2021 is a Saturday
01.01.2022 is a Saturday<

Perl[edit]

#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/What_weekdays_will_Christmas_and_New_Year
use warnings;
use Time::Local;
 
for (
['Christmas 2021', 25, 11, 2021 ],
['New Years 2022', 1, 0, 2022 ],
)
{
print "$_->[0] ", qw( Sunday Monday Tuesday Wednesday Thursday Fridat Saturday )
[(localtime timelocal(0, 0, 12, @{$_}[1..3]))[6]], "\n";
}
Output:
Christmas 2021 Saturday
New Years 2022 Saturday

Phix[edit]

with javascript_semantics
include timedate.e
procedure nyc(integer year)
    string ny = format_timedate({year,1,1,0,0,0},"Dddd"),
           cd = format_timedate({year,12,25,0,0,0},"Dddd")
    printf(1,"In %d, New year's day is on a %s, and Christmas day on a %s.\n",{year,ny,cd})
end procedure
papply({1578, 1590, 1642, 1957, 2020, 2021, 2022, 2242, 2245, 2393},nyc)

Note the builtin timedate type does not officially support dates prior to 1752... the fact that the first three lines agree with other entries on this page is pure luck.

Output:
In 1578, New year's day is on a Sunday, and Christmas day on a Monday.
In 1590, New year's day is on a Monday, and Christmas day on a Tuesday.
In 1642, New year's day is on a Wednesday, and Christmas day on a Thursday.
In 1957, New year's day is on a Tuesday, and Christmas day on a Wednesday.
In 2020, New year's day is on a Wednesday, and Christmas day on a Friday.
In 2021, New year's day is on a Friday, and Christmas day on a Saturday.
In 2022, New year's day is on a Saturday, and Christmas day on a Sunday.
In 2242, New year's day is on a Saturday, and Christmas day on a Sunday.
In 2245, New year's day is on a Wednesday, and Christmas day on a Thursday.
In 2393, New year's day is on a Friday, and Christmas day on a Saturday.

Python[edit]

 
iimport datetime
 
weekDays = ("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
thisXMas = datetime.date(2021,12,25)
thisXMasDay = thisXMas.weekday()
thisXMasDayAsString = weekDays[thisXMasDay]
print("This year's Christmas is on a {}".format(thisXMasDayAsString))
 
nextNewYear = datetime.date(2022,1,1)
nextNewYearDay = nextNewYear.weekday()
nextNewYearDayAsString = weekDays[nextNewYearDay]
print("Next new year is on a {}".format(nextNewYearDayAsString))
 
Output:
This year's Christmas is on a Saturday
Next new year is on a Saturday

Raku[edit]

my @d-o-w = < Sunday Monday Tuesday Wednesday Thursday Friday Saturday >;
 
.say for (flat 2020..2022, (1500 .. 2500).roll(7)).sort.map: {
"In {$_}, New Years is on a { @d-o-w[Date.new($_, 1, 1).day-of-week % 7] }, " ~
"and Christmas on a { @d-o-w[Date.new($_, 12, 25).day-of-week % 7] }."
}
Sample output:
In 1578, New Years is on a Sunday, and Christmas on a Monday.
In 1590, New Years is on a Monday, and Christmas on a Tuesday.
In 1642, New Years is on a Wednesday, and Christmas on a Thursday.
In 1957, New Years is on a Tuesday, and Christmas on a Wednesday.
In 2020, New Years is on a Wednesday, and Christmas on a Friday.
In 2021, New Years is on a Friday, and Christmas on a Saturday.
In 2022, New Years is on a Saturday, and Christmas on a Sunday.
In 2242, New Years is on a Saturday, and Christmas on a Sunday.
In 2245, New Years is on a Wednesday, and Christmas on a Thursday.
In 2393, New Years is on a Friday, and Christmas on a Saturday.

Ring[edit]

? "working..."
weekdays = ["Mon","Tues","Wednes","Thurs","Fri","Satur","Sun"]
dow = timelist()[15]
today = date()
tycd = "25/12/" + substr(today, 7, 4)
nnyd = "01/01/" + string(number(substr(today, 7, 4)) + 1)
for day = 0 to 366
anotherday = adddays(today, day)
if anotherday = tycd
 ? "This year's Christmas day is on a " + nameof(day) + "."
ok
if anotherday = nnyd
 ? "The next New Year's day is on a " + nameof(day) + "."
exit
ok
next
put "done..."
 
func nameof day
return weekdays[((day + today - 1) % 7) + 1] + "day"
Output:
working...
This year's Christmas day is on a Saturday.
The next New Year's day is on a Saturday.
done...

Wren[edit]

Library: Wren-date

This uses the same years as the Raku example.

The actual days for 1578 may have been different as the Gregorian calendar didn't start until 1582.

import "./date" for Date
 
var years = [1578, 1590, 1642, 1957, 2020, 2021, 2022, 2242, 2245, 2393]
for (year in years) {
var newYear = Date.new(year, 1, 1).weekDay
var xmas = Date.new(year, 12, 25).weekDay
System.print("In %(year), New year's day is on a %(newYear), and Christmas day on %(xmas).")
}
Output:
In 1578, New year's day is on a Sunday, and Christmas day on Monday.
In 1590, New year's day is on a Monday, and Christmas day on Tuesday.
In 1642, New year's day is on a Wednesday, and Christmas day on Thursday.
In 1957, New year's day is on a Tuesday, and Christmas day on Wednesday.
In 2020, New year's day is on a Wednesday, and Christmas day on Friday.
In 2021, New year's day is on a Friday, and Christmas day on Saturday.
In 2022, New year's day is on a Saturday, and Christmas day on Sunday.
In 2242, New year's day is on a Saturday, and Christmas day on Sunday.
In 2245, New year's day is on a Wednesday, and Christmas day on Thursday.
In 2393, New year's day is on a Friday, and Christmas day on Saturday.

XPL0[edit]

func    WeekDay(Year, Month, Day);      \Return address of day of week
int Year, Month, Day; \Works for years 1583 onward
int DayOfWeek, Names;
[if Month<=2 then [Month:= Month+12; Year:= Year-1];
DayOfWeek:= rem((Day-1 + (Month+1)*26/10 + Year + Year/4 + Year/100*6 + Year/400)/7);
Names:= ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
return Names(DayOfWeek);
]; \WeekDay
 
[Text(0, "This Christmas is on a ");
Text(0, WeekDay(2021, 12, 25)); CrLf(0);
Text(0, "This New Year's Day is on a ");
Text(0, WeekDay(2022, 1, 1)); CrLf(0);
]
Output:
This Christmas is on a Saturday
This New Year's Day is on a Saturday