Calendars and Dates

Overview

Many forms ask for a date: a birthday, a due date, the day of a trip. A program can ask a person to type the date, but people write dates in many ways, and a typed date is easy to get wrong. Quorum has two controls for dates. A calendar shows a whole month, and a person picks a day from it. A date field is a text field for a date with a button that opens a calendar below it. In this tutorial, you will add both to a form, read the date a person chose, limit which days can be picked, and make your program react the moment a date changes.

Goals

You have the following goals for this tutorial:

By the end of this lesson, you can

  • Add a date field with AddDateField and a calendar with AddCalendar
  • Read the chosen date with GetDate and work with it as a CalendarDate
  • Limit the days a person can choose with SetMinimumDate and SetMaximumDate
  • Use a CalendarListener to react when a person chooses a date
  • Decide how precise a piece of data needs to be, such as a day or an exact time
  • Explain how a person using a keyboard or a screen reader picks a date

Warm up

How would you write the date of the first day of school? Would you write "9/2," "2 September," "2025-09-02" or something else? Think of one other way someone might write it. If a friend in another country read "3/4," would they think March 4 or April 3? What problems could that cause on a form?

Vocabulary

You will be learning about the following vocabulary words:

Vocabulary
TermDefinition
CalendarA control that shows one month. A person picks a date by choosing one of its days.
Date fieldA text field for a date, with a button that opens a calendar below it. A person can type the date or choose it.
CalendarDateThe Quorum class for a day on the calendar: a year, a month and a day, with no time of day.
Date formatThe way a date is written as text, such as "March 12, 2027" or "03/12/2027". A DateFormat object holds one.
Current dayThe day in a calendar that the arrow keys are on. It has a ring around it while the calendar has the focus.
PrecisionHow exact a piece of data is. A date is precise to the day. A time can be precise to the minute or the second.
ListenerAn object that a control tells when something happens. A CalendarListener is told when a person chooses a date.

New Code to Learn

You will be using the following new pieces of code:

New Code to Learn
Quorum CodeCode ExampleExplanation
AddDateField(text name)DateField due = page:AddDateField("Due date")Adds a date field to a form page. The name is what a screen reader says.
AddCalendar(text name)Calendar tour = page:AddCalendar("Tour day")Adds a calendar to a form page. The text becomes the calendar's description.
SetDate(integer year, integer month, integer day)boolean valid = day:SetDate(2027, 3, 12)Sets a CalendarDate. Returns false, and changes nothing, if there is no such date.
SetToToday()today:SetToToday()Sets a CalendarDate to today, on the computer's clock.
GetDate()CalendarDate chosen = due:GetDate()Returns the date in a date field or calendar, or undefined if there is none.
GetDateField(text name)DateField due = page:GetDateField("Project due date")Finds a date field on a page by its name. GetCalendar finds a calendar by the text given to AddCalendar.
SetDate(CalendarDate date)due:SetDate(day)Puts a date in a date field or chooses it on a calendar, from code. Listeners are not told.
ToSpokenText()output day:ToSpokenText()Returns the date in words, such as "Friday, March 12, 2027".
DaysUntil(CalendarDate other)integer days = today:DaysUntil(due)Returns the number of days from one date to another. It is negative if the other date is earlier.
SetMinimumDate(CalendarDate date)tour:SetMinimumDate(today)Sets the earliest day that can be chosen.
SetMaximumDate(CalendarDate date)tour:SetMaximumDate(last)Sets the latest day that can be chosen.
AddCalendarListener(CalendarListener listener)tour:AddCalendarListener(listener)Tells the listener each time a person chooses a date.
CalendarChanged(CalendarChangedEvent event)action CalendarChanged(CalendarChangedEvent event)The action a listener writes. The event's GetNewDate and GetOldDate say what changed.

CSTA Standards

This tutorial covers the following CSTA standards:

For teachers · 2026 CSTA K–12 Computer Science Standards

