當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JavaScript Date 对象
生活随笔
收集整理的這篇文章主要介紹了
JavaScript Date 对象
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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");
獲取年月日與修改年月日 getYear() / getFullYear() ?獲取4位數年份 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>
轉載于:https://www.cnblogs.com/fanlinqiang/p/7741232.html
總結
以上是生活随笔為你收集整理的JavaScript Date 对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Cocos 2d-X Lua 游戏添加苹
- 下一篇: P1799 数列_NOI导刊2010提高