Home > Backend Development > C++ > How to Create a DateTime Object in a Specific Time Zone (e.g., PST) in C# Without Using Local or UTC?

How to Create a DateTime Object in a Specific Time Zone (e.g., PST) in C# Without Using Local or UTC?

Barbara Streisand
Release: 2025-01-26 04:16:14
Original
198 people have browsed it

How to Create a DateTime Object in a Specific Time Zone (e.g., PST) in C# Without Using Local or UTC?

Create the DateTime object in a specific time zone in C#

When testing the unit test involving time zone changes, the DateTime object must be created in a specific non -local time zone.

Question:

How to create DateTime objects with a specific time zone (such as PST) without relying on local time zones or UTC time zones?

Answer:

Datetime constructor can only allow the use of local time zones, UTC time zones or unspecified options. Therefore, we can use timezoneinfo. Solution:

In order to create DateTime objects with a specific time zone (such as PST), please consider achieving custom structure:

This structure allows you to create DateTime objects with a specific time zone and access its UTC time and local time. Use the expression body attribute to simplify the code.

The above is the detailed content of How to Create a DateTime Object in a Specific Time Zone (e.g., PST) in C# Without Using Local or UTC?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template