HS-ALG-IM-09

High School · Algorithms & Design

Design a computing technology using human-centered design principles.

Also in 7 other lessons

MS-PRO-VD-16

Middle School · Programming

Use variables of multiple data types to store, access, and manipulate data within a program.

Also in 6 other lessons

MS-DAT-DC-21

Middle School · Data & Analysis

Evaluate how different levels of precision and granularity in data collection affect accuracy, storage, and analysis.

Also in 3 other lessons

MS-SYS-IM-37

Middle School · Systems & Security

Examine how access to computing systems can vary based on personal and social factors, such as physical ability, geographic location, socioeconomic status, and age.

Also in 3 other lessons

Every standard, and the lessons that cover it

Explore

Dates in Quorum

Before a program can ask for a date, it needs a way to store one. Quorum's CalendarDate class holds a single day: a year, a month from 1 to 12, and a day of the month. It has no time of day and no time zone.

SetDate sets a date from three numbers. It also checks them. There is no February 30, so SetDate returns false and leaves the date as it was:

use Libraries.System.CalendarDate

CalendarDate day
boolean valid = day:SetDate(2027, 2, 30)
if not valid
    output "There is no February 30."
end

valid = day:SetDate(2027, 3, 12)
output day:ToText()
output day:ToSpokenText()

CalendarDate today
today:SetToToday()
output "That is " + today:DaysUntil(day) + " days from today."

ToText writes the date the way the international standard ISO 8601 does, year first: 2027-03-12. Because the year, month and day are always in the same order, nobody can mix up the month and the day. ToSpokenText writes the date in words, "Friday, March 12, 2027," which is how screen readers say dates in these controls. DaysUntil counts the days from one date to another.

How precise does a date need to be?

A CalendarDate is precise to the day. Quorum's DateTime class, which you may have used to read the clock, also keeps the hour, the minute and the second. Which one should a program use? It depends on the question the data answers:

  • a birthday or a due date needs only the day;
  • a race result needs minutes and seconds;
  • a bus schedule needs the minute.

More precision isn't always better. A time of day also brings a time zone with it, and the same moment falls on different dates in different places. If a program stored a birthday as a moment in time, a person in another time zone could get the birthday a day early or late. Storing only the day avoids that problem and keeps less data. Keep this in mind for the Engage activity.

Making a date field on a form

A date field looks like a text field, with a small calendar button at its right edge. A person can type a date, or press the button and pick one. A page's AddDateField action makes one, adds it to the page and returns it. Its name is what a screen reader says, so put a label with the same words just before it. On a form, the label sits beside the field.

The program below asks for a science fair project's due date and has a button that counts the days left.

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.DateField
use Libraries.Interface.Controls.Button
use Libraries.System.CalendarDate

Form form
Page page = form:GetMainPage()
page:SetTitle("Science Fair")

page:AddLabel("Due date label", "Project due date")
DateField due = page:AddDateField("Project due date")

CalendarDate fairDay
boolean valid = fairDay:SetDate(2027, 3, 12)
due:SetDate(fairDay)

Button checkButton = page:AddButton("Count the days")
checkButton:SetDescription("Shows how many days are left until the due date.")

DaysLeftBehavior behavior
checkButton:SetBehavior(behavior)

form:Display()

The field shows the date as "March 12, 2027." This is its date format. Writing the month as a word means nobody can mix up the month and the day.

Reading the date

A date field's GetDate action reads the text in the field and returns it as a CalendarDate. If the field is empty, or its text isn't a date in the field's format, GetDate returns undefined, so always check for that first.

Make a class in a file named DaysLeftBehavior.quorum. It comes from FormBehavior, which gives it the GetPage action, so it can find the date field by its name with GetDateField.

use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Forms.Page
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.DateField
use Libraries.System.CalendarDate

