1

I am trying to convert datetime from the server in the form of JSON to something I can use in HighChart but I keep getting the wrong day.

var json = {"lastMinute":"2013-05-06 15:46:00"}; // GMT

var lastMinute = json.lastMinute;
var a = lastMinute.split(' ');
var d = a[0].split('-');
var t = a[1].split(':');
var date = new Date(d[0],d[1],d[2],t[0],t[1],t[2]);

// NEED TO CONVERT TO LOCALTIME

this outputs: Thu Jun 06 2013 15:46:00 GMT-0700 (PDT)

can someone put my out of my misery and tell me what stupidly simple thing I'm doing wrong?

3
  • 2
    One word: moment.js Commented May 6, 2013 at 18:02
  • I like moment.js but we are currently under KISS development. Commented May 6, 2013 at 18:14
  • 2
    I use moment.js (and jQuery and ..) because I believe in being lazy (and hence, KISS?). Part of KISS is letting others run into (and fix) the problems before you. I like to use the resulting APIs which abstract away problems. Commented May 6, 2013 at 18:49

3 Answers 3

1

in js, months start with 0 instead of 1...

var json = {"lastMinute":"2013-05-06 15:46:00"}; // GMT

var lastMinute = json.lastMinute;
var a = lastMinute.split(' ');
var d = a[0].split('-');
var t = a[1].split(':');
var date = new Date(d[0],d[1]-1,d[2],t[0],t[1],t[2]);
Sign up to request clarification or add additional context in comments.

1 Comment

damn - you are completely right and I knew this. See - I said it was stupidly simple.
0

You can use Datejs (https://code.google.com/p/datejs/wiki/APIDocumentation) library. It is very usefull and you have the metod parseExact:

Date.parseExact("10/15/2004", "M/d/yyyy");

4 Comments

Until the Date.js team can get their act together and produce a stable release, I wouldn't recommend using it. They are still in "Alpha-1" from 2007! Try moment.js instead.
It's so stable it hasn't been updated in two years. ;) link but I agree with you, they must remove alpha label!
I wish. They had some good ideas. Last code check-in was Nov 2008. If it works for you - fine, but no way would I put something into production when the authors themselves don't have the confidence to call it stable. Moment on the other hand, is much more mature and has online unit tests to prove it.
I'm trying to avoid using any libraries or external addons and stick with good old fashioned JavaScript.
0

For ultimate in parsing and formatting dates in JavaScript, use the moment.js library.

You can be explicit about UTC, and this works just fine in downlevel browsers.

var m = moment.utc('2013-05-06 15:46:00');

These values look like they are in ISO8601 format (without the T). So you should be ok with just the above code. But if you want to be even more precise, you could do:

var m = moment.utc('2013-05-06 15:46:00', 'YYYY-MM-DD HH:mm:ss');

I would recommend keeping it as a moment for as long as possible. Their formatting is superb. But if you need it back as a javascript date, just do:

var date = m.toDate();

6 Comments

the datetime in the json is coming from a database that updates for the last minute of data available (which is rounded). My next new task is converting it from SERVER GMT to local time using getTimezoneOffset() - which seems to be an hour off still.
@jbolanos - Be aware getTimezoneOffset is the opposite sign then you might expect. Also be aware of offset differences for daylight savings time. If you're still an hour off (with or without using momentjs), please update your original question or open a new one so we can see more detail. Thanks.
yeah - I did a variable to set DST true or false (will be manual) and an if to add 60 to getTimezoneOffset() if true.
@jbolanos - getTimezoneOffset will already be adjusted for DST, based on the value of the date. But it assumes the date is already local - not UTC. See how it gets complex quickly? Momentjs is your friend - really.
true - but the server time doesn't change for DST - its locked to GMT
|

Your Answer

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.