js 計(jì)算月/周的第一天和最后一天代碼
因?yàn)轫?xiàng)目開發(fā)中遇到需要向后臺(tái)傳本周的開始和結(jié)束時(shí)間,以及上一周的起止時(shí)間,就琢磨了半天,總算寫出來一套,寫篇文章是為了方便自己記憶,也是分享給需要的人,水平有限,寫的不好請(qǐng)見諒:
1、getDateStr3函數(shù)是為了把時(shí)間對(duì)象轉(zhuǎn)變?yōu)閥y-mm-dd的字符串,方便傳值;
2、getWeekStartAndEnd函數(shù)是獲取周的起止時(shí)間,并且用getDateStr3轉(zhuǎn)換成字符串放到數(shù)組中,其中參數(shù)0代表當(dāng)前周,-1代表前一周,-2代表上上周,以此類推,反過來也可以1代表下一周;
3、getMonthStartAndEnd函數(shù)是獲取月的起止時(shí)間,傳參同上
//獲取當(dāng)前日期yy-mm-dd
//date 為時(shí)間對(duì)象
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;
}
/**
* 獲得相對(duì)當(dāng)前周AddWeekCount個(gè)周的起止日期
* AddWeekCount為0代表當(dāng)前周 為-1代表上一個(gè)周 為1代表下一個(gè)周以此類推
* **/
function getWeekStartAndEnd(AddWeekCount) {
//起止日期數(shù)組
var startStop = new Array();
//一天的毫秒數(shù)
var millisecond = 1000 * 60 * 60 * 24;
//獲取當(dāng)前時(shí)間
var currentDate = new Date();
//相對(duì)于當(dāng)前日期AddWeekCount個(gè)周的日期
currentDate = new Date(currentDate.getTime() + (millisecond * 7*AddWeekCount));
//返回date是一周中的某一天
var week = currentDate.getDay();
//返回date是一個(gè)月中的某一天
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;
}
/**
* 獲得相對(duì)當(dāng)月AddMonthCount個(gè)月的起止日期
* AddMonthCount為0 代表當(dāng)月 為-1代表上一個(gè)月 為1代表下一個(gè)月 以此類推
* ***/
function getMonthStartAndEnd(AddMonthCount) {
//起止日期數(shù)組
var startStop = new Array();
//獲取當(dāng)前時(shí)間
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();
//獲得上一個(gè)月的第一天
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;
}
獲取到每月的第一天和最后一天 需要傳入一個(gè)月份 如果忘記傳入 取當(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}
},
獲取到每個(gè)月有幾周,并且每周一和周日是哪天 如果不穿 默認(rèn)取當(dāng)年 當(dāng)月
//獲取到每個(gè)月有幾周,并且每周一和周日是哪天
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)槊總€(gè)月天數(shù)可能不一樣。不過借助 Date 對(duì)象則很容易實(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)時(shí)間) 起經(jīng)過的毫秒數(shù)。
dateString 表示日期的字符串值。該字符串應(yīng)該能被 Date.parse() 方法識(shí)別
year 代表年份的整數(shù)值。為了避免2000年問題最好指定4位數(shù)的年份; 使用 1998, 而不要用 98.
month 代表月份的整數(shù)值從0(1月)到11(12月)。
day 代表一個(gè)月中的第幾天的整數(shù)值,從1開始。
hour 代表一天中的小時(shí)數(shù)的整數(shù)值 (24小時(shí)制)。
minute 分鐘數(shù)。
second 秒數(shù)。
millisecond 表示時(shí)間的毫秒部分的整數(shù)值。
當(dāng)月第一天和最后一天
可直接用年月日構(gòu)造一個(gè)日期:
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)算時(shí)月份要減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 (中國(guó)標(biāo)準(zhǔn)時(shí)間)
Tue Jan 31 2012 00:00:00 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)
相關(guān)文章
Bootstrap中g(shù)lyphicons-halflings-regular.woff字體報(bào)404錯(cuò)notfound的解
這篇文章主要介紹了 Bootstrap中g(shù)lyphicons-halflings-regular.woff字體報(bào)404錯(cuò)notfound的解決方法,需要的朋友可以參考下2017-01-01
bootstrap為水平排列的表單和內(nèi)聯(lián)表單設(shè)置可選的圖標(biāo)
為水平排列的表單和內(nèi)聯(lián)表單設(shè)置可選的圖標(biāo)。本文通過示例代碼給大家講解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-02-02
利用JavaScript為句子加標(biāo)題的3種方法示例
這篇文章主要給大家介紹了關(guān)于如何利用JavaScript為句子加標(biāo)題的3種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
原生JS實(shí)現(xiàn)自定義滾動(dòng)條效果
這篇文章主要為大家詳細(xì)介紹了原生JS實(shí)現(xiàn)自定義滾動(dòng)條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
基于js里調(diào)用函數(shù)時(shí),函數(shù)名帶括號(hào)和不帶括號(hào)的區(qū)別
下面小編就為大家?guī)硪黄趈s里調(diào)用函數(shù)時(shí),函數(shù)名帶括號(hào)和不帶括號(hào)的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07
JS簡(jiǎn)單實(shí)現(xiàn)文件上傳實(shí)例代碼(無需插件)
注意一下:在chrome瀏覽器下,為了數(shù)據(jù)安全,隱藏的input:file不能trigger “click” 事件。 所以要設(shè)置input:file的透明度達(dá)到隱藏的效果2013-11-11
IE瀏覽器下JS腳本提交表單后,不能自動(dòng)提示問題解決方法
這篇文章主要介紹了IE瀏覽器下JS腳本提交表單后,不能自動(dòng)提示問題解決方法,涉及IE瀏覽器配置與javascript事件處理操作技巧,需要的朋友可以參考下2019-06-06

