1 //扩展Date的format方法
2 Date.prototype.format = function (format) {
3 var o = {
4 "M+": this.getMonth() + 1,
5 "d+": this.getDate(),
6 "h+": this.getHours(),
7 "m+": this.getMinutes(),
8 "s+": this.getSeconds(),
9 "q+": Math.floor((this.getMonth() + 3) / 3),
10 "S": this.getMilliseconds()
11 }
12 if (/(y+)/.test(format)) {
13 format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
14 }
15 for (var k in o) {
16 if (new RegExp("(" + k + ")").test(format)) {
17 format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
18 }
19 }
20 return format;
21 }
22 /**
23 *转换日期对象为日期字符串
24 * @param date 日期对象
25 * @param isFull 是否为完整的日期数据,
26 * 为true时, 格式如"2000-03-05 01:05:04"
27 * 为false时, 格式如 "2000-03-05"
28 * @return 符合要求的日期字符串
29 */
30 function getSmpFormatDate(date, isFull) {
31 var pattern = "";
32 if (isFull == true || isFull == undefined) {
33 pattern = "yyyy-MM-dd hh:mm:ss";
34 } else {
35 pattern = "yyyy-MM-dd";
36 }
37 return getFormatDate(date, pattern);
38 }
39 /**
40 *转换当前日期对象为日期字符串
41 * @param date 日期对象
42 * @param isFull 是否为完整的日期数据,
43 * 为true时, 格式如"2000-03-05 01:05:04"
44 * 为false时, 格式如 "2000-03-05"
45 * @return 符合要求的日期字符串
46 */
47
48 function getSmpFormatNowDate(isFull) {
49 return getSmpFormatDate(new Date(), isFull);
50 }
51 /**
52 *转换long值为日期字符串
53 * @param l long值
54 * @param isFull 是否为完整的日期数据,
55 * 为true时, 格式如"2000-03-05 01:05:04"
56 * 为false时, 格式如 "2000-03-05"
57 * @return 符合要求的日期字符串
58 */
59
60 function getSmpFormatDateByLong(l, isFull) {
61 return getSmpFormatDate(new Date(l), isFull);
62 }
63 /**
64 *转换long值为日期字符串
65 * @param l long值
66 * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss
67 * @return 符合要求的日期字符串
68 */
69
70 function getFormatDateByLong(l, pattern) {
71 return getFormatDate(new Date(l), pattern);
72 }
73 /**
74 *转换日期对象为日期字符串
75 * @param l long值
76 * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss
77 * @return 符合要求的日期字符串
78 */
79 function getFormatDate(date, pattern) {
80 if (date == undefined) {
81 date = new Date();
82 }
83 if (pattern == undefined) {
84 pattern = "yyyy-MM-dd hh:mm:ss";
85 }
86 return date.format(pattern);
87 }
88
89 //alert(getSmpFormatDate(new Date(1279849429000), true));
90 //alert(getSmpFormatDate(new Date(1279849429000),false));
91 //alert(getSmpFormatDateByLong(1279829423000, true));
92 //alert(getSmpFormatDateByLong(1279829423000,false));
93 //alert(getFormatDateByLong(1279829423000, "yyyy-MM"));
94 //alert(getFormatDate(new Date(1279829423000), "yy-MM"));
95 //alert(getFormatDateByLong(1279849429000, "yyyy-MM hh:mm"));