Posted on 2008-03-04 13:06
puras 閱讀(8608)
評論(1) 編輯 收藏 所屬分類:
JavaScript
好久沒有更新Blog了,一直在忙工作.
今天把前兩天臨時寫的一個工具方法發(fā)布出來,不是很完善,只是為了完成工作而已...
將當(dāng)前的字符串根據(jù)參數(shù)中給定的樣式轉(zhuǎn)換成相應(yīng)的日期對象:
1 String.prototype.toDate = function(style) {
2 if (style == null) style = 'yyyy-MM-dd hh:mm:ss';
3 var o = {
4 'y+' : 'y',
5 'M+' : 'M',
6 'd+' : 'd',
7 'h+' : 'h',
8 'm+' : 'm',
9 's+' : 's'
10 };
11 var result = {
12 'y' : '',
13 'M' : '',
14 'd' : '',
15 'h' : '00',
16 'm' : '00',
17 's' : '00'
18 }
19 var tmp = style;
20 for (var k in o) {
21 if (new RegExp('(' + k + ')').test(style)) {
22 result[o[k]] = this.substring(tmp.indexOf(RegExp.$1), tmp.indexOf(RegExp.$1) + RegExp.$1.length);
23 }
24 }
25 return new Date(result['y'], result['M'] - 1, result['d'], result['h'], result['m'], result['s']);
26 };
因為只是臨時寫寫,功能一定是不完善的,還有待于進(jìn)一步修改.
有興趣的可以一起討論討論...