獲取從本月起往前5個月的年月
阿新 • • 發佈:2018-01-26
function rip 代碼 turn scrip cti gpo bsp def
要實現一個功能,獲取從本月起往前5個月的年月,格式是這樣的‘2018-01‘,不多說直接上代碼
function initDefaultDate() { var result = []; for (var i = 0; i > -6; i--) { var curr_date = new Date(); curr_date.setMonth(curr_date.getMonth() + i); var strYear = curr_date.getFullYear(); var strMonth = curr_date.getMonth() + 1; var datastr = strYear + ‘-‘ + formatNumber(strMonth) result.push(datastr) } return result; } function formatNumber(value) { return (value < 10 ? ‘0‘ : ‘‘) + value; } document.write(initDefaultDate());
結果是這樣的
2018-01,2017-12,2017-11,2017-10,2017-09,2017-08
獲取從本月起往前5個月的年月