1

I am trying to get the year, month, date, hour, and minutes from a datetime in Javascript. This is my code:

var newdatestring=document.getElementById("2012-12-11 10:36").value;
var newdate=new Date(newdatestring);

newdate.setMinutes ( newdate.getMinutes() + 30 );
var formdate=newdate.getYear()+"-"+(newdate.getMonth()+1)+"-"+newdate.getDate()+" "+newdate.getHours()+":"+newdate.getMinutes();

alert(formdate);

this code working fine in Chrome and Opera browsers, but it doesn't work in IE or Firefox. In those browsers, it shows something like Nan-Nan-Nan Nan:Nan.

5
  • 5
    Maybe your problem is in the html... That doesn't look like a valid id, there shouldn't be any spaces or start with a number... Commented Dec 15, 2012 at 5:41
  • 1
    Why do you have an HTML element id named after a specific time? Are you using dynamic html forms or is that a static date? Commented Dec 15, 2012 at 5:42
  • 6
    "Nan-Nan-Nan Nan:Nan ... batman!" Commented Dec 15, 2012 at 5:44
  • 1
    ^^ WAT!? Commented Dec 15, 2012 at 5:45
  • @elclanrs I'm glad you've recognized the most important part of the talk heh. Commented Dec 15, 2012 at 5:49

1 Answer 1

1

try this

var month = newdate.getUTCMonth();
var day = newdate.getUTCDate();
var year = newdate.getUTCFullYear();

when newdate is the date object

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

Comments

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.