0

Im really new to Java script, so please bear with me. I'm trying to write a script that will return a date with the addition of a user selected number of days. It works fine as far as returning a value (in the variable expDate, however the value is in the long format (i.e. "Wed Jan 02 2013 00:00:00 GMT+0800 (HKT)) . I run into trouble when I try and break it down into different sections using split. Now my output is just blank.

Any help would be greatly appreciated!

Javascript

<script type="text/javascript">
function setExpDate(){

var formDate = document.getElementById('startDate').value;
var number = +document.getElementById('days').value;


var interval = number;

var startDate = new Date(Date.parse(formDate));

var expDate = startDate;
expDate.setDate(startDate.getDate() + interval);



var splitDate = expDate.split(' ');

var monthFomatted  = splitDate[1];
var dayFomatted  = splitDate[2];
var yearFomatted  = splitDate[3];


document.getElementById('total').innerHTML = monthFormatted;
document.getElementById('daysdays').innerHTML = Dateformatted;
};
</script>
</head>

HTML

<body>

<input type="text" size="10" maxlength="10" id="startDate" name="startDate" onblur="setExpDate(this.value)">

<select name="days" id="days" onchange="setExpDate(this.value)">
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
</select>

 <div id="total"></div> <br/><div id="daysdays"></div>
</body>
 </html>

1 Answer 1

3

.split() is a String method (that is it works on strings) and you are trying to use it on a Date object.

You'd be better to do

var monthFomatted  = expDate.getMonth();
var dayFomatted  = expDate.getDay();
var yearFomatted  = expDate.getYear();

getMonth(), getDay() and getYear() being methods that can be used on a Date object.

EDIT

Also note that you are setting monthFomatted then accessing monthFormatted which just isn't going to work. Check to make sure that variables are named consistently.

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.