欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用js獲取當(dāng)前年月日的方法及格式整理匯總

 更新時(shí)間:2022年12月05日 09:30:48   作者:不染-9732  
很多時(shí)候我們需要在前臺(tái)獲取當(dāng)前日期,下面這篇文章主要給大家介紹了關(guān)于使用js獲取當(dāng)前年月日的方法及格式整理的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

1. 獲取完整時(shí)間戳

var date = new Date(); ====》 Wed Sep 07 2022 17:18:30 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間) 小節(jié)2中的數(shù)據(jù)以此為基礎(chǔ)

2. 整理年月日數(shù)據(jù)為: 2022-02-09 的格式

var year = date.getFullYear();

var month = date.getMonth() + 1;

var day = date.getDate();

month = (month > 9) ? month : (“0” + month);
day = (day < 10) ? (“0” + day) : day;
var today = year + “-” + month + “-” + day;

3. 關(guān)于日期數(shù)據(jù)的額外補(bǔ)充

date.getYear(); // 獲取當(dāng)前年份(2 位)
date.getFullYear(); // 獲取完整的年份(4 位, 1970-???)
date.getMonth(); // 獲取當(dāng)前月份(0-11,0 代表 1 月)
date.getDate(); // 獲取當(dāng)前日(1-31)
date.getDay(); // 獲取當(dāng)前星期 X(0-6,0 代表星期天)
date.getTime(); // 獲取當(dāng)前時(shí)間(從 1970.1.1 開(kāi)始的毫秒數(shù))
date.getHours(); // 獲取當(dāng)前小時(shí)數(shù)(0-23)
date.getMinutes(); // 獲取當(dāng)前分鐘數(shù)(0-59)
date.getSeconds(); // 獲取當(dāng)前秒數(shù)(0-59)
date.getMilliseconds(); // 獲取當(dāng)前毫秒數(shù)(0-999)
date.toLocaleDateString(); // 獲取當(dāng)前日期
date.toLocaleTimeString(); // 獲取當(dāng)前時(shí)間
date.toLocaleString( ); // 獲取日期與時(shí)間

4. 判斷閏年

 var date1 = new Date();
 Date.prototype.isLeapYear = function() {   
    return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0)));   
 }   
 console.log(date1.isLeapYear()); // false

5.日期格式化

/** 格式 YYYY/yyyy/YY/yy 表示年份  
  * MM/M 月份  
  * W/w 星期  
  * dd/DD/d/D 日期  
  * hh/HH/h/H 時(shí)間  
  * mm/m 分鐘  
  * ss/SS/s/S 秒  
**/
//---------------------------------------------------  
Date.prototype.Format = function(formatStr)  {   
    var str = formatStr;   
    var Week = ['日','一','二','三','四','五','六'];  
  
    str=str.replace(/yyyy|YYYY/,this.getFullYear());   
    str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));   
  
    str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + (this.getMonth()+1));      
    str=str.replace(/M/g,this.getMonth());   
 
 
    str=str.replace(/w|W/g,Week[this.getDay()]);   
  
    str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());   
    str=str.replace(/d|D/g,this.getDate());   
  
    str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());   
    str=str.replace(/h|H/g,this.getHours());   
    str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());   
    str=str.replace(/m/g,this.getMinutes());   
  
    str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());   
    str=str.replace(/s|S/g,this.getSeconds());   
  
    return str;   
}   
var date=new Date();
var res=date.Format("yyyy-MM-dd HH:mm:ss  星期W");
console.info(res); //2022-01-07 16:18:36  星期五

總結(jié)

到此這篇關(guān)于使用js獲取當(dāng)前年月日的方法及格式整理匯總的文章就介紹到這了,更多相關(guān)js獲取當(dāng)前年月日及格式整理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論