C# Dates
Store one calendar day and print it under the sum you already trust. Inside Pine.Main, keep int left = 10;, int right = 2;, and the format line that prints left=10 right=2 sum=12. Then build DateTime day = new DateTime(2026, 3, 5); and print Console.WriteLine("{0:yyyy-MM-dd}", day);. Run with dotnet run. The screen should show left=10 right=2 sum=12, then 2026-03-05. Leave random numbers for the next page. This lesson is a fixed day, not a number drawn by chance.
Quality and Wrap-up continues, so Pine, Add, left, and right stay. The formatting page put those three numbers into {0}, {1}, and {2}. A width such as {0,4} only added spaces. This page keeps the tight sum line, without a width, and adds a second print whose pattern is a date. If the open file still shows spaces before 10, you are on the width demo. Put the sum pattern back to left={0} right={1} sum={2} before you judge the date line. If the body of Add is still a minus, the first line ends in 8. A date pattern will not turn that into 12.
DateTime lives in System, and the file already has using System;. The constructor takes a year, a month, and a day. The month is a number from 1 to 12. March is 3, not 0 and not 2.
Build 5 March 2026
Open the Pine file. The class name stays Pine. Keep Add and the four /// lines with name="left" and name="right". Delete a leftover Go method, leftover x and y labels, and any ReadLine line. The program must not wait for typing.
Under the sum, store one day and print it with a pattern:
DateTime day = new DateTime(2026, 3, 5);
Console.WriteLine("{0:yyyy-MM-dd}", day);
The first argument of new DateTime is the year, 2026. The second is the month, 3, which is March. The third is the day of that month, 5. The time on this value is midnight, and the pattern below does not print the time.
yyyy is a four-digit year. MM is the month in two digits, so March becomes 03. dd is the day in two digits, so the fifth becomes 05. The hyphens are characters you typed. They are not slots. The colon in {0:yyyy-MM-dd} separates the index from that pattern. {0} alone would print the date in a local default shape, which can include a clock time and a different order. This page uses the pattern so the second line is exactly 2026-03-05.
using System;
public class Pine
{
/// <summary>Adds the two stored numbers.</summary>
/// <param name="left">The first number.</param>
/// <param name="right">The second number.</param>
/// <returns>The sum of left and right.</returns>
public static int Add(int left, int right)
{
return left + right;
}
public static void Main()
{
int left = 10;
int right = 2;
int sum = Add(left, right);
Console.WriteLine("left={0} right={1} sum={2}", left, right, sum);
DateTime day = new DateTime(2026, 3, 5);
Console.WriteLine("{0:yyyy-MM-dd}", day);
}
}
Two lines
Run dotnet run. The screen shows left=10 right=2 sum=12, then 2026-03-05. Walk Main once. It stores ten and two. Add returns twelve. The first WriteLine fills three number slots, the same call as the formatting page without a width. The next line builds a DateTime and fills one slot. The format after the colon turns that value into year, month, and day. Nothing in the date line changes sum.
Read 2026-03-05 against the constructor. 2026 is the year you passed. 03 is month 3 written with two digits. 05 is day 5 written with two digits. If the second line is 2026-3-5 with no zeros, the pattern used M and d instead of MM and dd. Put the double letters back. If the second line includes a time such as midnight, the call printed day with no pattern. Put {0:yyyy-MM-dd} back.
The first line is still the proof that the sum survived. Do not delete it to make the file look like a date-only sample. A reader of this course should see the numbers and the day on one run.
Month numbers start at one
March is month 3 because DateTime counts January as 1. A constructor written new DateTime(2026, 0, 5) does not mean January. Month 0 is outside 1 through 12, and the run stops before either line is the lesson you wanted. Month 13 stops the same way. Day 0 stops. Day 32 stops. The message is that the calendar cannot hold that combination. Fix the numbers. Do not catch the failure on this page.
February has 28 days in 2026, which is not a leap year. new DateTime(2026, 2, 29) stops. new DateTime(2024, 2, 29) is legal, because 2024 is a leap year. You do not need a leap day for the demo. You need to know the constructor refuses a day the month does not have.
Some other languages number months from zero, so March is 2 in those calendars. This file is C#. The 3 you type is March. If you copy a zero-based month into DateTime, you land on the previous month or you stop the run. Keep 3 for this March.
The parts come back as ints
day.Year is the int 2026. day.Month is the int 3, not the text 03. day.Day is the int 5, not 05. The zeros live in the format pattern, not in the stored parts. Printing day.Month with no pattern shows 3. Printing the whole value with MM shows 03. Use the property when you want the number for arithmetic. Use the pattern when you want the line a person reads.
day.AddDays(1) does not change day. It returns a new DateTime for the next day. If you ignore that return, day is still 5 March and the screen stays 2026-03-05. To move forward you store the result: day = day.AddDays(1);. This page does not do that store. The checked run keeps 5 March so the second line stays put.
You can pass day.Year into a later calculation the way you pass left into Add. They are both ints. A DateTime is not an int. You cannot add day + 1 and get the next day. The method AddDays is the addition for a date.
Now moves
DateTime.Now is the clock on the machine at the moment the line runs. DateTime.Today is that calendar day at midnight. Both change when the day changes. A lesson that asks you to match a fixed screen cannot use them as the value under test. This file uses new DateTime(2026, 3, 5) so the second line is 2026-03-05 every time you run it.
You may print DateTime.Now in a scratch file when you want the live clock. Take that line out before you compare the screen with this page. If the second line is today's date instead of 2026-03-05, the constructor is not the one in the demo. Put new DateTime(2026, 3, 5) back.
The pattern still matters on a live clock. {0:yyyy-MM-dd} on DateTime.Now prints today's year, month, and day, and hides the time. {0} on DateTime.Now may show the time as well. Same rule as the fixed day: the letters after the colon choose the shape.
A bad pattern is not a bad sum
If the date format is broken, the date line fails and you may never see 2026-03-05. An unknown letter in the pattern, or a brace you forgot to close, stops that print. The sum line above it has already run, so left=10 right=2 sum=12 can be on the screen while the program then stops. Read the failing line. Fix the pattern. Do not edit Add because the date line failed.
MM and mm are different. MM is the month. mm is the minutes. On a midnight value, mm prints 00. If the line looks like a year, then zeros, then a day, you may have asked for minutes. Change mm back to MM. dd is the day of the month. DD is not the day pattern this page uses.
Keep one Pine. Keep the sum format without a width. Keep the date constructor on three arguments: year, month, day. Do not pass a string into new DateTime on this page. The three ints are the clear form.
Random stays shut
You can print the sum line and then 2026-03-05. The first line is left, right, and Add. The second line is a day you chose. Drawing a number you did not choose is the next page, random. This page stops before that job. A random call does not build 5 March 2026, and a DateTime does not roll a die.
Keep this file as the one you run. Keep month 3 and day 5. Keep {0:yyyy-MM-dd}. Keep DateTime.Now out of the checked run. When dotnet run shows the sum line and then 2026-03-05, this lesson did its job.
One last pass: one Pine, the tight sum format, one new DateTime(2026, 3, 5), no width on the numbers, no ReadLine, and no random call. Run dotnet run once more. If both lines match, the day and the sum agree, and the next topic can pick a number by chance.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top