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?
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 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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With