0% found this document useful (0 votes)
4 views24 pages

Understanding DateTime in C#

The document provides an overview of DateTime in C#, covering its general concepts, formatting, and operations. It explains the significance of GMT, UTC, and local time zones, as well as how to initialize and manipulate DateTime objects. Additionally, it includes details on TimeSpan, DateTime conversion methods, and practical application requirements for displaying local time across different cities.

Uploaded by

daicalaca24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views24 pages

Understanding DateTime in C#

The document provides an overview of DateTime in C#, covering its general concepts, formatting, and operations. It explains the significance of GMT, UTC, and local time zones, as well as how to initialize and manipulate DateTime objects. Additionally, it includes details on TimeSpan, DateTime conversion methods, and practical application requirements for displaying local time across different cities.

Uploaded by

daicalaca24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

.

NET Programming
Language
DateTime in C#

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use


Agenda

1. DateTime in General

2. DateTime Format

3. TimeSpan in C#

4. DateTime Conversion

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 2


Lesson Objectives

This is a sample text that you can edit.


01 Understand DateTime in C#
You can change font(size, color, name),
or apply any desired formatting.

This is a sample text that you can edit.


03 You can change font(size, color, name),
or apply any desired formatting.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 3


Section 1

DateTime in General

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 4


DateTime in General
▪ Greenwich Mean Time (GMT):
• is the mean solar time at the Royal Observatory in Greenwich, London, reckoned
from midnight
▪ Coordinated Universal Time (UTC)
• is the primary time standard by which the world regulates clocks and time.
▪ TimeZone: defines local date and time
• Vietnam: UTC+7
• Singapore/Kuala Lumpur: UTC+8
▪ Unix (Epoch time):
• defined as the number of seconds from [Link] on 1 January 1970

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 5


DateTime in C#
▪ The DateTime value type represents dates and times with values:
✓ ranging from [Link] (midnight), January 1, 0001 Anno Domini (Common Era)

✓ through [Link] P.M., December 31, 9999 A.D. (C.E.) in the Gregorian calendar.

▪ Time values are measured in 100-nanosecond units called ticks.

▪ In practice, you should choose Date and Time or Date only format

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 6


Initializing a DateTime object

When we focus on Date only When the time is important

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 7


DateTime in C#
▪ DayOfWeek: gets the day of the week represented by this instance.
▪ DayOfYear: gets the day of the year represented by this instance
▪ TimeOfDay: gets the time of day for this instance.
▪ DaysInMonth: returns the number of days in the specified month and year.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 8


DateTime in C#
▪ Use Add<member> methods to Add (or Subtract) from current value
▪ Example:

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 9


Compare Two DateTime In C#
▪ Use Compare method to compare two DateTime objects.
✓ If result is 0, both objects are the same.
✓ If the result is less than 0, then the first DateTime is earlier;
✓ otherwise the first DateTime is later.

▪ Use CompareTo method compare current date with an other.


✓ If result is 0, both objects are the same.
✓ If the result is less than 0, then the first DateTime is earlier;
✓ otherwise the first DateTime is later.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 10


Compare Two DateTime In C#

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 11


Section 2

DateTime Format

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 12


DateTime format
▪ We use date time format to define the text representation of the date and
time value
▪ CultureInfo represents information about a specific culture.
▪ The information includes the names for the culture, the writing system, the
calendar used, the sort order of strings, and formatting for dates and
numbers.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 13


DateTime format
▪ In DateTime, it shows difference format, 26 Feb 2030
✓ en-US: 2/26/2030
✓ en-GB, fr-FR, vi-VN: 26/02/2030
✓ ja-JP: 2030/02/26

▪ Important: always check CultureInfo to get date or month


✓ Example: you got a datetime value as 07/05/1987
✓ Is it: 07-May or 05-Jul?

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 14


Standard date and time format strings
Format Description Examples
specifier
"d" Short date pattern. 2009-06-15T[Link] -> 6/15/2009 (en-
US)
"D" Long date pattern. 2009-06-15T[Link] -> Monday, June
15, 2009 (en-US)
"f" Full date/time pattern (short time). 2009-06-15T[Link] -> Monday, June
15, 2009 1:45 PM (en-US)
"F" Full date/time pattern (long time). 2009-06-15T[Link] -> Monday, June
15, 2009 [Link] PM (en-US)
"g" General date/time pattern (short time). 2009-06-15T[Link] -> 6/15/2009 1:45
PM (en-US)
"G" General date/time pattern (long time). 2009-06-15T[Link] -> 6/15/2009
[Link] PM (en-US)
"M", "m" Month/day pattern. 2009-06-15T[Link] -> June 15 (en-US)

"Y", "y" Year month pattern. 2009-06-15T[Link] -> June 2009 (en-
US)

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 15


Custom date and time format strings
▪ Rules:
✓ Use character for value. Example: y for year, d for day,
✓ M for month and m for minute
✓ H for 24-hour and h for 12-hour (following by tt mean AM/PM)
✓ Number of characters is length of value
▪ Practice to use some custom format:
✓ "d/M/yyyy"
✓ "dd/MM/yyyy"
✓ "dd/MMM/yyyy"
✓ "dd/MMMM/yyyy"
✓ "MMM d, yyyy"

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 16


TimeSpan
▪ TimeSpan represents a period of time and has many helpful methods.
▪ TimeSpan contains days, hours, minutes, seconds, miliseconds
▪ TimeSpan is used in DateTime operators

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 17


DateTime Operators
▪ [Link]:
✓ adds the value of the specified [Link]

▪ [Link]:
✓ Subtracts the specified date and time from this instance.

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 18


DateTime Operators

Operator DateTime TimeSpan


DateTime TimeSpan
+
TimeSpan TimeSpan

DateTime TimeSpan
-
TimeSpan DateTime TimeSpan

DateTime bool
==, !=, >, >=, <
<=
TimeSpan bool

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 19


Convert DateTime to string
▪ Use DateTime format CultureInfo
▪ Always check CultureInfo
▪ Be consider UTC time

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 20


Convert string to DateTime
▪ Always check format before convert
✓ From local machine
✓ From data example
✓ From requirement

▪ Use Parser methods


✓ [Link]
✓ [Link]
✓ TryParse
✓ TryParseExact

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 21


Practice Time
▪ Create application to allow display ▪ Technical requirement:
local time at difference cities ✓ Application accepts at least 3 formats
✓ User input date and time date and time
✓ User input local code ✓ Application accept list of countries in
format:
✓ User input list of countries
<country name 1>(<culture code 1>),
✓ Application shows local time at cities < country name 2>(< culture code
2>), < country name 3>(< culture
code 3>),
✓ You can use various for application
output

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 22


09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 23
THANK YOU!

09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use

You might also like