Convert Local TimeZone to PST in Windows Phone

I need to Convert my Local TimeZone to PST in Windows Phone, do we have an API to do it? If not then what is the best practice here? Note the phone can be in any timezone based on where it is located in the world.

Jon Skeet
people
quotationmark

Assuming you mean Pacific Time, you can use my Noda Time project for this, assuming that DateTime works correctly in terms of the the local conversion.

// Initial conversion using BCL classes
DateTime local = ...;
DateTime utc = local.ToUniversalTime();

// The rest in Noda Time
Instant instant = Instant.FromDateTimeUtc(utc);
DateTimeZone zone = DateTimeZoneProviders.Tzdb["America/Los_Angeles"];
ZonedDateTime pacificTime = instant.InZone(zone);

From the ZonedDateTime you can get the LocalDateTime, or just keep it as a ZonedDateTime. (You can convert back to a DateTime as well, with ToDateTimeUnspecified().)

You can ask Noda Time to do both parts of the conversion, but because the PCL version of TimeZoneInfo doesn't provide the Id property, we have to perform some heuristics to guess the time zone - using DateTime to do the first conversion may be a little more reliable (although it doesn't give you as much control over how to handle DST transitions).

If you only need the current time, you don't need the BCL types at all. You can go for Noda Time all the way:

// You'd normally inject this, so that you can test different scenarios. Noda Time
// provides a Testing package which includes a fake implementation of IClock.
// Use SystemClock.Instance for production use.
IClock clock = ...;
DateTimeZone zone = DateTimeZoneProviders.Tzdb["America/Los_Angeles"];
ZonedDateTime pacificTime = clock.Now.InZone(zone);

In Noda Time 2.0, we're introducing ZonedClock which will make things slightly easier, too - you'll be able to create a ZonedClock for the Pacific time zone, and then ask it for the current ZonedDateTime or the current LocalDateTime.

people

See more on this question at Stackoverflow