如何将x天添加到从datepicker的getDate()返回的日期中,并以特定的日期格式返回?

我想在jQuery datepicker字段返回的日期中添加几天,然后将其放入易于阅读的日期格式。

我有以下代码:

var end = $("#to-date").datepicker("getDate");

返回值:

Tue May 12 2020 00:00:00 GMT+0100 (British Summer Time)

我想在该日期前加上3天,然后以另一种日期格式返回该日期。

我增加了额外的日子:

console.log( end.getDate() + 3 );

返回值:

15 (3 days from the 12th)

我原以为这会

Fri May 15 2020 00:00:00 GMT+0100 (British Summer Time)

The end result I am looking for is to get the date in a dd/mm/yy format after adding days to an existing date.

Is this possible with getDate()?

评论
  • 谎言可笑
    谎言可笑 回复

    You can use getTime function it will provide your date into milliseconds. Add total milliseconds for 3 days (i.e. 3 * 24 * 60 * 60 * 1000) into this getTime value and convert into date again.

    在下面进行测试。

    let start = new Date();
    console.log(start);
    
    let end = new Date(start.getTime() + (3 * 24 * 60 * 60 * 1000));
    console.log(end);