JavaScript Date 对象
JavaScript Date 对象
http://www.w3school.com.cn/jsref/jsref_obj_date.asp
日期处理 Date ——如何创建日期对象
var d = new Date();
var d = new Date("2015/08/22");
获取年月日与修改年月日
getMonth() //从0开始计数
getDate()
setYear()
setMonth()
setDate()
获取星期几
getDay() 0-6
获取时分秒
getHours()
getMinutes()
getSeconds()
显示当前时间\停留时间
日期处理
Date.parse("2015-08-24")
new Date(time)
getTime()/setTime()
日期函数的封装
//判断某年份是否为闰年
function isLeap(_year){
return _year%400==0||(_year%100!==0&&_year%4==0);
}
//将日期格式化输出 “2015-08-24”
function formatDate(_date,_separate){//传入日期及想要输出的分隔符
_separate=_separate||"-";
_date=_date.toString();
_date=new Date(_date);
return _date.getFullYear()+_separate+(_date.getMonth()+1)+_separate+_date.getDate();
}
获得某个月份的天数
将字符串转换为日期
"2017&08@08".replace(/(\d+)(\D)/g, function(matched, sub1,sub2, index, str){
console.log(matched, sub1, sub2);
return sub1+"-";
})
判断两个日期相差的天数
获得N天以后的日期
//
日期对象应用:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
</style>
<script>
window.οnlοad=function(){
var t_date=new Date('2049-1-1');
setInterval(function(){
var _date=new Date();
var _year=_date.getFullYear();
var _month=_date.getMonth()+1;
var _tDate=_date.getDate();
var _hour=_date.getHours();
var _minute=_date.getMinutes();
var _second=_date.getSeconds();
var _html="当前时间为:"+_year+"年 "+_month+"月 "+_tDate+"日 "+_hour+"时 "+_minute+"分 "+_second+"秒"+"<br/>";
var tmp=t_date-_date;//单位是毫秒
var t_tDate=Math.floor(tmp/(24*60*60*1000));
var t_hour=Math.floor(tmp%(24*60*60*1000)/(60*60*1000));
var t_minute=Math.floor(tmp%(60*60*1000)/(60*1000));
var t_second=Math.floor(tmp%(60*1000)/1000);
_html+="假设您的死亡时间为2067-9-18年,距离您转世还有:"+t_tDate+"天 "+t_hour+"小时 "+t_minute+"分 "+t_second+"秒 ";
document.getElementById('_div').innerHTML=_html;
//document.getElementById('_div').innerHTML="";
},1000);
}
</script>
</head>
<body id="_body">
<div id="_div">
</div>
</body>
</html>