To convert a DateTime
object to UTC in C#, you can use the ToUniversalTime()
method of the DateTime
struct.
Here's an example:
main.cs127 chars3 lines
In this example, the dateTime
object is the current local time, and the utcDateTime
object is the same time, converted to UTC.
Keep in mind that this method uses the system's time zone settings to perform the conversion. If you need to convert to a specific time zone, you can use the TimeZoneInfo
class, like this:
main.cs274 chars4 lines
In this example, timeZone
is a TimeZoneInfo
object that represents the Pacific Standard Time zone. The ConvertTimeToUtc()
method takes the local time (dateTime
) and the time zone (timeZone
) and returns the corresponding UTC time (convertedDateTime
).
gistlibby LogSnag