MCPcopy
hub / github.com/lfb/nodejs-koa-blog / parseTime

Function parseTime

admin-blog/src/utils/index.js:11–52  ·  view source on GitHub ↗
(time, cFormat)

Source from the content-addressed store, hash-verified

9 * @returns {string | null}
10 */
11export function parseTime(time, cFormat) {
12 if (arguments.length === 0 || !time) {
13 return null
14 }
15 const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
16 let date
17 if (typeof time === 'object') {
18 date = time
19 } else {
20 if ((typeof time === 'string')) {
21 if ((/^[0-9]+$/.test(time))) {
22 // support "1548221490638"
23 time = parseInt(time)
24 } else {
25 // support safari
26 // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
27 time = time.replace(new RegExp(/-/gm), '/')
28 }
29 }
30
31 if ((typeof time === 'number') && (time.toString().length === 10)) {
32 time = time * 1000
33 }
34 date = new Date(time)
35 }
36 const formatObj = {
37 y: date.getFullYear(),
38 m: date.getMonth() + 1,
39 d: date.getDate(),
40 h: date.getHours(),
41 i: date.getMinutes(),
42 s: date.getSeconds(),
43 a: date.getDay()
44 }
45 const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
46 const value = formatObj[key]
47 // Note: getDay() returns 0 on Sunday
48 if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
49 return value.toString().padStart(2, '0')
50 })
51 return time_str
52}
53
54/**
55 * @param {number} time

Callers 2

parseTime.spec.jsFile · 0.90
formatTimeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected