# 日期

  • js
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

var weeknum = today.getDay()
/*
 * 获取周一,周日信息
 * 周一,周二返回上周,周三及以后返回本周
 */
function getWeek() {
 var current = new Date(),
     today = current.getDate(),
     weekNum = current.getDay(),
     firstDay,
     lastDay

 if (weekNum < 3) {
   firstDay = today - weekNum - 6
 } else {
   firstDay = today - weekNum + 1
 }

 lastDay = firstDay + 6

 var monday = new Date(current.setDate(firstDay))
 var sunday = new Date(current.setDate(lastDay))

 return {
   monday: formatDate(monday),
   sunday: formatDate(sunday)
 }

}

function formatDate(date) {
 var separator = arguments[1] || '-',
     year = date.getFullYear(),
     mouth = ('0' + (date.getMonth() + 1)).substr(-2, 2),
     day = ('0' + date.getDate()).substr(-2, 2)

 return [year, mouth, day].join(separator)
}

# 获取双数字日期

('0' + (new Date().getMonth() + 1)).slice(-2)
Last Updated: 5/14/2022, 11:38:45 AM