I have the following ASP.Net MVC Controller method:
public ActionResult DoSomething(DateTime utcDate)
{
var localTime = utcDate.ToLocalTime();
}
The problem is that localTime will have the exact same value as utcDate. I assume this is because utcDate doesn’t know it has a UTC value. So my question is how can I convert utcDate (which I KNOW is UTC) into local?
If you know the
DateTimecontains a UTC value, you can use the following:For example, in my current application, I create timestamps in my database with SQL’s
utcnow, but when I read them into my C# application theKindproeprty is alwaysUnknown. I created a wrapper function to read the timestamp, deliberately set itsKindtoUtc, and then convert it to local time – essentially as above.Note that
DateTime.ToLocalTime()only doesn’t affect the value if one (or both) of the following holds:DateTime‘sKindproperty isDateTimeKind.LocalI think we can assume the second point isn’t true. Thus it seems that
iKnowThisIsUtc‘sKindproperty is set toLocalalready. You need to figure out why whatever is supplying you with theseDateTimes thinks they are local.