开发者

TimeZoneInfo.ConvertTime doesn't Convert anything

Running asp.net mvc 2 on win 7 with .net 4.0

I have a controller action method that receives 2 DateTime objects from a form. The UI on the form uses the jQueryUi datepicker (not sure if that maters).

The user 开发者_StackOverflowwho fills out that form will ALWAYS be entering the date/time in Hawaiian time zone.

I want to convert that to UTC time and store it in a database.

When I call TimeZoneInfo.ConverTime(DateTime,TimeZoneInfo,TimeZoneInfo) it returns the exact same datetime as i passed into it without doing any conversion. I checked the debugger and the only thing that changed was it changed the the DateTime.Kind property to DateTimeKind.Utc.

public ActionResult New(ScheduleNew data){         
    TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById( "Hawaiian Standard Time" );

    DateTime start = TimeZoneInfo.ConvertTime(data.StartDate, tz, TimeZoneInfo.Utc);
    DateTime end = TimeZoneInfo.ConvertTime(data.EndDate, tz, TimeZoneInfo.Utc);
}

I've Tried an alternate version as well with the same results.

public ActionResult New(ScheduleNew data){
    DateTime start = new DateTime( data.StartDate.Year, data.StartDate.Month, data.StartDate.Day, data.StartDate.Hour, data.StartDate.Minute, data.StartDate.Second, DateTimeKind.Unspecified );
    DateTime end = new DateTime( data.EndDate.Year, data.EndDate.Month, data.EndDate.Day, data.EndDate.Hour, data.EndDate.Minute, data.EndDate.Second, DateTimeKind.Unspecified );

    TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById( "Hawaiian Standard Time" );

    StartDate = TimeZoneInfo.ConvertTime(start, tz, TimeZoneInfo.Utc);
    EndDate = TimeZoneInfo.ConvertTime(end, tz, TimeZoneInfo.Utc),
}

ScheduleData is just a simple ViewModel class with two date time Properties called StartDate and EndDate.

I want to emphasize, I don't care where the server is located, I don't care where the user is located. The user will always enter time in the Hawaiian time zone, and the server should always convert that datetime to to UTC.

Basically what I want, is to add 10 hours to the time the user enters (hawaiian to utc) and I could achieve that by just calling .AddHours(10) and it would be just fine. But later on down the road I will need this app to be more flexible.


If you want more flexible approach, try something like this:

 DateTime now = DateTime.UtcNow;

 TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time");
 TimeSpan utcOffset = timeZoneInfo.GetUtcOffset(now);
 DateTime hawaiianTime = new DateTime(now.Ticks + utcOffset.Ticks, DateTimeKind.Local);

This should work correctly no matter what time zone you are using. By the way: If I were you, I would create start and end dates as DateTimeKind.Utc (and store all schedule data as UTC as well). I am not sure how GetUtcOffset() will work with unspecified time kind...

Update on GetUtcOffset()

It seems that GetUtcOffset() works exactly the same for DateTimes created with different DateTimeKind values. However, I haven't test it with invalid input (i.e. with dates that does not exist due to daylight saving time switching). I believe DateTimeKind.Utc is fairly save here, but it might for Local or Unspecified.


I can't find any problems with you code... It looks like it should work. Have you tried the alternate of:

DateTime start = TimeZoneInfo.ConvertTimeToUtc(data.StartDat, tz);


try this code

var dt = new DateTime(YourOldDate.Ticks, DateTimeKind.Utc);
DateTime NewDate = TimeZoneInfo.ConvertTime(dt, TimeZoneInfo.Local);

This worked from me, may be you can give a try.


It appears to be that in the code you are not doing anything with the result of the conversion.

public ActionResult New(ScheduleNew data){         
    TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById( "Hawaiian Standard Time" );

    DateTime start = TimeZoneInfo.ConvertTime(data.StartDate, tz, TimeZoneInfo.Utc);
    DateTime end = TimeZoneInfo.ConvertTime(data.EndDate, tz, TimeZoneInfo.Utc);
}

The start and end values are set, but are never returned. Actually, no value is being returned at all here, so this shouldn't compile. Perhaps there is more to it that you didn't show.

At any rate, the conversion itself is just fine. As long as data.StartDate and data.EndDate have Unspecified kind, then it should work as expected. You could make it slightly cleaner though by using TimeZoneInfo.ConvertTimeToUtc instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