Skip to content

Commit 1634814

Browse files
authored
Merge pull request #49614 from wwlpublish/de351b99f8ea832924188d0da67ffa78e98666a953fb05b6e5d461a33431d017-live
Modules/M01-get-started-with-dates-times-time-zones
2 parents 6d39e27 + 930c3fb commit 1634814

18 files changed

+619
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.get-started-with-dates-times-time-zones.introduction
3+
title: Introduction to handling date and time in C#
4+
metadata:
5+
title: Introduction to handling date and time in C#
6+
description: "Learn how to handle date and time effectively in C# applications, focusing on scheduling and logging across time zones."
7+
ms.date: 03/24/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 2
12+
content: |
13+
[!include[](includes/1-introduction.md)]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.get-started-with-dates-times-time-zones.examine-csharp-date-time
3+
title: Examine C# classes for date, time, and time zones
4+
metadata:
5+
title: Examine C# classes for date, time, and time zones
6+
description: "Learn about the various classes and methods that allow you to represent, manipulate, and display date, time, and time zone values effectively."
7+
ms.date: 03/24/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 4
12+
content: |
13+
[!include[](includes/2-examine-csharp-date-time.md)]
14+
quiz:
15+
title: "Check your knowledge"
16+
questions:
17+
- content: "Which class in C# would be most suitable for calculating the length of events or breaks?"
18+
choices:
19+
- content: "The `DateTime` class"
20+
isCorrect: false
21+
explanation: "Incorrect. While the `DateTime` class can represent specific points in time, it isn't primarily used for representing durations or intervals."
22+
- content: "The `TimeZoneInfo` class"
23+
isCorrect: false
24+
explanation: "Incorrect. The `TimeZoneInfo` class is used for converting times between different time zones, not for representing durations or intervals."
25+
- content: "The `TimeSpan` class"
26+
isCorrect: true
27+
explanation: "Correct. The `TimeSpan` class represents durations or intervals, making it useful for calculating the length of events or breaks."
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.get-started-with-dates-times-time-zones.implement-date-time-classes
3+
title: Implement date and time classes in C#
4+
metadata:
5+
title: Implement date and time classes in C#
6+
description: "Learn about C# classes like DateOnly, TimeOnly, DateTime, DateTimeOffset, TimeZoneInfo, and TimeSpan for handling date and time."
7+
ms.date: 03/24/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 5
12+
content: |
13+
[!include[](includes/3-implement-date-time-classes.md)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.get-started-with-dates-times-time-zones.use-csharp-datetimeoffset-timezoneinfo-timespan-class-methods
3+
title: Use DateTimeOffset, TimeZoneInfo, and TimeSpan in C#
4+
metadata:
5+
title: Use DateTimeOffset, TimeZoneInfo, and TimeSpan in C#
6+
description: "Explore methods for handling date and time with offsets, time zone conversions, and intervals in .NET applications."
7+
ms.date: 03/24/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 6
12+
content: |
13+
[!include[](includes/4-use-csharp-datetimeoffset-timezoneinfo-timespan-class-methods.md)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.get-started-with-dates-times-time-zones.apply-csharp-dayofweek-cultureinfo-calendar-calendarweekrule-class-methods
3+
title: Apply DayOfWeek, CultureInfo, Calendar, and CalendarWeekRule in C#
4+
metadata:
5+
title: Apply DayOfWeek, CultureInfo, Calendar, and CalendarWeekRule in C#
6+
description: "Learn to handle culture-specific data, calendar operations, and day-of-week manipulations in .NET applications."
7+
ms.date: 03/24/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 7
12+
content: |
13+
[!include[](includes/5-apply-csharp-dayofweek-cultureinfo-calendar-calendarweekrule-class-methods.md)]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.get-started-with-dates-times-time-zones.exercise-example
3+
title: 'Exercise: Dates, times, and time zones in C#'
4+
metadata:
5+
title: 'Exercise: Dates, times, and time zones in C#'
6+
description: "Exercise: Implement dates, times and time zone solutions in using C#."
7+
ms.date: 03/24/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 30
12+
content: |
13+
[!include[](includes/6-exercise-example.md)]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.get-started-with-dates-times-time-zones.knowledge-check
3+
title: Knowledge check
4+
metadata:
5+
title: Knowledge check
6+
description: "Knowledge check"
7+
ms.date: 03/24/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 2
12+
content: |
13+
[!include[](includes/7-knowledge-check.md)]
14+
quiz:
15+
title: "Check your knowledge"
16+
questions:
17+
- content: "Which class in C# is used to represent a specific time of day without a date?"
18+
choices:
19+
- content: "DateOnly"
20+
isCorrect: false
21+
explanation: "Incorrect. The DateOnly class is used to represent dates without considering the time of day."
22+
- content: "TimeOnly"
23+
isCorrect: true
24+
explanation: "Correct. The TimeOnly class is designed to represent a specific time of day without a date."
25+
- content: "DateTime"
26+
isCorrect: false
27+
explanation: "Incorrect. The DateTime class represents both date and time, not just a specific time of day."
28+
- content: "Which class is used to schedule a meeting that accounts for different time zones?"
29+
choices:
30+
- content: "DateTime"
31+
isCorrect: false
32+
explanation: "Incorrect. While DateTime can represent date and time, it does not account for different time zones."
33+
- content: "DateTimeOffset"
34+
isCorrect: true
35+
explanation: "Correct. DateTimeOffset includes an offset from UTC, making it suitable for applications that need to account for different time zones."
36+
- content: "TimeSpan"
37+
isCorrect: false
38+
explanation: "Incorrect. TimeSpan represents durations or intervals, not specific points in time across different time zones."
39+
- content: "A programmer needs to calculate the duration of an event in C#. Which class should they use?"
40+
choices:
41+
- content: "TimeSpan"
42+
isCorrect: true
43+
explanation: "Correct. TimeSpan is used to represent durations or intervals, making it ideal for calculating the length of events."
44+
- content: "DateTime"
45+
isCorrect: false
46+
explanation: "Incorrect. DateTime represents specific points in time, not durations."
47+
- content: "DateTimeOffset"
48+
isCorrect: false
49+
explanation: "Incorrect. DateTimeOffset includes time zone information but is not used for representing durations."
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.get-started-with-dates-times-time-zones.summary
3+
title: Summary
4+
metadata:
5+
title: Summary
6+
description: "Summary"
7+
ms.date: 03/24/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 2
12+
content: |
13+
[!include[](includes/8-summary.md)]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
In this module, you learn how to handle date and time effectively in C# applications that focus on scheduling meetings and logging events across different time zones.
2+
3+
Imagine you're a programmer working for a global company that schedules meetings and logs events across various time zones. You need to ensure that meetings are scheduled accurately and events are logged correctly, regardless of the participants' locations. For instance, you might have a team member in New York and another in Tokyo, and you need to schedule a meeting that works for both. Additionally, you need to log the event times accurately for reporting purposes. Handling date and time in such scenarios can be challenging due to differences in time zones, daylight saving changes, and cultural-specific date formats. This module equips you with the knowledge and tools to manage these complexities effectively.
4+
5+
## Learning objectives
6+
7+
- Use `DateOnly`, `TimeOnly`, and `DateTime` classes to represent dates and times.
8+
- Perform time zone conversions and interval calculations using `DateTimeOffset`, `TimeZoneInfo`, and `TimeSpan`.
9+
- Retrieve and manipulate day of the week information using `DayOfWeek`.
10+
- Handle culture-specific data using `CultureInfo`.
11+
- Perform calendar-related operations using `Calendar` and define rules for determining the first week of the year using `CalendarWeekRule`.
12+
13+
## Prerequisites
14+
15+
- Visual Studio Code installed with the C# Dev Kit.
16+
- Basic knowledge of the Visual Studio Code IDE.
17+
- Basic understanding of the C# programming language.
18+
- Familiar with classes, abstract classes, interfaces and inheritance.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
3+
Imagine you're a programmer working on a part of a project that involves scheduling meetings and logging events across different time zones. To tackle your task, you need to understand how to handle date and time in C#. So you learn about the various classes and methods that allow you to represent, manipulate, and display date and time values effectively.
4+
5+
## DateOnly, TimeOnly, DateTime
6+
7+
These classes focus on basic date and time representation, suitable for common tasks like scheduling and logging.
8+
9+
- **`DateOnly`**: Use this class when you need to work with dates without considering the time of day, such as setting event dates.
10+
- **`TimeOnly`**: Ideal for scenarios where only the time is relevant, like scheduling daily sessions.
11+
- **`DateTime`**: The most versatile structure, combining both date and time, suitable for general scheduling tasks.
12+
13+
For these classes, you find methods that help with parsing, formatting, and comparing date and time values. For example, `DateTime` offers a wide range of methods for date and time manipulation, including addition, subtraction, and comparison.
14+
15+
Examine the following example to get started:
16+
17+
```csharp
18+
TimeOnly meetingTime = new TimeOnly(14, 30); // 2:30 PM
19+
Console.WriteLine($"Meeting Time: {meetingTime}");
20+
21+
// Example console output: Meeting Time: 14:30
22+
```
23+
24+
The example demonstrates how to create and display a `TimeOnly` object, which represents a specific time of day without a date. The constructor initializes the time using hours and minutes, and the `Console.WriteLine` method outputs it in a readable format.
25+
26+
## DateTimeOffset, TimeZoneInfo, TimeSpan
27+
28+
These classes handle more complex scenarios, such as time zone conversions and interval calculations, providing precision and flexibility.
29+
30+
- **`DateTimeOffset`**: Essential for applications that need to account for different time zones, as it includes an offset from UTC.
31+
- **`TimeZoneInfo`**: Allows conversion of times between different time zones, ensuring accurate scheduling.
32+
- **`TimeSpan`**: Represents durations or intervals, useful for calculating the length of events or breaks.
33+
34+
These classes come with methods for converting between time zones, adjusting offsets, and performing arithmetic on time intervals. For instance, `TimeSpan` offers methods for adding, subtracting, and comparing time intervals.
35+
36+
Examine the following example:
37+
38+
```csharp
39+
TimeSpan duration = new TimeSpan(2, 0, 0); // 2 hours
40+
Console.WriteLine($"Event Duration: {duration}");
41+
42+
// Example console output: Event Duration: 02:00:00
43+
```
44+
45+
The example shows how to create and display a `TimeSpan` object, illustrating the concept of time intervals. It initializes a `TimeSpan` with hours, minutes, and seconds, and outputs the duration in a readable format.
46+
47+
## DayOfWeek, CultureInfo, Calendar, CalendarWeekRule
48+
49+
These classes support culture-specific operations and specific date calculations, essential for internationalization, and calendar-based applications.
50+
51+
- **`DayOfWeek`**: An enumeration useful for determining or comparing specific days, aiding in weekly scheduling.
52+
- **`CultureInfo`**: Provides information about specific cultures, including date and time formatting rules, essential for globalized applications.
53+
- **`Calendar`**: Represents different calendar systems, allowing you to work with dates in various cultural contexts.
54+
- **`CalendarWeekRule`**: Defines how the first week of the year is determined, useful for applications relying on week-based calculations.
55+
56+
These classes offer methods for formatting and parsing date and time according to cultural norms, and for date calculations within specific calendar systems.
57+
58+
Examine the following example using `DayOfWeek`:
59+
60+
```csharp
61+
DayOfWeek today = DateTime.Now.DayOfWeek;
62+
Console.WriteLine($"Today is: {today}");
63+
64+
// Example console output: Today is: [DayOfWeek]
65+
```
66+
67+
The example demonstrates how to retrieve and display the current day of the week using the `DayOfWeek` enumeration. It accesses the `DayOfWeek` property from the current date and time, and outputs the result in a readable format.
68+
69+
By understanding `DateTimeOffset`, `TimeZoneInfo`, and `TimeSpan` classes and their basic methods, you begin to build a foundation for handling date and time in your applications. These concepts help ensure accurate scheduling and logging across different time zones.

0 commit comments

Comments
 (0)