Question

I have a field that has a datepicker, but what I want that field, called New Date, to have populate with the date in the div id="olddate" plus one day. The problem I'm seeing is that the date in the olddate div is a text string, so I need to convert that into a date object, put it in the New Date field and add a day. I also prefer to keep it in jQuery, if possible. I've seen plenty of answers to questions very similar to this, but nothing seems to work. Thanks in advance!! Fiddle: http://jsfiddle.net/Carlos1815/U9Avn/

Here's what I got thus far:

<html lang="en">
<head>
<link href="css/ui-lightness/jquery-ui-1.10.4.css" rel="stylesheet">
<script src="js/jquery-1.10.2.js"></script>
<script src="js/jquery-ui-1.10.4.js"></script>
<script>    
$(function() {
        var olddate = $("#olddate").text(); 
        $( "#datepicker" ).datepicker();
        $( "#datepicker" ).datepicker( "setDate", olddate);
        });      
</script>
</head>
<body>      
    <div id="olddate">3/30/2013</div>
    <div>New Date: <input type="text" id="datepicker"></div>     
</body>
</html>
Was it helpful?

Solution

Its worth to use moment.js at this moment to solve this date time problems.

Working Demo

$(function () {        
    var day = moment($("#olddate").text(), "MM/DD/YYYY");
    day.add('days', 1)
    var newDate=day.toDate();    
    $("#datepicker").datepicker();
    $("#datepicker").datepicker("setDate", newDate);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top