js获取当前时间

本文最后更新于:2 年前

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>tools</title>
<link rel="stylesheet" href="">
</head>

<body>
<div>
<button type="button" onclick="getNowtime()">获取当前时间</button>
<span id="clock"></span>
</div>
<script type="text/javascript">
function getNowtime() {
var date = new Date();

// string to date
// new Date("2022-03-01").getFullYear()

// var dateStr = '"' + date.getUTCFullYear() + '-' +
// (date.getUTCMonth() + 1).toPaddedString(2) + '-' +
// date.getUTCDate().toPaddedString(2) + 'T' +
// date.getUTCHours().toPaddedString(2) + ':' +
// date.getUTCMinutes().toPaddedString(2) + ':' +
// date.getUTCSeconds().toPaddedString(2) + 'Z"';

console.log(date.getUTCFullYear());
console.log((date.getUTCMonth() + 1).toPaddedString(2));
console.log(date.getUTCDate());
console.log(date.getUTCHours());
console.log(date.getUTCMinutes());
console.log(date.getUTCSeconds());


// var b = a.toLocaleTimeString();
// var c = a.toLocaleDateString();
// document.getElementById("clock").innerHTML = c + "&nbsp" + b;
// document.getElementById("clock").innerHTML = dateStr;
}

Date.prototype.toJSON = function() {
return '"' + this.getUTCFullYear() + '-' +
(this.getUTCMonth() + 1).toPaddedString(2) + '-' +
this.getUTCDate().toPaddedString(2) + 'T' +
this.getUTCHours().toPaddedString(2) + ':' +
this.getUTCMinutes().toPaddedString(2) + ':' +
this.getUTCSeconds().toPaddedString(2) + 'Z"';
};


function convertFullDateToString(date) {
if (date == null) {
date = new Date();
}

var strDate = "";
var year = "";
var month = "";
var day = "";
year = date.getFullYear();
if (parseInt(date.getMonth() + 1, 10) < 10) {
month = "0" + parseInt(date.getMonth() + 1, 10)
} else {
month = parseInt(date.getMonth() + 1, 10);
}
if (parseInt(date.getDate(), 10) < 10) {
day = "0" + parseInt(date.getDate(), 10)
} else {
day = parseInt(date.getDate(), 10);
}
strDate = year + DATE_DELIMITER + month + DATE_DELIMITER + day;
return strDate;
}


function checkFullDate(field) {
field.value = trim(field.value);
var strValue = field.value;
if (strValue == "") {
return false;
}
if (isNumeric(strValue)) {
if (strValue.length > 6 && strValue.length < 9) {
strValue = strValue.substring(0, 4) + DATE_DELIMITER + strValue.substring(4, 6) + DATE_DELIMITER + strValue.substring(6);
field.value = strValue;
} else {
errorMessage("The correct date format is yyyy-MM-dd.");
field.value = "";
field.focus();
field.select();
return false;
}
}
if (!isDate(strValue, DATE_DELIMITER) && !isDate(strValue) || strValue.substring(0, 1) == "0") {
errorMessage("The correct date format is yyyy-MM-dd.");
field.value = "";
field.focus();
field.select();
return false;
}
var splitStrValue = strValue.split(DATE_DELIMITER);
if (splitStrValue[1].length < 2) splitStrValue[1] = "0" + splitStrValue[1];
if (splitStrValue[2].length < 2) splitStrValue[2] = "0" + splitStrValue[2];
field.value = splitStrValue[0] + DATE_DELIMITER + splitStrValue[1] + DATE_DELIMITER + splitStrValue[2];
return true;
}

function checkFullDateTime(field) {
var dateT = field.value;
if (dateT != "") {
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var r = dateT.match(reg);
if (r == null) {
errorMessage("The correct date format is yyyy-MM-dd HH:mm:ss.");
field.value = "";
field.focus();
field.select();
return false;
}
if (!isDateTime(dateT, DATE_DELIMITER) && !isDateTime(dateT) || dateT.substring(0, 1) == "0") {
errorMessage("The correct date format is yyyy-MM-dd HH:mm:ss.");
field.value = "";
field.focus();
field.select();
return false;
}
} else {
return false;
}
return true;
}



