preface
In the development process, we always encapsulate some public functions as our tools to simplify code or reuse code, for this purpose, I intend to sort out some of the commonly used packaged tool functions in my daily work, this article for the time and date formatting tool function encapsulation.
series
1. Deep and light copy of front-end toolkit
2. Date formatting of the front-end toolkit
3. Anti-shake function of front-end kit
4. Front-end kit widgets
5. Front-end toolkit log beautification
background
Usually in development we encounter various types of Date forms such as the more common new Date() output in the following format
Mon Dec 16 2019 11:01:04 GMT+0800
Or generic string formats such as 2019-12-16 and 2019-12-16 12:00:00 with additional minutes and more personalized separators such as 2019/12/16, December 16,2019, etc. Sometimes we need to use third-party libraries to do more complicated date and time formatting, but sometimes we don’t need to do that much formatting in development, so HERE I simply package a function to apply date formatting in general. Let’s paste the code first
Tool encapsulation
@param cFormat * @param returns {*} */function parseTime(time, cFormat) {
if (arguments.length === 0) {
return null
}
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if ((' ' + time).length === 10) time = parseInt(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()
}
// eslint-disable-next-line
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
let value = formatObj[key]
if (key === 'a') return ['一'.'二'.'三'.'four'.'five'.'六'.'day'][value - 1]
if (result.length > 0 && value < 10) {
value = '0' + value
}
return value || 0
})
// eslint-disable-next-line
return time_str
}
Copy the code
CFormat is a format template string. If it is a date-time stamp, it is converted to the standard date format and pre-converted to a format object such as year, month, day, hour, minute, second. The object is then matched and replaced by re for the year, month, day, hour, minute, second week.
console.log(parseTime(new Date())) //print 2019-12-16 11:10:31
console.log(parseTime(new Date(),'{y}-{m}-{d}')) / /print 2019-12-16
console.log(parseTime(new Date(),'{y}/{m}/{d}')) / /print 2019/12/16
console.log(parseTime(new Date(),'{y}')) / /print 2019
console.log(parseTime(new Date(),'{h}:{i}:{s}')) / /print 11:14:31
console.log(parseTime(new Date(),'{y} - {m} - {a} {d} week')) / /printLog (parseTime(new Date())'2019/12/11'))) / /print 2019-12-11
console.log(parseTime('the 2019-12-11 12:00:00')) / /print 2019-12-11 12:00:00
console.log(parseTime(new Date().getTime())) //printThe 2019-12-16 11:20:17Copy the code
You can pass a timestamp or date object format to fit different formatting templates, or you can pass a default value to date to format the default date
Open source library
Personal home page | bin – UI | bin – admin