day_of_week#

Overview#

NCL’s day_of_week calculates the day of the week given month, day, and year.

Grab and Go#

import cftime

day = 4
month = 6
year = 2024

dow = cftime.datetime(
    year, month, day, calendar='proleptic_gregorian', has_year_zero=True
).strftime("%w")
print(dow)
2

Using the datetime module#

The datetime module is part of the Python Standard Library and could be sufficient to calculate the day of the week.

import datetime

day = 4
month = 6
year = 2024

dow = datetime.date(year, month, day).strftime('%A')
print(f"{year}-{month}-{day} is a {dow}")
2024-6-4 is a Tuesday

However, the datetime module does not support year 0. If you need to work with year 0, we suggest using cftime.

The datetime module also only uses the proleptic Gregorian calendar. If you need to work with other calendars, we suggest using cftime.

Using cftime#

cftime offers a solution to these potential issues by supporting year 0 and all of the CF Convention calendars. Here is how you can use cftime to calculate the day of the week.

import cftime

day = 4
month = 6
year = 2024

dow = cftime.datetime(
    year, month, day, calendar='proleptic_gregorian', has_year_zero=True
).strftime('%A')
print(f"Proleptic Gregorian calendar: \t\t{year}-{month}-{day} is a {dow}")

dow = cftime.datetime(
    year, month, day, calendar='standard', has_year_zero=True
).strftime('%A')
print(f"Standard calendar: \t\t\t{year}-{month}-{day} is a {dow}")

dow = cftime.datetime(year, month, day, calendar='julian', has_year_zero=True).strftime(
    '%A'
)
print(f"Julian calendar: \t\t\t{year}-{month}-{day} is a {dow}")

dow = cftime.datetime(
    year, month, day, calendar='360_day', has_year_zero=True
).strftime('%A')
print(f"360 day calendar: \t\t\t{year}-{month}-{day} is a {dow}")
Proleptic Gregorian calendar: 		2024-6-4 is a Tuesday
Standard calendar: 			2024-6-4 is a Tuesday
Julian calendar: 			2024-6-4 is a Monday
360 day calendar: 			2024-6-4 is a Wednesday

Using the strftime() method allows you to format the output of cftime.datetime. If you desire an output equivalent to that of NCL’s day_of_week, we suggest using .strftime('%w').

Differences between cftime and NCL’s day_of_week#

Calendars#

NCL’s day_of_week only supports the proleptic Gregorian calendar, while the cftime module supports all CF conventions calendars, including the proleptic_gregorian calendar.

Input Type#

Notably, using cftime to calculate the day of the week works by getting a strftime() value from a cftime.datetime object, which means that the calculations for the day of the week have to be collected through each date individually, while NCL’s days_in_month can take in multidimensional integer arrays, given that the year and month arrays have the same dimensions.

Year 0#

There is a slight difference in the way that NCL’s day_of_week and the cftime module handle the year 0.

  • NCL’s day_of_week supports all positive years and year 0 by default.

  • The cftime module supports all years, but handling for year 0 is dependent upon the calendar and/or the has_year_zero keyword argument.

Python Resources#