You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.6 KiB
50 lines
1.6 KiB
function getCurrentTime() {
|
|
const yy = new Date().getFullYear()
|
|
const mm = new Date().getMonth() + 1
|
|
const dd = new Date().getDate()
|
|
const hh = new Date().getHours()
|
|
const mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
|
|
const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
|
|
const time = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
|
|
return time
|
|
}
|
|
|
|
function parseTime(time, cFormat) {
|
|
if (arguments.length === 0) {
|
|
return null
|
|
}
|
|
var format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
|
|
var date
|
|
if (typeof time === 'undefined' || time === null || time === 'null') {
|
|
return ''
|
|
} else if (typeof time === 'object') {
|
|
date = time
|
|
} else {
|
|
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
|
|
time = parseInt(time)
|
|
}
|
|
if ((typeof time === 'number') && (time.toString().length === 10)) {
|
|
time = time * 1000
|
|
}
|
|
date = new Date(time)
|
|
}
|
|
const formatObj = {
|
|
y: date.getFullYear(),
|
|
m: date.getMonth() + 1,
|
|
d: date.getDate(),
|
|
h: date.getHours(),
|
|
i: date.getMinutes(),
|
|
s: date.getSeconds(),
|
|
a: date.getDay()
|
|
}
|
|
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
|
var value = formatObj[key]
|
|
// Note: getDay() returns 0 on Sunday
|
|
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
|
|
if (result.length > 0 && value < 10) {
|
|
value = '0' + value
|
|
}
|
|
return value || 0
|
|
})
|
|
return time_str
|
|
}
|