Js獲取時間 年月日 封裝
阿新 • • 發佈:2019-01-07
// 獲取日期 function getBeforeDate(n){ var date = new Date() ; var year,month,day ; date.setDate(date.getDate()-n); year = date.getFullYear(); month = date.getMonth()+1; day = date.getDate() ; s = ( month < 10 ? ( '0' + month ) : month ) + '-' + ( day < 10 ? ( '0' + day ) : day) ; return s ; } console.log(getBeforeDate(0));//昨天的日期 12-12 // 獲取月份 function getBeforeMonth(n){ var date = new Date() ; var year,month,day ; date.setDate(date.getMonth()+1-n); month = date.getDate() // console.log(month); s = month+'月'; // s = ( month < 10 ? ( '0' + month ) : month +'月') ; return s; } console.log(getBeforeMonth(0)); //12月 // console.log(getBeforeMonth(5)); // 獲取年月 function getBeforeYM(n){ var date = new Date() ; var year,month,day ; date.setDate(date.getMonth()+1-n); year = date.getFullYear(); month = date.getDate(); s = year+""+( month < 10 ? ( '0' + month ) : month ) ; return s ; } console.log(getBeforeYM(1));//201811