Link to home
Create AccountLog in
Avatar of taborrg
taborrg

asked on

Javascript need to format a date

Hi,

This is an example of the return I get for a date - 11/7/112.
I need to format it like 07/NOV/2012.
Also is that year "112" right?

Here's the code:

var d = new Date(document.getElementById('check_in').value);
      d.setDate(d.getDate()+ tmpNumDays); //days to add
      document.getElementById('check_out_date').value= (d.getMonth()+1)+"/"+d.getDate()+"/"+d.getYear();


Thanks
Avatar of Gary
Gary
Flag of Ireland image

112 is not right


months[1] = "Jan";
months[2] = "Feb";
months[3] = "Mar";
months[4] = "Apr";
months[5] = "May";
months[6] = "Jun";
months[7] = "Jul";
months[8] = "Aug";
months[9] = "Sep";
months[10] = "Oct";
months[11] = "Nov";
months[12] = "Dec";

var d = new Date(document.getElementById('check_in').value);
      d.setDate(d.getDate()+ 10); //days to add
      document.getElementById('check_out_date').value= d.getDate()+"/"+ months[(d.getMonth()+1)] + "/"+d.getYear();
Avatar of taborrg
taborrg

ASKER

It's month day year, the 112 is what it's returning for 2012
I missed the array in the code, this goes before the month names.
var months = new Array(13);

Check the date is in the correct format from your date picker.
Avatar of taborrg

ASKER

This is from the datepicker - 05/NOV/2012  -  it's what I need.

I'm still getting  11/7/112 as the returned date from this code:

var months = new Array(13);
      months[1] = "Jan";
      months[2] = "Feb";
      months[3] = "Mar";
      months[4] = "Apr";
      months[5] = "May";
      months[6] = "Jun";
      months[7] = "Jul";
      months[8] = "Aug";
      months[9] = "Sep";
      months[10] = "Oct";
      months[11] = "Nov";
      months[12] = "Dec";
      
      var d = new Date(document.getElementById('check_in').value);
      d.setDate(d.getDate()+ tmpNumDays); //days to add
      document.getElementById('check_out_date').value= (d.getMonth()+1)+"/"+d.getDate()+"/"+d.getYear();


Thanks
Add an alert and report back what it says

d.setDate(d.getDate()+ 10); //days to add
alert(d)
Avatar of taborrg

ASKER

Sun Nov 11 2012 00:00:00 GMT-1000 (Hawaiian Standard Time)
Avatar of taborrg

ASKER

Actually it says:  Thu Nov 15 2012 00:00:00 GMT-1000 (Hawaiian Standard Time)
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of taborrg

ASKER

Perfect.

Thanks.