0

I have a DateTime value stored in an SQL Server DB with a value like '2016-04-13 00:00:00.0000000'. My problem is that I want to display this value on a HTML page as is without the browser or my server adjusting for Time Zone offsets.

I make an Ajax request to get my data to an MVC controller. I can see that the value of the Date in my ViewModel is correct. When it gets to the browser depending on the machines timezone the Date is offset.

All I want is that if I have a value 2015-04-13 00:00 in my database then that exact value gets displayed on my interface regardless of timezones, locales etc.

My Ajax request returns a value like "/Date(1460502000000)/". I've tried the following conversions:

new Date(datavar);
Date(datevar)
new Date(parseInt(datevar.replace('/Date(', '')))

I've also tried using moment.js like the following:

moment.utc(datevar).format();
moment(datevar).tostring()

An several others all of which try to offset the date some way or other.

The suggested linked answer is different in that it will include the timezone offset. So a client on US Central Time a value of "/Date(1460505600000)/" will be converted to 'Tue Apr 12 2016 18:00 GMT-0500" while a client on GMT will get the same value converted to "Wed Apr 13 2016 00:00:00 GMT+0100"

keitn
  • 1,288
  • 2
  • 19
  • 43
  • Possible duplicate of [How to format a Microsoft JSON date?](http://stackoverflow.com/questions/206384/how-to-format-a-microsoft-json-date) –  Apr 13 '16 at 13:30
  • I've added a section at the bottom of my post to explain why it is different. – keitn Apr 13 '16 at 13:46
  • Can't you just use a string rather than a Date when you get it from the database? – dibs487 Apr 13 '16 at 14:16
  • If I have to then yes, the problem is I use third party components such as a grid which offer filtering, sorting etc. If my dates are strings then this won't work. I also have date slider controls which require a date. I know I can send everything back from the server as a string and build up convert each to a date but I'd prefer not to have that level of overhead. – keitn Apr 13 '16 at 14:20
  • @keitn, The dupe does answer your question, but you should be using UTC dates –  Apr 13 '16 at 22:25

0 Answers0