例如日期是2019-01-29(2019年1月29日) 我想将一月一月从29日期设置为31日期,并使用JavaScript结果显示为2019-01-31
答案 0 :(得分:0)
var date = new Date();
date.setMonth(date.getMonth() + 1);
date.setDate(-1);
将日期设置为-1会将其设置为上个月的最后一天。
答案 1 :(得分:0)
//Set this to whatever date you want..
var d = '2019-02-21';
//Parse out our date object a bit..
var asOf = new Date(d);
var year = asOf.getFullYear();
var month = asOf.getMonth();
//Initially set to first day of next month..
var desiredDate = new Date(year, month + 1, 1);
//Now just subtract 1 day to make it the last day of prior month..
desiredDate.setDate(desiredDate.getDate() - 1);
//Show the date of last day in month..
console.log(`The last day of ${month + 1}/${year} is: ${desiredDate.toLocaleString().split(',')[0]}`);