Danny
Danny

Reputation: 1003

Javascript Formatting UTC Date

Ok I have this exact date:

2012-10-01T13:00:00+0000

I need to split it into two variables: one is the date: 2012-10-01, and the other is the time in hours and minutes: 13:00

What I have so far produces 2012-09-12 & 14:00 - Completely the wrong date...

Here's the code:

var d, dd, hh, mi, mm, theDate, theTime, yyyy;

var myDate = "2012-10-01T13:00:00+0000"; //Date I need converting into two variables

d = new Date(myDate);

yyyy = d.getFullYear().toString();
mm = d.getMonth().toString();
dd = d.getDate().toString();
hh = d.getHours().toString();
mi = d.getMinutes().toString();


theDate = yyyy + "-" + (mm[1] ? mm : "0" + mm[0]) + "-" + (dd[1] ? dd : "0" + dd[0]);
theTime = (hh[1] ? hh : "0" + hh[0]) + ":" + (mi[1] ? mi : "0" + mi[0]);

//theDate produces: 2012-09-12, (should be 2012-10-01)
//theTime produces: 14:00, (should be 13:00)

Upvotes: 0

Views: 330

Answers (2)

Trevor
Trevor

Reputation: 6689

Add 1 to the month.

The time will adjust based on your local timezone. You are getting 14:00 because you are in GMT +1. Change time to 13:00:00+0100 to get the correct time.

Upvotes: 0

Martin
Martin

Reputation: 584

My guess is: month starts at 0 so +1 day takes the hour so your format is wrong. Yous should print d to see what you actually constructed. Apart from that try: formatting the date

Upvotes: 1

Related Questions