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.
Asked
Active
Viewed 302 times
0
-
3Hi, and welcome to Stack Overflow. Unfortunately, while you have stated some general requirements, it is difficult to tell what you are actually *asking* about. Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). Also, I think you'll find that many questions of this nature have been asked and answered already, so be sure to start by searching. Good luck. :) – Matt Johnson-Pint Feb 12 '19 at 04:58
-
How are you passing the dates from asp.net to the browser? Are you using `date.ToString()` or json serialization? What was your expected outcome vs the current outcome. – cleftheris Feb 12 '19 at 08:54
1 Answers
1
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.

cleftheris
- 4,626
- 38
- 55