亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

js 計(jì)算月/周的第一天和最后一天代碼

 更新時間:2020年02月01日 14:29:47   投稿:mdxy-dxy  
這篇文章主要介紹了js 計(jì)算月/周的第一天和最后一天代碼,需要的朋友可以參考下

因?yàn)轫?xiàng)目開發(fā)中遇到需要向后臺傳本周的開始和結(jié)束時間,以及上一周的起止時間,就琢磨了半天,總算寫出來一套,寫篇文章是為了方便自己記憶,也是分享給需要的人,水平有限,寫的不好請見諒:

1、getDateStr3函數(shù)是為了把時間對象轉(zhuǎn)變?yōu)閥y-mm-dd的字符串,方便傳值;

2、getWeekStartAndEnd函數(shù)是獲取周的起止時間,并且用getDateStr3轉(zhuǎn)換成字符串放到數(shù)組中,其中參數(shù)0代表當(dāng)前周,-1代表前一周,-2代表上上周,以此類推,反過來也可以1代表下一周;

3、getMonthStartAndEnd函數(shù)是獲取月的起止時間,傳參同上

//獲取當(dāng)前日期yy-mm-dd
//date 為時間對象
function getDateStr3(date) {
  var year = "";
  var month = "";
  var day = "";
  var now = date;
  year = ""+now.getFullYear();
  if((now.getMonth()+1)<10){
    month = "0"+(now.getMonth()+1);
  }else{
    month = ""+(now.getMonth()+1);
  }
  if((now.getDate())<10){
    day = "0"+(now.getDate());
  }else{
    day = ""+(now.getDate());
  }
  return year+"-"+month+"-"+day;
}
/** 
* 獲得相對當(dāng)前周AddWeekCount個周的起止日期 
* AddWeekCount為0代表當(dāng)前周  為-1代表上一個周  為1代表下一個周以此類推
* **/ 
function getWeekStartAndEnd(AddWeekCount) { 
  //起止日期數(shù)組  
  var startStop = new Array(); 
  //一天的毫秒數(shù)  
  var millisecond = 1000 * 60 * 60 * 24; 
  //獲取當(dāng)前時間  
  var currentDate = new Date();
  //相對于當(dāng)前日期AddWeekCount個周的日期
  currentDate = new Date(currentDate.getTime() + (millisecond * 7*AddWeekCount));
  //返回date是一周中的某一天
  var week = currentDate.getDay(); 
  //返回date是一個月中的某一天  
  var month = currentDate.getDate();
  //減去的天數(shù)  
  var minusDay = week != 0 ? week - 1 : 6; 
  //獲得當(dāng)前周的第一天  
  var currentWeekFirstDay = new Date(currentDate.getTime() - (millisecond * minusDay)); 
  //獲得當(dāng)前周的最后一天
   var currentWeekLastDay = new Date(currentWeekFirstDay.getTime() + (millisecond * 6));
  //添加至數(shù)組  
  startStop.push(getDateStr3(currentWeekFirstDay)); 
  startStop.push(getDateStr3(currentWeekLastDay)); 
  
  return startStop; 
} 
/** 
* 獲得相對當(dāng)月AddMonthCount個月的起止日期 
* AddMonthCount為0 代表當(dāng)月 為-1代表上一個月 為1代表下一個月 以此類推
* ***/ 
function getMonthStartAndEnd(AddMonthCount) { 
  //起止日期數(shù)組  
  var startStop = new Array(); 
  //獲取當(dāng)前時間  
  var currentDate = new Date();
  var month=currentDate.getMonth()+AddMonthCount;
  if(month<0){
    var n = parseInt((-month)/12);
    month += n*12;
    currentDate.setFullYear(currentDate.getFullYear()-n);
  }
  currentDate = new Date(currentDate.setMonth(month));
  //獲得當(dāng)前月份0-11  
  var currentMonth = currentDate.getMonth(); 
  //獲得當(dāng)前年份4位年  
  var currentYear = currentDate.getFullYear(); 
  //獲得上一個月的第一天  
  var currentMonthFirstDay = new Date(currentYear, currentMonth,1); 
  //獲得上一月的最后一天  
  var currentMonthLastDay = new Date(currentYear, currentMonth+1, 0); 
  //添加至數(shù)組  
  startStop.push(getDateStr3(currentMonthFirstDay)); 
  startStop.push(getDateStr3(currentMonthLastDay)); 
  //返回  
  return startStop; 
}

