JS Obtiene la fecha unos días antes y después de la fecha aparte

JS fecha de adquisición separados por unos pocos días antes y después de la fecha especificada.

/**
*date为开始日期,dayLength是相隔的天数,可以为负数;(可跨年)
*/
function getTargetDate(date,dayLength) {
    var tempDate = new Date(date);
    tempDate.setDate(tempDate.getDate() + dayLength);
    var year = tempDate.getFullYear();
    var month = tempDate.getMonth() + 1 < 10 ? "0" + (tempDate.getMonth() + 1) : tempDate.getMonth() + 1;
    var day = tempDate.getDate() < 10 ? "0" + tempDate.getDate() : tempDate.getDate();
    return year + "-" + month + "-" + day;
}

Prueba: getTargetDate (new Date ( "12/31/2019 "), 1);
Resultados: 2020-01-01

Publicado 37 artículos originales · ganado elogios 29 · Vistas a 10000 +

Supongo que te gusta

Origin blog.csdn.net/qq_42755868/article/details/91888695
Recomendado
Clasificación