8. DateTime, Regex & Utilities
Part 1: DateTime & Time Management
1. DateTime Struct
Creating DateTime
// Constructor
DateTime dt1 = new DateTime(2024, 12, 17); // Date only
DateTime dt2 = new DateTime(2024, 12, 17, 14, 30, 0); // Date and time
DateTime dt3 = new DateTime(2024, 12, 17, 14, 30, 0, 500); // With milliseconds
// Current date/time
DateTime now = DateTime.Now; // Local time
DateTime utcNow = DateTime.UtcNow; // UTC time
DateTime today = DateTime.Today; // Today at midnight (00:00:00)
// Parsing
DateTime dt4 = DateTime.Parse("12/17/2024");
DateTime dt5 = DateTime.Parse("2024-12-17T14:30:00");
// Safe parsing
if (DateTime.TryParse("12/17/2024", out DateTime result))
{
Console.WriteLine(result);
}
// Exact format
DateTime dt6 = DateTime.ParseExact(
"17-12-2024",
"dd-MM-yyyy",
CultureInfo.InvariantCulture);
// Try parse exact
if (DateTime.TryParseExact(
"17-12-2024",
"dd-MM-yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out DateTime exactResult))
{
Console.WriteLine(exactResult);
}
// Min and Max values
DateTime min = DateTime.MinValue; // 01/01/0001 00:00:00
DateTime max = DateTime.MaxValue; // 12/31/9999 23:59:59DateTime Properties
DateTime Methods
DateTime Quick Reference Table
Property/Method
Description
Example
2. DateTimeOffset (C# 2.0+)
Feature
DateTime
DateTimeOffset
3. DateOnly & TimeOnly (.NET 6.0+)
4. TimeSpan Struct
Creating TimeSpan
TimeSpan Properties
TimeSpan Operations
5. DateTime Formatting
Standard Format Strings
Standard Format Strings Table
Format
Name
Example Output
Custom Format Strings
Common Scenarios
Part 2: Regular Expressions
6. Regex Class (System.Text.RegularExpressions)
7. Character Classes
Character Classes Reference
Pattern
Matches
Example
8. Quantifiers
Quantifiers Reference
Quantifier
Matches
Example
9. Anchors
10. Groups
11. Alternation
12. Regex Methods
13. RegexOptions Enum
14. Common Regex Patterns
Common Patterns Reference
Pattern Type
Regex
Matches
Part 3: Math & Random
15. Math Class
Math Methods Reference
Method
Description
Example
16. Rounding Comparison
Rounding Behavior Table
Value
ToEven
AwayFromZero
ToZero
Floor
Ceiling
17. Random Class
18. Guid Struct
Part 4: String Utilities
19. String.Format() and Composite Formatting
20. StringBuilder (Brief Reference)
21. String Comparison
StringComparison Options
Option
Case-Sensitive
Culture-Specific
Use When
Quick Reference Summary
DateTime
TimeSpan
Regex
Math
Random
String Utilities
Last updated