获取一年中相关日期的封装

2018-08-13  本文已影响0人  H_XMM
1、日期每次增加7天的函数
function addSevenDay(d,s){
  var d = new Date(d);
  d = d.valueOf();
  d = d + s*24*60*60*1000;
  d = new Date(d);
  return d;
}
2、格式化日期
function see(d){
  var month = d.getMonth() + 1 < 10 ?"0" +(d.getMonth() + 1) : d.getMonth + 1;
  var day = d.getDate() < 10?"0" + d.getDate() : d.getDate();
  return d.getFullYear() + "-" + month + "-" + day;
}
3、获取每一周的周一的号数
function getMonday(d){
   var w = d.getDay();
   var n = (w  == 0? 7 : w) - 1;
   var Monday - new Date(d.setDate(d.getDate() - n));
   return see(Monday);
}
4、获取每一周周日的号数
function getSunday(d){
     var w = d.getDay();
     var n = (w  == 0? 7 : w) - 1;
     var Friday = new Date(d.setDate(d.getDate() - n + 6));
}
5、获取当前周为全年的第几周
function getWeek(d){
  var time, week, checkDate = new Date(d);
  checkDate.setDate(checkDate.getDate() + 4 - (checkDate.getDay() || 7));
  time = checkDate.getTime();
  checkDate.setMonth(0);
  checkDate.setDate(1);
  week = Math.floor(Math.round((time - checkDate / 86400000 / 7) + 1;
  return week;
}
上一篇下一篇

猜你喜欢

热点阅读