Unity 5.5 Obsolete Particle System Code

How to convert DateTime in Specific timezone?

The .NET framework already has classes and methods available to convert DateTimes between different time zones. Have a look at the ConvertTime methods of the TimeZoneInfo class.

Edit: When you get the time to put into the database, assuming it was created with correct time zone information you can easily convert to UTC:

DateTime utcTime = inputDateTime.ToUniversalTime();

Get timeInfo as done in the question edit:

TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById(GetFromConfig.ManilaTimeZoneKey());

When you send the database time to user, convert it to the correct timezone using timeInfo.

DateTime userTime = TimeZoneInfo.ConvertTimeFromUtc(dbDateTime, timeInfo);

Personally I'd try and keep this logic separate from the propery get/set methods.

C# Datetimes: Conversion for different time zones

Use the TimeZoneInfo Class to convert a local time to a time in an alternative timezone:

TimeZoneInfo est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime targetTime = TimeZoneInfo.ConvertTime(timeToConvert, est);

Converting time zones in C#

Sample code that uses the version of TimeZoneInfo.ConvertTime which expects both a source and target timezone.

DateTime sourceTime = new DateTime(2015, 6, 10, 10, 20, 30, DateTimeKind.Unspecified);

TimeZoneInfo sourceTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");

foreach(var targetTimeZoneID in new string[] { "Pacific Standard Time", "Israel Standard Time", "Central European Standard Time" })
{
TimeZoneInfo targetTimeZone = TimeZoneInfo.FindSystemTimeZoneById(targetTimeZoneID);
var converted = TimeZoneInfo.ConvertTime(sourceTime, sourceTimeZone, targetTimeZone);

Console.WriteLine("{0}: {1:yyyy-MM-dd HH:mm:ss}", targetTimeZoneID, converted);
}
Console.ReadLine();

Output is:

Pacific Standard Time: 2015-06-10 10:20:30
Israel Standard Time:
2015-06-10 20:20:30
Central European Standard Time: 2015-06-10
19:20:30

Convert current local time to any other timezone

Ok, as I mentoined in comments, if you want to get timezones which differs from yours, you can do it in that way:

var zone = TimeZoneInfo.GetSystemTimeZones()
.Where(x=>x.BaseUtcOffset != TimeZoneInfo.Local.BaseUtcOffset)
.First();

To convert UTC DateTime to another timezone, you have to use TimeZoneInfo.ConvertTimeFromUtc, sample:

var datetime = // datetime in UTC, for example, DateTime.UtcNow
var zone = // target zone
return TimeZoneInfo.ConvertTimeFromUtc(datetime, zone);

You can check samples in my pet project here

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

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

Converting Times Between Time Zones in C#

The following code solves my problem-

DateTime utcTime = DateTime.UtcNow;
TimeZoneInfo BdZone = TimeZoneInfo.FindSystemTimeZoneById("Bangladesh Standard Time");
DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, BdZone);

Converting DateTime.Now To A Different Time Zone

Thanks for clarifying your question.

If the DateTime instance Kind is Local, then TimeZoneInfo.ConvertTime will expect the second parameter to be the local timezone of your computer.

If DateTime instance Kind is Utc, then TimeZoneInfo.ConvertTime will expect the second parameter to be the Utc timezone.

You need to convert outageEndDate to the right timezone first, just in case the localProvice timezone doesn't match the timezone on your computer.

outageEndDate = TimeZoneInfo.ConvertTime(outageEndDate, localTime);


Related Topics



Leave a reply



Submit