vue对日期的年、月、日进行增加,转换成指定格式的字符串(yyyy-MM-dd)

let date = new Date('2023-12-28'); //当前日期

let startYear = date.getFullYear(); // 年

let startMonth = date.getMonth() + 1; // 月

let addYear = 3;

date.setFullYear(startYear + Number(addYear ));

endDate = this.formatDate(date);

let addMonth = 3;

let endMonth = startMonth + Number(addMonth );

if (endMonth > 12) {

     date.setFullYear(startYear + 1);

     date.setMonth(endMonth - 13);

     endDate = this.formatDate(date);

else {

     date.setMonth(endMonth - 1);

     endDate = this.formatDate(date);

}

let startTimestamp = Date.parse(date); // 转换成时间戳

一天的时间戳为86400000

let addDate = 3;

endDate = this.formatDate(

    new Date(

        startTimestamp + Number(addDate) * 86400000

     )

);

日期转换成指定格式的字符串yyyy-MM-dd

formatDate(date) {

      let dateStr = date.getFullYear() + "-";

      dateStr += date.getMonth() + 1 + "-";

      dateStr += date.getDate();

      return dateStr;

}

你可能感兴趣的:(#,javascript,vue.js,javascript,ecmascript)