js获取获取本星期第一天,本月第一天,本季度第一天 - Go语言中文社区

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


项目中的具体需求:

根据下拉框动态选择日期

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;
	}
  }

 
 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/u014772531/article/details/84808988
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-09-04 14:41:42
  • 阅读 ( 1224 )
  • 分类:前端

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