js中Date()的用法及日期计算方法

Date函数是用来处理日期和时间的,其时间是和GMT时间1970年1月1日进行比较的。

Date构造方法

Date函数有6种构造函数,如下:

new Date();                                   //获取当前系统日期

new Date("month dd,yyyy hh:mm:ss");   

new Date("month dd,yyyy");

new Date(yyyy,mth,dd,hh,mm,ss);

new Date(yyyy,mth,dd);

new Date(ms);                                       //根据毫秒数生成指定日期

注意最后一种形式,参数表示的是需要创建的时间和GMT时间1970年1月1日之间相差的毫秒数。

各种函数的含义如下:

month:用英文表示月份名称,从January到December

mth:用整数表示月份,从0-11(1月到12月)

dd:表示一个月中的第几天,从1到31

yyyy:四位数表示的年份

hh:小时数,从0(午夜)到23(晚11点)

mm:分钟数,从0到59的整数

ss:秒数,从0到59的整数

ms:毫秒数,为大于等于0的整数

new Date("January 12,2016 22:19:35");  //Thu Jan 12 2016 22:19:35 GMT+0800 (中国标准时间)
new Date("January 12,2016");           //Tue Jan 12 2016 00:00:00 GMT+0800 (中国标准时间)
new Date(2016,0,12,22,19,35)           //Tue Jan 12 2016 22:19:35 GMT+0800 (中国标准时间)
new Date(2016,0,12);                   //Tue Jan 12 2016 00:00:00 GMT+0800 (中国标准时间)
new Date(1452528000000);               //Tue Jan 12 2016 00:00:00 GMT+0800 (中国标准时间)
new Date();                            //Fri Jul 29 2016 11:15:00 GMT+0800 (中国标准时间)


Date函数的方法

getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。
getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。
getMonth() 从 Date 对象返回月份 (0 ~ 11)。
getFullYear() 从 Date 对象以四位数字返回年份。
getYear() 请使用 getFullYear() 方法代替。
getHours() 返回 Date 对象的小时 (0 ~ 23)。
getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。
getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。
getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。
getTime() 返回 1970 年 1 月 1 日至今的毫秒数。
getTimezoneOffset() 返回本地时间与格林威治标准时间 (GMT) 的分钟差。
getUTCDate() 根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。
getUTCDay() 根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。
getUTCMonth() 根据世界时从 Date 对象返回月份 (0 ~ 11)。
getUTCFullYear() 根据世界时从 Date 对象返回四位数的年份。
getUTCHours() 根据世界时返回 Date 对象的小时 (0 ~ 23)。
getUTCMinutes() 根据世界时返回 Date 对象的分钟 (0 ~ 59)。
getUTCSeconds() 根据世界时返回 Date 对象的秒钟 (0 ~ 59)。
getUTCMilliseconds() 根据世界时返回 Date 对象的毫秒(0 ~ 999)。
parse() 返回1970年1月1日午夜到指定日期(字符串)的毫秒数。
setDate() 设置 Date 对象中月的某一天 (1 ~ 31)。
setMonth() 设置 Date 对象中月份 (0 ~ 11)。
setFullYear() 设置 Date 对象中的年份(四位数字)。
setYear() 请使用 setFullYear() 方法代替。
setHours() 设置 Date 对象中的小时 (0 ~ 23)。
setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)。
setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)。
setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)。
setTime() 以毫秒设置 Date 对象。
setUTCDate() 根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。
setUTCMonth() 根据世界时设置 Date 对象中的月份 (0 ~ 11)。
setUTCFullYear() 根据世界时设置 Date 对象中的年份(四位数字)。
setUTCHours() 根据世界时设置 Date 对象中的小时 (0 ~ 23)。
setUTCMinutes() 根据世界时设置 Date 对象中的分钟 (0 ~ 59)。
setUTCSeconds() 根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。
setUTCMilliseconds() 根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。
toSource() 返回该对象的源代码。
toString() 把 Date 对象转换为字符串。
toTimeString() 把 Date 对象的时间部分转换为字符串。
toDateString() 把 Date 对象的日期部分转换为字符串。
toGMTString() 请使用 toUTCString() 方法代替。
toUTCString() 根据世界时,把 Date 对象转换为字符串。
toLocaleString() 根据本地时间格式,把 Date 对象转换为字符串。
toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串。
toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串。
UTC() 根据世界时返回 1997 年 1 月 1 日 到指定日期的毫秒数。
valueOf() 返回 Date 对象的原始值。

将日期转换为yyyy-MM-dd格式的字符串

//将日期转化字符串(yyyy-MM-dd)
function DateToStr(date){
     var year = date.getFullYear();
     var month =(date.getMonth() + 1).toString();
     var day = (date.getDate()).toString();
     if (month.length == 1) {
         month = "0" + month;
     }
     if (day.length == 1) {
         day = "0" + day;
     }
     dateTime = year +"-"+ month +"-"+  day;
     return dateTime;
}


将yyy-MM-dd格式的字符串转换为日期

function strToDate(dateStr){
	var dateStr = dateStr.replace(/-/g, "/");//现将yyyy-MM-dd类型转换为yyyy/MM/dd
	var dateTime = Date.parse(dateStr);//将日期字符串转换为表示日期的秒数
	//注意:Date.parse(dateStr)默认情况下只能转换:月/日/年 格式的字符串,但是经测试年/月/日格式的字符串也能被解析
	var data = new Date(dateTime);//将日期秒数转换为日期格式
	return data;
}


日期大小比较

在js中yyyy/MM/dd格式的日期是可以直接比较大小的,所以只需要将其他格式的日期转换为这种格式就可以直接比较。例如:
function compareDate(startDate,endDate){
	var d1 = new Date(startDate.replace(/\-/g, "\/"));  
	var d2 = new Date(endDate.replace(/\-/g, "\/"));  
	if(d1 >d2)  
 	{  
  		alert("开始时间不能大于结束时间!");  
  		return false;  
 	}
	return true;
}

根据出生日期计算年龄

//根据出生日期计算年龄(yyyy-MM-dd)
function getBrithAge(birthday){
	var nowDay = new Date();
	//出生年月日
	var birthYear = birthday.getFullYear();
	var birthMonth = birthday.getMonth()+1;
	var birthDate = birthday.getDate();
	//当前年月日
	var nowYear = nowDay.getFullYear();
	var nowMonth = nowDay.getMonth()+1;
	var nowDate = nowDay.getDate();
	
	//计算年差
	var age  = nowYear - birthYear;
	//当前月大于出生月,则岁数不足
	if(nowMonth < birthMonth){
		age = age -1;
	}else if(nowMonth == birthMonth){
		if(nowDate < birthDate){
			age = age -1;
		}
	}
	
	return age;
}


  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值