📅  最后修改于: 2023-12-03 14:59:40.946000             🧑  作者: Mango
In C#, a timestamp represents a specific point in time, typically measured in milliseconds or ticks since a specific starting point. Timestamps are often used for various purposes, such as recording event times, measuring execution times, or managing data synchronization.
This guide will provide an overview of working with timestamps in C# and cover the various concepts and methods related to timestamps.
In C#, there are two main types commonly used for representing timestamps:
DateTime - This type represents a timestamp as a date and time value. It provides various methods and properties for manipulating and formatting timestamps.
DateTimeOffset - This type represents a timestamp as a date and time value with an associated time zone offset. It is useful for working with timestamps across different time zones.
Both types offer similar functionality but differ in terms of their handling of time zones. For most scenarios, DateTime is sufficient. However, if you need to work with time zone offsets, DateTimeOffset is recommended.
To get the current timestamp, you can use the DateTime.Now
or DateTimeOffset.Now
properties. These properties return the current local date and time.
DateTime currentDateTime = DateTime.Now;
DateTimeOffset currentDateTimeOffset = DateTimeOffset.Now;
You can create custom timestamps using the DateTime
or DateTimeOffset
constructors and specify the desired date and time values. For example:
DateTime customDateTime = new DateTime(2021, 10, 1, 12, 30, 0);
DateTimeOffset customDateTimeOffset = new DateTimeOffset(customDateTime, TimeSpan.FromHours(2));
Timestamps can be formatted into various string representations using format specifiers. The most commonly used format specifiers include:
yyyy
: 4-digit yearMM
: 2-digit monthdd
: 2-digit dayHH
: 2-digit hour (24-hour format)mm
: 2-digit minutess
: 2-digit secondfff
: 3-digit millisecondsFor example, to format a timestamp in the yyyy-MM-dd HH:mm:ss
format, you can use the ToString
method with the desired format specifier:
DateTime currentDateTime = DateTime.Now;
string formattedDateTime = currentDateTime.ToString("yyyy-MM-dd HH:mm:ss");
You can perform arithmetic operations on timestamps to calculate time differences or add/subtract time intervals. The DateTime
and DateTimeOffset
types offer various methods for such calculations, including:
Add
: Adds a time interval to the timestamp.Subtract
: Subtracts a time interval from the timestamp, resulting in a TimeSpan
representing the time difference.AddTicks
, AddMilliseconds
, AddSeconds
, AddMinutes
, AddHours
, AddDays
, AddMonths
, AddYears
: Adds a specific amount of time to the timestamp.Add(TimeSpan)
: Adds a TimeSpan
object to the timestamp.Subtract(TimeSpan)
: Subtracts a TimeSpan
object from the timestamp.DateTime futureDateTime = currentDateTime.AddMinutes(30);
DateTime pastDateTime = currentDateTime.Subtract(TimeSpan.FromDays(1));
TimeSpan timeDifference = futureDateTime - currentDateTime;
In C#, timestamps play a crucial role in managing time-related operations. Understanding how to work with timestamps using the DateTime and DateTimeOffset types enables precise time tracking and management in your applications.