I'm trying to convert the convert time to the user's time zone, but I don't have the windows time zone string such as "Pacific Standard Time". All I have is a string offset such as "-07:00". Looks like I need to create a timespan. Is the only way to parse this string manually?. Seems like there should be a way to convert a time using a string offset, but maybe I am missing something.
I have this but it requires the timezone. I'm trying to modify it to use the offset instead, but you can see the timespan that is created for the conversion and I need to get my offset to the timespan.
static void Main(string[] args)
{
var currentTimeInPacificTime = ConvertUtcTimeToTimeZone(DateTime.UtcNow, "Pacific Standard Time");
//TimeSpan ts = new TimeSpan("-07:00");
Console.ReadKey();
}
static DateTimeOffset ConvertUtcTimeToTimeZone(DateTime dateTime, string toTimeZoneDesc)
{
if (dateTime.Kind != DateTimeKind.Utc) throw new Exception("dateTime needs to have Kind property set to Utc");
TimeSpan toUtcOffset = TimeZoneInfo.FindSystemTimeZoneById(toTimeZoneDesc).GetUtcOffset(dateTime);
var convertedTime = DateTime.SpecifyKind(dateTime.Add(toUtcOffset), DateTimeKind.Unspecified);
return new DateTimeOffset(convertedTime, toUtcOffset);
}
There are time zone strings which includes "Pacific Standard Time". The complete list can be found here. http://www.xiirus.net/articles/article-_net-convert-datetime-from-one-timezone-to-another-7e44y.aspx
Any DateTime object can be converted to some timezone -
So you method can be modified as -
You can just use the
TimeSpan.Parse
method:Or if you want be a little more safe, try the
TimeSpan.TryParse
method:But of course if all you want to do is convert a UTC date/time to a local date/time, you can just do this:
Or to convert it to any timezone:
For more complicated/non-standard formats you can also use
TimeSpan.ParseExact(String, String, IFormatProvider)
, where the second String is a Custom TimeSpan Format String.API information is available at msdn.microsoft.com, and is linked above.linked.
Then you don't have what you need to make the correct conversion. Read "Time Zone != Offset" in the timezone tag wiki.
It is important to understand that the
"Pacific Standard Time"
value is the.Id
for theTimeZoneInfo
object that is used for US Pacific Time. It covers both Pacific Standard Time (UTC-8) and Pacfic Daylight Time (UTC-7).Now you have what is commonly called the XY Problem. You shouldn't have any need to work with the offset by itself.
In your code, there is a call to
dateTime.Add(toUtcOffset)
. When doing time zone conversions, this is a code smell that you are doing it wrong. You should never have to manually add or subtract time just to manipulate time zones. That should be reserved for actually changing the moment in time you are talking about.What you should be doing is to collect a real time zone id from your user. Either
"Pacific Standard Time"
for use withTimeZoneInfo
, or"America/Los_Angeles"
for use with a TZDB implementation like Noda Time.If time zone conversions aren't important in your context, then you might just want to collect a full
DateTimeOffset
value such as2013-08-17T13:27:00.000-07:00
instead.