0

I found various answers to add no. of days to a given date. But i want to add a number of days to a Date & Time which is in format MM-DD-YYYY HH:MM:SS

I tried this and this was successful when i had only date but with Date & time it returns invalid date when i console.log(startDate)

HTML

<input type="text" id="startBidding" name="startBidding" >
<input type="radio" name="expiry" value="5">  <span>  5 Days </span> 
<input type="radio" name="expiry" value="10"> <span> 10 Days </span>
<input type="text" id="endBidding" name="endBidding" >

jQuery

$(document).ready(function() {
    $('input[type=radio][name=expiry]').change(function() {

        var days = this.value;
        var startDate=new Date($("#startBidding").val());
        startDate.setDate(startDate.getDate()+parseInt(days));
        var inputDate = (startDate.getMonth()+1)+'-'+(startDate.getDate())+'-'+(startDate.getFullYear());
        $("#endBidding").val(inputDate);
    });
});
phantom
  • 43
  • 8

1 Answers1

0

You just need to put your date into a format javascript will parse correctly.

var new_date = $("#startBidding").val().replace( /(\d{2})-(\d{2})-(\d{4}) /, "$3-$1-$2T");
var startDate = new Date(new_date);

Fiddle: http://jsfiddle.net/wmsm0pnn/

trex005
  • 5,015
  • 4
  • 28
  • 41
  • not working... now the problem is that it is not working in Firefox and fine in chrome – phantom Aug 04 '15 at 05:32
  • Looks like Firefox was being picky about the ISO standard requiring a T. I have updated my code and provided a fiddle that works in both. – trex005 Aug 04 '15 at 06:39