0

I'm using ASP.NET MVC, and I wish to store all my DateTime values on the server in UTC format. And I wish for all transmission of DateTime values to be in UTC format. But I want to display DateTimes in the browser in local time. I've been getting confused and having trouble making it work. Below is my process....

In my UI, the user is able to enter a date which I compose into a string and use to create a Date object. It ends up looking like this:

var dt = new Date("3/23/2012 8:00 AM");

So the user intends to create a Date for 8 AM their time. Now I wish to send this to the server in UTC format so I have this method:

Date.prototype.toUTC = function ()
{
 var self = this;
 return new Date(self.getUTCFullYear(), self.getUTCMonth(), self.getUTCDate(), self.getUTCHours(), self.getUTCMinutes());
};

Which I use like so:

data.startDt = dt.toUTC(); //Data is the object being set to the server

Then I make an Ajax call using jQuery to send the data object to the server. On the server when I debug, and examine the data that comes in I see StartDt (which is mapped to a .NET DateTime object) as being {3/23/2012 12:00:00 PM}.

This is the value I store in my database. I'm not totally certain it is correct though.

Both the client and server are located in the Eastern United States (UTC-05:00).

Now, when I send this date back to the client in JSON format .NET sends this:

"/Date(1332518400000)/"

In JavaScript I parse it this way:

var dt = new Date(parseInt(serverDt.substr(6))); //parseInt ingnores last /

My thinking is that dt is a UTC Date, but that I can display it in in local format by calling toShortTime() as shown below:

Date.prototype.get12Hour = function ()
{
 var h = this.getHours();
 if (h > 12) { h -= 12; }
 if (h == 0) { h = 12; }
 return h;
};
Date.prototype.getAMPM = function ()
{
 return (this.getHours() < 12) ? "AM" : "PM";
};
Date.prototype.toShortTime = function ()
{
 return this.get12Hour() + ":" + this.getMinutes() + " " + this.getAMPM();
};

But that doesn't give me the 8:00 AM back that I want. It gives me the 12:00 PM. Where am I going wrong?

Peter Mortensen
31.6k22 gold badges110 silver badges134 bronze badges
asked Apr 23, 2012 at 15:43
3
  • 1
    In your code, dt is a UTC time. You need to convert that from UTC to local time. See stackoverflow.com/questions/3741348/… Commented Apr 23, 2012 at 15:57
  • I think you may be correct. If I run this new Date(Date.UTC(dt.getFullYear(), dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes())); I do get my 8AM back. Commented Apr 23, 2012 at 16:16
  • If you write it as an answer I'll give you credit for it. Commented Apr 24, 2012 at 3:22

3 Answers 3

1

In your code, dt is a UTC time. You need to convert that from UTC to local time.
See Javascript: Convert a UTC Date() object to the local timezone

answered Apr 24, 2012 at 3:50
0

Are you constructing the .NET DateTime object with the appropriate DateTimeKind value? You're sending a UTC-relative value to the server, which I'm guessing is storing the value as an EDT-relative time instead of a UTC-relative time, hence the incorrect value. As you stated, 1332518400000 is 12PM EDT, not UTC, which points to a transcription problem on the server:

> new Date(1332518400000)
Fri Mar 23 2012 12:00:00 GMT-0400 (Eastern Daylight Time)
answered Apr 23, 2012 at 16:02
2
  • 1
    Well, the data is passed to an MVC controller and the ModelBinder is actually creating the DateTime object. This is what the client sends: 2012年03月23日T16:00:00.000Z Commented Apr 23, 2012 at 16:15
  • I have to admit, I am confused how 8:AM gets sent as 16:00, but I do think the Z at the end means UTC. Commented Apr 23, 2012 at 16:24
0

This function works beautifully for me.

function ParseDateForSave(dateValue) {
 // create a new date object
 var newDate = new Date(parseInt(dateValue.substr(6)));
 // return the UTC version of the date
 return newDate.toISOString();
}
answered Apr 8, 2014 at 17:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.