JAVASCRIPT
VUE
REACT
NODE
ES6
TYPESCRIPT

Js里面的date

2017. 05. 12    

Js里面的Date

// 获取当前日期
var date = new Date();

// 获取当前月份
var nowMonth = date.getMonth() + 1;

// 获取当前是几号
var strDate = date.getDate();

// 添加分隔符“-”
var seperator = "-";

// 对月份进行处理,1-9月在前面添加一个“0”
if (nowMonth >= 1 && nowMonth <= 9) {
   nowMonth = "0" + nowMonth;
}

// 对月份进行处理,1-9号在前面添加一个“0”
if (strDate >= 0 && strDate <= 9) {
   strDate = "0" + strDate;
}

// 最后拼接字符串,得到一个格式为(yyyy-MM-dd)的日期
var nowDate = date.getFullYear() + seperator + nowMonth + seperator + strDate;

//注:getDay() 方法可返回一周(0~6)的某一天的数字。星期天为 0, 星期一为 1, 以此类推。
function dayStart(month, year) {
	var tmpDate = new Date(year, month, 1);
	return (tmpDate.getDay());
}

计算某年是不是闰年,通过求年份除以4的余数即可

var month_regular = [31,29,31,30,31,30,31,31,30,31,30,31];//闰年月
var month_leap = [31,28,31,30,31,30,31,31,30,31,30,31]; //正常年月
function daysMonth(month, year) {
	var tmp = year%4===0&&year%100!==0||year%400===0;
	if (tmp == 0) {
		return (month_leap[month]);
	} else {
		return (month_regular[month]);
	}
}