昨天写的时候,在下面代码处就碰到了问题,
如下:
if(data.zero) { if(data.Month_zero) { if(time.month < 10) { time.month = "0" + time.month; } } if(data.Day_zero) { if(time.day < 10) { time.day = "0" + time.day; } } if(data.Hours_zero) { if(time.hours < 10) { time.hours = "0" + time.hours; } } if(data.Min_zero) { if(time.minutes < 10) { time.minutes = "0" + time.minutes; } } if(data.Second_zero) { if(time.seconds < 10) { time.seconds = "0" + time.seconds; } } }
因为代码耦合程度太高,所以想用一个函数来封装,如下:1
2
3
4
5function Zero(a) {
if(a < 10) {
a = “0” + a;
}
}
却无法让其改变原始的参数值,挣扎了许久,就放下了。
今天经过一天的征战,终于得以解决。源码如下:
timeDate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121/
timeDate.js
by LafiteWu
by QQ:863512936
2018-01-05 v1.1
/
(function($) {
$.fn.timeDate = function(options) {
var that = $(this);
var defaults = {
show: “Y-M-D-W”, //显示形式
zero: true, // 是否开启零,也是总开关,当它值为false时,下面的值无效
Month_zero: true, // 月份是否加零
Day_zero: true, // 日期上是否加零
Hours_zero: true, // 小时前是否加零
Min_zero: true, // 分钟前是否加零
Second_zero: true // 秒钟前加零
};
var data = $.extend(defaults,options);
var now = new Date();
var weekday = [“星期天”,”星期一”,”星期二”,”星期三”,”星期四”,”星期五”,”星期六”];
var time = {
year: now.getFullYear(),
month: now.getMonth()+1,
day: now.getDate(),
week: weekday[now.getDay()],
hours: now.getHours(),
minutes: now.getMinutes(),
seconds: now.getSeconds(),
Zero:function(a) {
if(a < 10) {
return a = “0” + a;
}else {
return a;
}
},
Html:function(a) {
that.html(a);
}
};
if(data.zero) {
if(data.Month_zero) {
time.month = time.Zero(time.month);
}
if(data.Day_zero) {
time.day = time.Zero(time.day);
}
if(data.Hours_zero) {
time.hours = time.Zero(time.hours);
}
if(data.Min_zero) {
time.minutes = time.Zero(time.minutes);
}
if(data.Second_zero) {
time.seconds = time.Zero(time.seconds);
}
}
switch(data.show) {
case(“Y”):
time.Html(time.year);
break;
case(“M”):
time.Html(time.month);
break;
case(“D”):
time.Html(time.day);
break;
case(“H”):
time.Html(time.hours);
break;
case(“m”):
time.Html(time.minutes);
break;
case(“S”):
time.Html(time.seconds);
break;
case(“W”):
time.Html(time.week);
break;
case(“Y-M”):
time.Html(time.year+”年”+time.month+”月”);
break;
case(“Y/M”):
time.Html(time.year+”/“+time.month);
break;
case(“M-D”):
time.Html(time.month+”月”+time.day+”日”);
break;
case(“M/D”):
time.Html(time.month+”/“+time.day);
break;
case(“M-D-W”):
time.Html(time.month+”月”+time.day+”日 “+time.week);
break;
case(“M/D/W”):
time.Html(time.month+”/“+time.day+” “+time.week);
break;
case(“Y-M-D”):
time.Html(time.year+”年”+time.month+”月”+time.day+”日”);
break;
case(“Y-M-D-W”):
time.Html(time.year+”年”+time.month+”月”+time.day+”日 “+time.week);
break;
case(“Y/M/D/W”):
time.Html(time.year+”/“+time.month+”/“+time.day+” “+time.week);
break;
case(“Y/M/D”):
time.Html(time.year+”/“+time.month+”/“+time.day);
break;
case(“H-m”):
time.Html(time.hours+”:”+time.minutes);
break;
case(“H-m-S”):
time.Html(time.hours+”:”+time.minutes+”:”+time.seconds);
break;
default:
throw new Error(“Check if your format is correct”);
}
}
})(jQuery);
好,优化完成。