Posted on 2008-03-04 13:06 
puras 阅读(8643) 
评论(1)  编辑  收藏  所属分类: 
JavaScript 
			 
			
		 
		好久没有更新Blog了,一直在忙工作.
今天把前两天临时写的一个工具方法发布出来,不是很完善,只是为了完成工作而已...
将当前的字符串根据参数中给定的样式转换成相应的日期对象:
 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 };
因为只是临时写写,功能一定是不完善的,还有待于进一步修改.
有兴趣的可以一起讨论讨论...