js獲取指定格式的時間字串

2022-02-27 17:28:33 字數 762 閱讀 5954

如下:

//

對date的擴充套件,將 date 轉化為指定格式的string

//月(m)、日(d)、小時(h)、分(m)、秒(s)、季度(q) 可以用 1-2 個佔位符,

//年(y)可以用 1-4 個佔位符,毫秒(s)只能用 1 個佔位符(是 1-3 位的數字)

//例子:

//(new date()).format("yyyy-mm-dd hh:mm:ss.s") ==> 2006-07-02 08:09:04.423

//(new date()).format("yyyy-m-d h:m:s.s") ==> 2006-7-2 8:9:4.18

date.prototype.format = function (fmt) ;

if (/(y+)/.test(fmt))

fmt = fmt.replace(regexp.$1, (this.getfullyear() + "").substr(4 - regexp.$1.length));

for (var k in

o)

if (new regexp("(" + k + ")").test(fmt))

fmt = fmt.replace(regexp.$1, (regexp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" +o[k]).length)));

return

fmt;

}

js中按照指定格式獲取當前系統時間

在js 中,var nowtime new date 可獲取當前時間,但是格式通常不是我們想要的,如何更改?關於日期操作,可查詢js中的date物件 var nowtime new date console.log nowtime mon sep 03 2018 14 57 36 gmt 0800 ...

練習 按指定格式拼接字串

題目 分析 1.首先準備乙個int陣列,內容是1,2,3 2.定義乙個方法,用來將陣列變成字串 三要素 返回值型別 string 方法名稱 fromarraytostring 引數列表 int 3.格式 word1 word2 word3 用到 for迴圈,字串拼接,每個陣列元素之前都有乙個word...

js 獲取當前時間特定格式

1.獲取當前時間 var mydate new date 2.獲取時間中的年月日時分秒 mydate.getyear 獲取當前年份 2位 mydate.getfullyear 獲取完整的年份 4位,1970 mydate.getmonth 獲取當前月份 0 11,0代表1月 mydate.getda...