獲取到每月的第一天和最后一天 需要傳入一個月份 如果忘記傳入 取當(dāng)前月份

//獲取到每月的第一天和最后一天
 getMonthFirstOrLaseDay:function(month){
        var month=month || (new Date()).getMonth() //設(shè)置默認(rèn) 如果不穿 取當(dāng)前月份
    var nowdays = new Date(); 
    var year = nowdays.getFullYear(); 
    if(month==0) { 
      month=12; 
      year=year-1; 
    } 
    if (month < 10) { 
      month = "0" + month; 
    } 
    var firstDay = year+'' + month+'' + "01";
    var myDate = new Date(year, month, 0); 
    var lastDay = year+'' + month+'' + myDate.getDate();
    return {firstDay:firstDay,lastDay:lastDay}
  },

獲取到每個月有幾周,并且每周一和周日是哪天 如果不穿 默認(rèn)取當(dāng)年 當(dāng)月

//獲取到每個月有幾周,并且每周一和周日是哪天
 getAForWeeks:function (year, month) {
  var year=year || (new Date()).getFullYear()
  var month=month || (new Date()).getMonth()


  var d = new Date();
  // what day is first day
  d.setFullYear(year, month-1, 1);
  var w1 = d.getDay();
  if (w1 == 0) w1 = 7;
  // total day of month
  d.setFullYear(year, month, 0);
  var dd = d.getDate();
  // first Monday
  if (w1 != 1) d1 = 7 - w1 + 2;
  else d1 = 1;
  week_count = Math.ceil((dd-d1+1)/7);
  var allWeek={};
  for (var i = 0; i < week_count; i++) {
    var monday = d1+i*7;
    var sunday = monday + 6;
    var from = year+''+this.fnToDub(month)+''+this.fnToDub(monday);
    var to;
    if (sunday <= dd) {
      to = year+''+this.fnToDub(month)+''+this.fnToDub(sunday);
    } else {
      d.setFullYear(year, month-1, sunday);
      to = d.getFullYear()+''+this.fnToDub((d.getMonth()+1))+''+this.fnToDub(d.getDate());
    }
    allWeek[(i+1)]={
     from:from,
     to:to
    }
  }
  return {allWeek:allWeek,week_count:week_count}
 },

獲取當(dāng)月的第一天和當(dāng)月的最后一天其實(shí)還挺麻煩的,因?yàn)槊總€月天數(shù)可能不一樣。不過借助 Date 對象則很容易實(shí)現(xiàn):

構(gòu)造函數(shù)

new Date();
new Date(value);
new Date(dateString);
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);

各參數(shù)的含義:

value 代表自1970年1月1日00:00:00 (世界標(biāo)準(zhǔn)時間) 起經(jīng)過的毫秒數(shù)。
dateString 表示日期的字符串值。該字符串應(yīng)該能被 Date.parse() 方法識別
year 代表年份的整數(shù)值。為了避免2000年問題最好指定4位數(shù)的年份; 使用 1998, 而不要用 98.
month 代表月份的整數(shù)值從0(1月)到11(12月)。
day 代表一個月中的第幾天的整數(shù)值,從1開始。
hour 代表一天中的小時數(shù)的整數(shù)值 (24小時制)。
minute 分鐘數(shù)。
second 秒數(shù)。
millisecond 表示時間的毫秒部分的整數(shù)值。

當(dāng)月第一天和最后一天

可直接用年月日構(gòu)造一個日期:

var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);

var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

指定月份的第一天和最后一天

比如2012年1月第一天和最后一天,運(yùn)算時月份要減1

var y = 2012, m = 1
var firstDay = new Date(y, m - 1, 1);
var lastDay = new Date(y, m, 0);
console.log(firstDay);
console.log(lastDay);

運(yùn)行結(jié)果:

Sun Jan 01 2012 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時間)
Tue Jan 31 2012 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時間)

相關(guān)文章

最新評論