JS實(shí)現(xiàn)時(shí)間格式化的方式匯總
//擴(kuò)展Date的format方法
Date.prototype.format = function (format) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}
/**
*轉(zhuǎn)換日期對(duì)象為日期字符串
* @param date 日期對(duì)象
* @param isFull 是否為完整的日期數(shù)據(jù),
* 為true時(shí), 格式如"2000-03-05 01:05:04"
* 為false時(shí), 格式如 "2000-03-05"
* @return 符合要求的日期字符串
*/
function getSmpFormatDate(date, isFull) {
var pattern = "";
if (isFull == true || isFull == undefined) {
pattern = "yyyy-MM-dd hh:mm:ss";
} else {
pattern = "yyyy-MM-dd";
}
return getFormatDate(date, pattern);
}
/**
*轉(zhuǎn)換當(dāng)前日期對(duì)象為日期字符串
* @param date 日期對(duì)象
* @param isFull 是否為完整的日期數(shù)據(jù),
* 為true時(shí), 格式如"2000-03-05 01:05:04"
* 為false時(shí), 格式如 "2000-03-05"
* @return 符合要求的日期字符串
*/
function getSmpFormatNowDate(isFull) {
return getSmpFormatDate(new Date(), isFull);
}
/**
*轉(zhuǎn)換long值為日期字符串
* @param l long值
* @param isFull 是否為完整的日期數(shù)據(jù),
* 為true時(shí), 格式如"2000-03-05 01:05:04"
* 為false時(shí), 格式如 "2000-03-05"
* @return 符合要求的日期字符串
*/
function getSmpFormatDateByLong(l, isFull) {
return getSmpFormatDate(new Date(l), isFull);
}
/**
*轉(zhuǎn)換long值為日期字符串
* @param l long值
* @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss
* @return 符合要求的日期字符串
*/
function getFormatDateByLong(l, pattern) {
return getFormatDate(new Date(l), pattern);
}
/**
*轉(zhuǎn)換日期對(duì)象為日期字符串
* @param l long值
* @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss
* @return 符合要求的日期字符串
*/
function getFormatDate(date, pattern) {
if (date == undefined) {
date = new Date();
}
if (pattern == undefined) {
pattern = "yyyy-MM-dd hh:mm:ss";
}
return date.format(pattern);
}
//alert(getSmpFormatDate(new Date(1279849429000), true));
//alert(getSmpFormatDate(new Date(1279849429000),false));
//alert(getSmpFormatDateByLong(1279829423000, true));
//alert(getSmpFormatDateByLong(1279829423000,false));
//alert(getFormatDateByLong(1279829423000, "yyyy-MM"));
//alert(getFormatDate(new Date(1279829423000), "yy-MM"));
//alert(getFormatDateByLong(1279849429000, "yyyy-MM hh:mm"));
相關(guān)文章
原生js獲取寬高與jquery獲取寬高的方法關(guān)系對(duì)比
這篇文章主要介紹了原生js獲取寬高與jquery獲取寬高的方法關(guān)系對(duì)比,需要的朋友可以參考下2014-04-04JavaScript實(shí)現(xiàn)微信小程序打卡時(shí)鐘項(xiàng)目實(shí)例
這篇文章主要為大家介紹了JavaScript實(shí)現(xiàn)微信小程序打卡時(shí)鐘項(xiàng)目實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04微信小程序天氣預(yù)報(bào)功能實(shí)現(xiàn)(支持自動(dòng)定位,附源碼)
對(duì)于一個(gè)經(jīng)常出門(mén)在外的人,關(guān)注天氣是至關(guān)重要的,下面這篇文章主要給大家介紹了關(guān)于微信小程序天氣預(yù)報(bào)功能實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),支持自動(dòng)定位,需要的朋友可以參考下2022-04-04JS選取DOM元素常見(jiàn)操作方法實(shí)例分析
這篇文章主要介紹了JS選取DOM元素常見(jiàn)操作方法,結(jié)合實(shí)例形式分析了javascript針對(duì)dom元素的常見(jiàn)選取與樣式修改相關(guān)操作技巧,需要的朋友可以參考下2018-12-12