Get a date with 'YYYY-MM-DD' format in JavaScript from a Datetime picker

1

Good morning. I have a Datetime picker in a form to select a date and I need 30 days to add it and the result appears in a input with 'YYYY-MM-DD' format. I capture the value in the event 'onChange' of Datetime picker . The problem comes when adding the indicated amount and obtaining the date in the required format.

$('.datepicker-only-init').datetimepicker({
       format: 'YYYY-MM-DD'
}).on('dp.change', function (e) {
        console.log(e.date);
 });

A cosole.log of the date returns this format:

  

Wed May 31 2017 10:12:02 GMT-0400 (Western Standard Time, South America)

    
asked by FeRcHo 31.05.2017 в 16:14
source

2 answers

1

As an example for the date:

var deadline = new Date();
console.log(deadline);
//Wed May 31 2017 12:23:16 GMT-0500 (Hora de verano central (México))
var x = new Date(deadline.setDate(deadline.getDate() + 30));
console.log(x);
//Fri Jun 30 2017 12:23:16 GMT-0500 (Hora de verano central (México))

Assuming that the object "e.date" is a date type, it would look something like this:

var x = new Date(e.date.setDate(e.date.getDate() + 30));
console.log(x);
    
answered by 31.05.2017 / 19:22
source
1

I think that this way your problem will be solved

$('.datepicker-only-init').datetimepicker({
       format: 'YYYY-MM-DD'
}).on('dp.change', function (e) {
       console.log(e.date.setDate(e.date.getDate() + 30));
 });
    
answered by 31.05.2017 в 16:36