function validateDate(form) {

var testResult = true; //store temp test result
var isValid = true;

var focusField = null;
var i = 0;
var fields = new Array();
var oDate = eval("new " + jcv_retrieveFormName(form) + "_date()");
for (var x in oDate) {
if (!jcv_verifyArrayElement(x, oDate[x])) {
continue;
}
//the input maybe an array
var getVar = oDate[x][2];
var isMultiLine = false;
if (getVar("multiLine") == "true") {
isMultiLine = true;
}
var loopFields = form[oDate[x][0]];
if (!jcv_isFieldPresent(loopFields)) { //input not present
continue;
}
var fieldsCount = loopFields.length;
if (isMultiLine) {
if (fieldsCount == undefined) { //is not array
fieldsCount = 1;
var tempLoopField = loopFields;
loopFields = new Array(tempLoopField);
}
} else {
fieldsCount = 1;
var tempLoopField = loopFields;
loopFields = new Array(tempLoopField);
}

for (var index = 0; index < fieldsCount; index++) {
var field = loopFields[index];
//must set isValid = true and testResult = false
if (((index == 0 && fieldsCount == 1) || index > 0) && isValid == false) {
isValid = true;
testResult = false;
}
//only check currentCheckField if it's not null
if (currentCheckField != null && currentCheckField != field) {
continue;
}

//skip the first input if isMultiLine
if (isMultiLine && index == 0) {
continue;
}
if (!jcv_isFieldPresent(field)) {
continue;
}
field.value = trim(field.value);
var value = field.value;
var isStrict = true;
var datePattern = oDate[x][2]("datePatternStrict");
// try loose pattern
if (datePattern == null) {
datePattern = oDate[x][2]("datePattern");
isStrict = false;
}
//continue if has no value or datePattern
if ((value.length <= 0) || (datePattern.length <= 0)) {
continue;
}

if (datePattern == "yyyy-MM-dd") {
//all number,convert to date
if (regExpTest(value, /\d+/g) == true && getByteLength(value) == 8) {
value = value.substring(0, 4) + DATE_DELIMITER +
value.substring(4, 6) + DATE_DELIMITER +
value.substring(6);
}
var reg = new RegExp("^(\\d+)" + DATE_DELIMITER + "(\\d{1,2})" + DATE_DELIMITER + "(\\d{1,2})$");
var r = value.match(reg);
if (r != null) {
var d = new Date(r[1], r[2] - 1, r[3], 1, 1, 1); //month start with zero in js
if (d.getFullYear() == r[1] && d.getMonth() == (r[2] - 1) && d.getDate() == r[3]) {
field.value = r[1] + DATE_DELIMITER + r[2] + DATE_DELIMITER + r[3];
value = field.value;
continue;
}
}
} else if (datePattern == "yyyy-MM") {
var reg = new RegExp("^(\\d+)" + DATE_DELIMITER + "(\\d{1,2})$");
var r = value.match(reg);
if (r != null) {
r[2] = r[2] - 1;
var d = new Date(r[1], r[2], 1, 1, 1, 1); //month start with zero in js
if (d.getFullYear() == r[1] && d.getMonth() == r[2]) {
continue;
}
}
} else if (datePattern == "yyyy-MM-dd HH") {
var reg = new RegExp("^(\\d+)" + DATE_DELIMITER + "(\\d{1,2})" + DATE_DELIMITER + "(\\d{1,2}) (\\d{1,2})$");
var r = value.match(reg);
if (r != null) {
r[2] = r[2] - 1;
var d = new Date(r[1], r[2], r[3], r[4], 1, 1); //month start with zero in js
if (d.getFullYear() == r[1] && d.getMonth() == r[2] && d.getDate() == r[3] && d.getHours() == r[4]) {
continue;
}
}
} else if (datePattern == "yyyy-MM-dd HH:mm") {
var reg = new RegExp("^(\\d+)" + DATE_DELIMITER + "(\\d{1,2})" + DATE_DELIMITER + "(\\d{1,2}) (\\d{1,2}):(\\d{1,2})$");
var r = value.match(reg);
if (r != null) {
r[2] = r[2] - 1;
var d = new Date(r[1], r[2], r[3], r[4], r[5], 1); //month start with zero in js
if (d.getFullYear() == r[1] && d.getMonth() == r[2] && d.getDate() == r[3] && d.getHours() == r[4] &&
d.getMinutes() == r[5]) {
continue;
}
}
} else if (datePattern == "yyyy-MM-dd HH:mm:ss") {
var reg = new RegExp("^(\\d+)" + DATE_DELIMITER + "(\\d{1,2})" + DATE_DELIMITER + "(\\d{1,2}) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})$");
var r = value.match(reg);
if (r != null) {
r[2] = r[2] - 1;
var d = new Date(r[1], r[2], r[3], r[4], r[5], r[6]); //month start with zero in js
if (d.getFullYear() == r[1] && d.getMonth() == r[2] && d.getDate() == r[3] && d.getHours() == r[4] &&
d.getMinutes() == r[5] && d.getSeconds() == r[6]) {
continue;
}
}
} else if (datePattern == "yyyy-MM-dd HH:mm:ss.SSS") {
var reg = new RegExp("^(\\d+)" + DATE_DELIMITER + "(\\d{1,2})" + DATE_DELIMITER + "(\\d{1,2}) (\\d{1,2}):(\\d{1,2}):(\\d{1,2}).(\\d{1,3})$");
var r = value.match(reg);
if (r != null) {
r[2] = r[2] - 1;
var d = new Date(r[1], r[2], r[3], r[4], r[5], r[6]); //month start with zero in js
if (d.getFullYear() == r[1] && d.getMonth() == r[2] && d.getDate() == r[3] && d.getHours() == r[4] &&
d.getMinutes() == r[5] && d.getSeconds() == r[6]) {
continue;
}
}
} else { //if not support then continue;
continue;
}


if (i == 0) {
focusField = field;
}
fields[i++] = oDate[x][1];
isValid = false;
jcv_custom_processFieldAfterValid(isValid, field, fields, i - 1);

}
}
if (fields.length > 0) {
jcv_handleErrors(fields, focusField);
}
if (testResult == false) {
isValid = false;
}
return isValid;
}
</script>
</body>

</html>

js获取当前时间
https://chris-z-su.github.io/2022/10/15/前端/js获取当前时间/
作者
Chris
发布于
2022年10月15日
许可协议