-
Notifications
You must be signed in to change notification settings - Fork 5
Working with Dates
Sann Lynn Htun edited this page Nov 19, 2024
·
1 revision
Working with dates and times in C# is facilitated by the
DateTime
andDateTimeOffset
structures, along with methods that allow for date and time manipulation, formatting, and parsing.
- DateTime Structure
Represents dates and times and provides properties and methods for date and time manipulation.
using System;
class Program
{
static void Main()
{
DateTime now = DateTime.Now; // Current date and time
DateTime specificDate = new DateTime(2024, 12, 25); // Christmas 2024
DateTime parsedDate = DateTime.Parse("2024-11-19"); // Parsing a date
Console.WriteLine($"Now: {now}");
Console.WriteLine($"Specific Date: {specificDate}");
Console.WriteLine($"Parsed Date: {parsedDate}");
}
}
- Date and Time Formatting
Format dates and times using standard and custom format strings.
using System;
class Program
{
static void Main()
{
DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("yyyy-MM-dd")); // Standard format
Console.WriteLine(now.ToString("dddd, dd MMMM yyyy")); // Custom format
}
}
- Date and Time Arithmetic
Perform operations like adding or subtracting dates and times.
using System;
class Program
{
static void Main()
{
DateTime today = DateTime.Today;
DateTime nextWeek = today.AddDays(7); // Add 7 days
Console.WriteLine($"Today: {today.ToShortDateString()}");
Console.WriteLine($"Next Week: {nextWeek.ToShortDateString()}");
}
}
- DateTimeOffset Structure
Represents dates and times with an offset from UTC, useful for dealing with time zones.
using System;
class Program
{
static void Main()
{
DateTimeOffset now = DateTimeOffset.Now;
Console.WriteLine(now);
DateTimeOffset utcNow = DateTimeOffset.UtcNow;
Console.WriteLine(utcNow);
}
}
- Parsing and Validating Dates
Parse strings to DateTime and validate date formats.
using System;
class Program
{
static void Main()
{
if (DateTime.TryParse("2024-11-19", out DateTime parsedDate))
{
Console.WriteLine($"Parsed Date: {parsedDate}");
}
else
{
Console.WriteLine("Invalid date format.");
}
}
}
- DateTime: Basic structure for representing dates and times.
- DateTimeOffset: For handling dates and times with time zone offsets.
- Formatting: Use standard and custom formats for date and time representation.
- Arithmetic: Perform operations like adding or subtracting time spans.
- Parsing and Validation: Convert strings to DateTime and validate date formats.
Working with dates in C# allows for robust handling of time-related data, critical for applications ranging from scheduling to logging.
-
Introduction to C#
What is C#? -
Variables and Data Types
Understand how to declare and use variables. -
Operators
Arithmetic, relational, and logical operators in C#. -
Control Statements
If-else, switch, and looping constructs.
-
Classes and Objects
Basics of OOP in C#. -
Inheritance
How to use inheritance effectively. -
Polymorphism
Method overriding and overloading. -
Encapsulation
Understanding access modifiers.
-
LINQ Basics
Working with Language Integrated Query. -
Asynchronous Programming
Introduction to async/await. -
File Handling
Reading and writing files.
-
Number Formatting
Formatting numbers in C#. -
Exception Handling
Handling runtime errors effectively. -
Working with Dates
DateTime and time zone handling. -
Using Keyword in C#
Different usages of theusing
keyword in C#.
-
Setting Up Development Environment
Installing Visual Studio and .NET SDK. - Useful Libraries Libraries and tools for C# development.