class DaysLeftBehavior is FormBehavior
    action Run(BehaviorEvent event)
        Page page = GetPage()
        DateField field = page:GetDateField("Project due date")
        if field = undefined
            return now
        end

        CalendarDate due = field:GetDate()
        if due = undefined
            output "Type a date such as March 12, 2027, or choose one from the calendar."
            return now
        end

        CalendarDate today
        today:SetToToday()
        integer days = today:DaysUntil(due)
        if days > 0
            output "The project is due " + due:ToSpokenText() + ". That is " + days + " days from today."
        elseif days = 0
            output "The project is due today."
        else
            integer late = 0 - days
            output "The due date was " + late + " days ago."
        end
    end
end

Run the program and press "Count the days." Then type 3/12/2027 in the field, press Tab and press the button again. The field's format is "March 12, 2027," so 3/12/2027 isn't a date to it, and GetDate returns undefined. The text stays as typed, so the person can fix it.

You can choose another format with SetDateFormat. A DateFormat has three ready-made formats: GetLongFormat ("March 12, 2027," the default), GetNumericFormat ("03/12/2027") and GetISOFormat ("2027-03-12"):

use Libraries.System.DateFormat

DateFormat formats
due:SetDateFormat(formats:GetISOFormat())

Making a calendar on a form

Sometimes a person needs the whole month at once, for example to pick a day in the next few weeks. A page's AddCalendar action adds a calendar. It shows one month, with the month and year at the top between Previous month and Next month buttons. Weeks start on Sunday. Today has a ring around it, and the chosen day is filled with color.

A calendar can limit which days can be picked. SetMinimumDate and SetMaximumDate set the earliest and latest days. Days outside them are dimmed and struck through, and they can't be chosen.

The program below lets a person book a museum tour in the next 30 days.

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.Calendar
use Libraries.Game.Graphics.Label
use Libraries.System.CalendarDate

Form form
Page page = form:GetMainPage()
page:SetTitle("Book a Tour")

page:AddLabel("Help", "Choose a day in the next 30 days.")
Calendar tourDay = page:AddCalendar("Tour day")

CalendarDate first
first:SetToToday()
CalendarDate last = first:Copy()
last:AddDays(30)
tourDay:SetMinimumDate(first)
tourDay:SetMaximumDate(last)

Label report = page:AddLabel("Report", "No day chosen yet.")

TourDayListener listener
listener:SetReport(report)
tourDay:AddCalendarListener(listener)

form:Display()

Copy makes a second date for the same day, so AddDays(30) moves last without moving first. A calendar's GetDate returns the chosen day, or undefined before a day is chosen.

Blocks

Reacting when a date is chosen

A calendar and a date field both tell listeners when a person chooses a date. A listener is a class that comes from CalendarListener and writes one action, CalendarChanged. The event's GetNewDate returns the date chosen now, and GetOldDate the date before it. Either can be undefined: the old date when nothing was chosen before, and the new date when a person empties a date field.

Make a class in a file named TourDayListener.quorum. It shows the chosen day in the label:

use Libraries.Interface.Events.CalendarListener
use Libraries.Interface.Events.CalendarChangedEvent
use Libraries.Game.Graphics.Label
use Libraries.System.CalendarDate

class TourDayListener is CalendarListener
    Label report = undefined

    action SetReport(Label label)
        report = label
    end

    action CalendarChanged(CalendarChangedEvent event)
        CalendarDate chosen = event:GetNewDate()
        if chosen = undefined
            report:SetText("No day chosen yet.")
        else
            report:SetText("Your tour is on " + chosen:ToSpokenText() + ".")
        end
    end
end

Run the program and choose a day. The label changes as soon as the day is chosen. Try to choose a day that is struck through. Nothing happens, and the listener isn't told.

The same listener works on a date field. A date field tells its listeners when a person picks a day in its calendar, or types a new date and leaves the field. As with other controls, a program's own SetDate doesn't tell listeners.

Choosing a date with the keyboard

