0

I have code like this

<script language="javascript" type="text/javascript">

function getdate() {
    var tt = document.getElementById('txtDate').value;
    var ct = document.getElementById('package').value;

    var date = new Date(tt);
    var newdate = new Date(date);
    newdate.setDate(newdate.getDate() + ct);

    var mm = newdate.getMonth()+1;
    var dd = newdate.getDate();
    var yyy = newdate.getFullYear();

    var someFormattedDate = mm + '/' + dd + '/' + yyy;
   document.getElementById('EndDate').value = someFormattedDate;
}
</script>

<div class="form-group">
    <label>Start Tour</label>
    <input type="date" id="txtDate" name="start_date" required> 
</div>

<p></p>

<label> Ending Tour</label>
<input type="text" id="EndDate" name="end_date" required>   


<label>Nama Package</label>
<select class="form-control" id="package" name="package" onChange="getdate()">
    <option value= disabled>--Pilih Package Tour--</option>
    <?php
    $sql = mysql_query("select * from package");
    while ($lihat = mysql_fetch_array($sql)) {
    ?>
        <option value="<?php echo $lihat['lama_hari']; ?>"><?php echo $lihat['nama_package'] ?></option>
    <?php } ?>
</select>

When I run this code, I get the following result:

enter image description here

Why do I get this date in Ending Tour?

I count add date with the value from database of nama package, the value is 1 in database.
The result should be 09/03/2016, not 09/21/2016

I have no idea why the result becomes like that. I'm beginner here.

5
  • So you want to add x number of days to the date selected by the user where x is the value of <select id="package">? Commented Sep 21, 2016 at 3:49
  • 1
    What you actual want ? Commented Sep 21, 2016 at 5:51
  • because you set document.getElementById('EndDate').value = someFormattedDate; in your code thats why the Ending Tour has a value Commented Sep 21, 2016 at 7:02
  • yes i want to add value from id package Commented Sep 22, 2016 at 1:39
  • what i want is when i select dropdown in the nama package, the date is counting from value of package. and the result will show in tgl ending tour with format date Commented Sep 22, 2016 at 1:44

2 Answers 2

2

use momentJS http://jsfiddle.net/VtoCorleone/jbgUt/2/ this is good for convert, adding, subracting etc dates in javascript

don't forget to include it to your html

moment(date, "YYYY-MM-DD").add('days', 10); // 10 = the days add
Sign up to request clarification or add additional context in comments.

Comments

1

Its because your variable named ct is string type, First convert it to int type.

newdate.setDate(newdate.getDate() + parseInt(ct));

1 Comment

thank you for helping :) It's Work. That's what i want.

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.