Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a time zone (or a Kind) of a DateTime value?

I mean to store strict UTC time in a DateTime variable and output it in ISO 8601 format.

To do the last I've used .ToString("yyyy-MM-ddTHH:mm:sszzz"), and it has uncovered that the time zone is UTC+01:00.

I've tried to use .Kind = DateTimeKind.Utc, but it says the Kind property has no setter.

How do I explicitly specify the time is in UTC? How is the Kind property set?

like image 402
Ivan Avatar asked Jun 04 '11 23:06

Ivan


People also ask

How do you use timezone in DateTime?

Timezone aware object using datetime now(). time() function of datetime module. Then we will replace the value of the timezone in the tzinfo class of the object using the replace() function. After that convert the date value into ISO 8601 format using the isoformat() method.

How do I get the timezone from DateTime?

How to Get the Current Time of a Timezone with datetime. You can get the current time in a particular timezone by using the datetime module with another module called pytz . You can then check for all available timezones with the snippet below: from datetime import datetime import pytz zones = pytz.

How do I set DateTime kind to UTC?

SpecifyKind() Method in C# This method is used to create a new DateTime object which has the same number of ticks as the specified DateTime but is designated as either local time, Coordinated Universal Time (UTC), or neither, as indicated by the specified DateTimeKind value.


2 Answers

If you want to get advantage of your local machine timezone you can use myDateTime.ToUniversalTime() to get the UTC time from your local time or myDateTime.ToLocalTime() to convert the UTC time to the local machine's time.

// convert UTC time from the database to the machine's time DateTime databaseUtcTime = new DateTime(2011,6,5,10,15,00); var localTime = databaseUtcTime.ToLocalTime();  // convert local time to UTC for database save var databaseUtcTime = localTime.ToUniversalTime(); 

If you need to convert time from/to other timezones, you may use TimeZoneInfo.ConvertTime() or TimeZoneInfo.ConvertTimeFromUtc().

// convert UTC time from the database to japanese time DateTime databaseUtcTime = new DateTime(2011,6,5,10,15,00); var japaneseTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time"); var japaneseTime = TimeZoneInfo.ConvertTimeFromUtc(databaseUtcTime, japaneseTimeZone);  // convert japanese time to UTC for database save var databaseUtcTime = TimeZoneInfo.ConvertTimeToUtc(japaneseTime, japaneseTimeZone); 

List of available timezones

TimeZoneInfo class on MSDN

like image 80
SandRock Avatar answered Oct 02 '22 17:10

SandRock


While the DateTime.Kind property does not have a setter, the static method DateTime.SpecifyKind creates a DateTime instance with a specified value for Kind.

Altenatively there are several DateTime constructor overloads that take a DateTimeKind parameter

like image 22
Frank Boyne Avatar answered Oct 02 '22 15:10

Frank Boyne