Ben Narube
Ben Narube

Reputation: 448

Converting a DateTime.Now in an MVC 4 application to My Country's Standard Time (when application is deployed in the US)

I've developed an MVC 4 with C# application which I've deployed using Windows Azure to their datacenter in Virginia. I log user activity in my application recording both the username and the time that an activity was initiated. The time recorded needs to be converted to Fiji Standard Time. Based on what I have found online I wrote an extension method to help achieve this functionality.

public static DateTime ToFijiTime(this DateTime datetime)
{

        datetime = DateTime.SpecifyKind(datetime, DateTimeKind.Utc);
        TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Fiji Standard Time");
        DateTime MyDateTime = TimeZoneInfo.ConvertTime(datetime, tz);
        return TimeZoneInfo.ConvertTimeToUtc(MyDateTime, tz);        
}

However this doesn't really work well once it's deployed. I get the right date and almost the right time. For some reason it always gives the time as an "am" time. For example if the time should be 2/04/2013 6:30:19 PM (Fiji Standard Time)The time recorded is actually 2/04/2013 6:30:19 AM (Fiji Standard Time) but times that are in the morning like2/04/2013 1:30:19 AM are saved correctly. Can anyone please tell me what's wrong with my code?

Upvotes: 1

Views: 770

Answers (1)

Freelancer
Freelancer

Reputation: 9074

Store the users timezone server side then,

Use following:

@TimeZoneInfo.ConvertTimeFromUtc(Model.CreatedOn, TimeZoneInfo.FindSystemTimeZoneById("E. US Standard Time"))

Also refer this :

Convert UTC/GMT time to local time

Upvotes: 1

Related Questions