您的位置:首页 > Web前端 > JavaScript

js获取获取本星期第一天,本月第一天,本季度第一天

2016-07-07 09:05 441 查看

项目中的具体需求:

根据下拉框动态选择日期

var timeUtil = {
//获取本星期第一天
getFirstDayOfWeek:function(){
var now = new Date();
var day = now.getDate(), //获取本月几号
weekday = now.getDay(), //获取星期几
month = now.getMonth(),//获取本月
year = now.getFullYear();//获取本年
if(day > weekday-1){
now.setDate(day - weekday-1);
}else{
//月份从0开始
if(month == 0){
now.setYear(year-1);
now.setMonth(11);
now.setDate(weekday - day-1);
}else{
now.setMonth(month - 1);
now.setDate(weekday - day-1);
}
}
return this.timeFormate(now);
},

//获取本月第一天
getFirstDayOfMonth:function(){
var now = new Date();
var day = now.setDate(1);

return this.timeFormate(now);
},

//获取本季度第一天
getFirstDayOfQuarter: function(){
var now = new Date();
var month = now.getMonth();
if(month <3 ){
now.setMonth(0);
}else if(2 < month && month < 6){
now.setMonth(3);
}else if(5 < month && month < 9){
now.setMonth(6);
}else if(8 < month && month < 11){
now.setMonth(9);
}
now.setDate(1);
return this.timeFormate(now);
},

//时间格式化
timeFormate : function(date){
if(!date || typeof(date) === "string"){
this.error("参数异常,请检查...") ;
}
var year = date.getFullYear(); //年
var month = date.getMonth() + 1; //月
var day = date.getDate(); //日

if (date.getMinutes() / 60 > 1) {
hh += Math.floor(date.getMinutes()) / 60;
}
var clock = year + "-";
if (month < 10)
clock += "0";
clock += month + "-";
if (day < 10)
clock += "0";
clock += day + " ";
return clock;
}
}

 
 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