0

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)
1
  • Use getUTCFullYear, getUTCMonth, getUTCDate, getUTCHours and getUTCMinutes. And remember that month is always 0-11. Commented Sep 12, 2012 at 12:35

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.