0

I want to emit the day after the selected day.

$('#toDate').datepicker({
    inline: true,
    altField: '#x',
    dateFormat: "dd-mm-yy", //day
    altFormat: "yy-mm-dd", //year
    monthNames: ["Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık"],
    dayNamesMin: ["Pa", "Pt", "Sl", "Ça", "Pe", "Cu", "Ct"],
    firstDay: 1,
    numberOfMonths: 1,
    onSelect: function(dateText, inst) {
        var dateup = ('0' + (parseInt(inst.selectedDay) + 1)).slice(-2);
        var monthup = ('0' + (parseInt(inst.selectedMonth) + 1)).slice(-2);
        var newdate = inst.selectedYear+'-'+monthup+'-'+dateup;
        socket.emit('sockettoDate', newdate);
    }
});

This code cannot calculate the day after the last day of month.

For example if 2017-12-31 selected, result is 2017-12-32. Any solution?

ilvthsgm
  • 586
  • 1
  • 8
  • 26
  • Possible duplicate of [Add days to JavaScript Date](https://stackoverflow.com/questions/563406/add-days-to-javascript-date) – CBroe Dec 20 '17 at 13:23
  • Create a proper Date object, instead of doing this on a string concatenation level - it will handle such "overflows" for you. – CBroe Dec 20 '17 at 13:23

3 Answers3

1

if your date format is same as you have given in example then you can use following function for add days into date. After getting new date you can use new added date

function addDays(date, days) {
  var result = new Date(date);
  result.setDate(result.getDate() + days);
  return result;
}

USE

addDays(new Date('2017-12-31 '),1);

OUT Put

Mon Jan 01 2018 00:00:00 GMT+0530 (India Standard Time)

Rony Patel
  • 357
  • 1
  • 2
  • 14
0

It's because the way you did things you're working with numbers, not dates. To fix it you need to convert it to a date first.

To work get a date from the jquery datepicker plugin, use:

var currentDate = $("#toDate").datepicker('getDate').getDate();

and then you can add days following this answer: Incrementing a date in JavaScript

I just finished working on a pretty complex jquery calendar and I've found momentjs to be a lifesaver, so I'd suggest you get it, too. With it you can do things like: currentDate.add(1, 'days'); which makes the developer's life much easier.

Jonas Grumann
  • 10,438
  • 2
  • 22
  • 40
0

You can use JavaScript Date object for this.

onSelect: function(dateText, inst) {
    var date = new Date(dateText);
    var newdate = new Date(date);

    newdate.setDate(newdate.getDate() + 31 );

    socket.emit('sockettoDate', newdate);
}

But my recommendation, to use momentjs for any date and time calculation. great library for date and time manipulating.

jitender
  • 33
  • 9