Reputation: 1
I am developing an application using ReactJS and ASP.NET. Here I need to display the time a record is updated, the time different between now and the last updated time. Once it is in stage server, as the time in stage and local is different, it gives me an incorrect time as it is comparing the time in the server. I am using DateTime.UTCNow in the server to store the updated time.
Upvotes: 0
Views: 576
Reputation: 4849
The c# DateTime.UtcNow
gives a snapshot date and time as it would be in Coordinated Universal Time. When this date is passed to the browser to be used by JavaScript it is usually represented by a string in the ISO 8601 format which looks like this "2007-03-01T13:00:00Z"
.
Then in the browser it can will be converted to the Date
type and displayed like so.
var date = new Date("2007-03-01T13:00:00Z");
var dateTextInLocalTime = date.date.toLocaleString();
It is normal that the dateTextInLocalTime be shifted in comparison to the Utc counterpart seen on the server since it is converted to your browsers local time.
Upvotes: 1