In a calendar, the arrow keys move a ring around the current day. Moving past the end of the month shows the next month. Choosing a day takes one more key.

A calendar's keys
KeyWhat it does
Left, RightThe day before or after
Up, DownThe same day a week before or after
Home, EndThe first or last day of the week
Page Up, Page DownThe same day of the previous or next month
Shift+Page Up, Shift+Page DownThe same day of the previous or next year
Enter or SpaceChooses the current day
TabMoves from the days to the Previous month and Next month buttons, then on

In a date field, a person can type the date, or open its calendar:

A date field's keys
KeyWhat it does
Alt+DownOpens the calendar and moves the focus into it
Alt+Up or EscapeCloses the calendar without choosing a day
TabMoves from the text to the calendar button, then on
Space or Enter on the calendar buttonOpens the calendar and moves the focus into it

In an open calendar, the keys are the ones in the first table. Choosing a day writes it in the field and closes the calendar. When the calendar closes, the focus goes back to the field. A click outside the calendar closes it too.

These keys follow the date picker in the WAI-ARIA Authoring Practices, a guide from the W3C for accessible web controls, so they match many date pickers on the web.

What a screen reader says

The days of a calendar are drawn by the calendar, but each one still has words for screen readers. Each day says its full date, such as "Thursday, November 12, 2026," and adds "today" when it is today. Because each day says the whole date, a person never has to remember which month they are in. As the arrow keys move the current day, the screen reader follows it.

The calendar's name is the month it shows, such as "March 2027," and it changes with the month. That is why AddCalendar uses your text, such as "Tour day," as the calendar's description instead of its name. The month buttons say where they go, such as "Next month, April 2027."

A date field's name is the name you gave it, such as "Project due date." Its description is the date in words, such as "Friday, March 12, 2027," whatever the field's format is. So a short format such as "03/12/2027" is still read plainly. If the text isn't a date, the description is "Not a date" until it is fixed. The field keeps its description up to date itself, so don't set it.

The exact words depend on the screen reader. Nothing extra is spoken when a date is chosen; a person finds out about the change from the focused day or field, or by moving to the report label.

Colors

A calendar takes its colors from the current theme, so it changes in dark mode too. The Themes and Dark Mode tutorial shows how to change them with the theme key "Calendar."

Engage

In this activity, you build a sign-up form for a school club, for a made-up club member named Jill. Jill just moved from a country where "3/4" means April 3, and uses a keyboard instead of a mouse. First, decide what dates the club needs and how precise each one must be. For each piece of data, write one sentence: does it need the day, the time, or both? What would go wrong if you stored less? What would you have to store, and keep track of, if you stored more?

Then build the form. It needs at least:

  • a date field for the person's birthday, with a label;
  • a calendar for the first meeting, limited to the next two weeks;
  • a "Sign up" button.

When the button is pressed, output the meeting day in words. If the birthday field is empty or isn't a date, output a message that asks for a date such as "March 12, 2010." Try changing the birthday field's format to GetNumericFormat or GetISOFormat. Which format do you think is clearest for Jill, and why?

When it works, test the form the way Jill would: use only the keyboard, Tab, the arrow keys, Page Up, Page Down, Enter and Alt+Down. If you have a screen reader, turn it on and notice what it reports for the days and the field. Check your form against Jill's needs:

  • Can Jill pick the meeting day without a mouse?
  • Could Jill read any date on the form the wrong way?
  • If Jill types a date wrong, does the message say how to fix it?

Change one thing that would make the form easier for Jill to use.

Wrap up

A calendar shows a month to pick a day from, and a date field lets a person type a date or open a calendar. Both give you a CalendarDate, which you can read with GetDate, compare, count days with, and write in words. A CalendarListener reacts as soon as a date is chosen. Think back to the warm up: how does a form that shows dates as "March 12, 2027" and reads them aloud in full avoid the "3/4" problem? In the next tutorial, Lists, you will show a person a list of items to choose from.