require(['jquery'], function($) {
// 時(shí)間日期格式轉(zhuǎn)換 方法一
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() + 1) > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
str = str.replace(/M/g, (this.getMonth() + 1));
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
}
// ("00" + (d.getUTCMonth() + 1)).slice(-2) 取兩位數(shù)
var myDate = new Date();
console.log(myDate.Format("yyyy年MM月dd日"));
// 時(shí)間日期格式轉(zhuǎn)換 方法二
Date.prototype.format = function(format) {
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds() //millisecond
};
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;
}
//調(diào)用
//new Date().format("yyyy-MM-dd hh:mm:ss");
})
前端基礎(chǔ)全局方法(時(shí)間日期格式轉(zhuǎn)換)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門默伍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來欢嘿,“玉大人衰琐,你說我怎么就攤上這事×侗模” “怎么了羡宙?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長掐隐。 經(jīng)常有香客問我狗热,道長,這世上最難降的妖魔是什么虑省? 我笑而不...
- 正文 為了忘掉前任匿刮,我火速辦了婚禮,結(jié)果婚禮上探颈,老公的妹妹穿的比我還像新娘熟丸。我一直安慰自己,他們只是感情好伪节,可當(dāng)我...
- 文/花漫 我一把揭開白布光羞。 她就那樣靜靜地躺著,像睡著了一般怀大。 火紅的嫁衣襯著肌膚如雪纱兑。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼淫奔!你這毒婦竟也來了山涡?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬榮一對(duì)情侶失蹤唆迁,失蹤者是張志新(化名)和其女友劉穎鸭丛,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體唐责,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡鳞溉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了鼠哥。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片熟菲。...
- 正文 年R本政府宣布呆贿,位于F島的核電站嚷兔,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏做入。R本人自食惡果不足惜谴垫,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望母蛛。 院中可真熱鬧翩剪,春花似錦、人聲如沸彩郊。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽秫逝。三九已至恕出,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間违帆,已是汗流浹背浙巫。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長得像尝胆,于是被迫代替她去往敵國和親丧裁。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 最近因?yàn)轫?xiàng)目需要缓呛,使用到了Excel表數(shù)據(jù)上傳,遇到了一個(gè)讓我非常頭疼的問題杭隙,那就是excel表中的數(shù)據(jù)格式是日期...
- 原因 主要是中國時(shí)區(qū)的問題 解決方法:設(shè)置時(shí)區(qū) 添加以下代碼即可TimeZone tz = TimeZone.ge...