UNPKG

4.79 kBJavaScriptView Raw
1var setupDefaults = require('./setupDefaults')
2
3var helperStringUpperCase = require('./helperStringUpperCase')
4var helperGetDateFullYear = require('./helperGetDateFullYear')
5var helperGetDateMonth = require('./helperGetDateMonth')
6
7var toStringDate = require('./toStringDate')
8var getYearWeek = require('./getYearWeek')
9var getYearDay = require('./getYearDay')
10
11var assign = require('./assign')
12
13var isValidDate = require('./isValidDate')
14var isFunction = require('./isFunction')
15
16var padStart = require('./padStart')
17
18function handleCustomTemplate (date, formats, match, value) {
19 var format = formats[match]
20 if (format) {
21 if (isFunction(format)) {
22 return format(value, match, date)
23 } else {
24 return format[value]
25 }
26 }
27 return value
28}
29
30function formatDayE (day) {
31 return day === 0 ? 7 : day
32}
33
34/**
35 * 日期格式化为字符串,转义符号 []
36 *
37 * @param {Date} date 日期或数字
38 * @param {String} format 输出日期格式(年份(yy|yyyy)、月份(M|MM自动补0)、天(d|dd自动补0)、12小时制(h|hh自动补0)、24小时制(H|HH自动补0)、分钟(m|mm自动补0)、秒(s|ss自动补0)、毫秒(S|SSS自动补0)、D当年的第几天、a/A上午下午、e/E星期几、w当年的第几周、W当月的第几周、q当年第几个季度、Z时区)
39 * @param {Object} options {formats: {q: ['日', '一', '二', '三', '四', '五', '六'], E: function (value, match, date) {return '三'}, }} 自定义格式化模板
40 * @return {String}
41 */
42var dateFormatRE = /\[([^\]]+)]|y{2,4}|M{1,2}|d{1,2}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|S{1,3}|Z{1,2}|W{1,2}|D{1,3}|[aAeEq]/g
43function toDateString (date, format, options) {
44 if (date) {
45 date = toStringDate(date)
46 if (isValidDate(date)) {
47 var result = format || setupDefaults.formatString
48 var hours = date.getHours()
49 var apm = hours < 12 ? 'am' : 'pm'
50 var formats = assign({}, setupDefaults.formatStringMatchs, options ? options.formats : null)
51 var fy = function (match, length) {
52 return ('' + helperGetDateFullYear(date)).substr(4 - length)
53 }
54 var fM = function (match, length) {
55 return padStart(helperGetDateMonth(date) + 1, length, '0')
56 }
57 var fd = function (match, length) {
58 return padStart(date.getDate(), length, '0')
59 }
60 var fH = function (match, length) {
61 return padStart(hours, length, '0')
62 }
63 var fh = function (match, length) {
64 return padStart(hours <= 12 ? hours : hours - 12, length, '0')
65 }
66 var fm = function (match, length) {
67 return padStart(date.getMinutes(), length, '0')
68 }
69 var fs = function (match, length) {
70 return padStart(date.getSeconds(), length, '0')
71 }
72 var fS = function (match, length) {
73 return padStart(date.getMilliseconds(), length, '0')
74 }
75 var fZ = function (match, length) {
76 var zoneHours = date.getTimezoneOffset() / 60 * -1
77 return handleCustomTemplate(date, formats, match, (zoneHours >= 0 ? '+' : '-') + padStart(zoneHours, 2, '0') + (length === 1 ? ':' : '') + '00')
78 }
79 var fW = function (match, length) {
80 return padStart(handleCustomTemplate(date, formats, match, getYearWeek(date)), length, '0')
81 }
82 var fD = function (match, length) {
83 return padStart(handleCustomTemplate(date, formats, match, getYearDay(date)), length, '0')
84 }
85 var parseDates = {
86 yyyy: fy,
87 yy: fy,
88 MM: fM,
89 M: fM,
90 dd: fd,
91 d: fd,
92 HH: fH,
93 H: fH,
94 hh: fh,
95 h: fh,
96 mm: fm,
97 m: fm,
98 ss: fs,
99 s: fs,
100 SSS: fS,
101 S: fS,
102 ZZ: fZ,
103 Z: fZ,
104 WW: fW,
105 W: fW,
106 DDD: fD,
107 D: fD,
108 a: function (match) {
109 return handleCustomTemplate(date, formats, match, apm)
110 },
111 A: function (match) {
112 return handleCustomTemplate(date, formats, match, helperStringUpperCase(apm))
113 },
114 e: function (match) {
115 return handleCustomTemplate(date, formats, match, date.getDay())
116 },
117 E: function (match) {
118 return handleCustomTemplate(date, formats, match, formatDayE(date.getDay()))
119 },
120 q: function (match) {
121 return handleCustomTemplate(date, formats, match, Math.floor((helperGetDateMonth(date) + 3) / 3))
122 }
123 }
124 return result.replace(dateFormatRE, function (match, skip) {
125 return skip || (parseDates[match] ? parseDates[match](match, match.length) : match)
126 })
127 }
128 return 'Invalid Date'
129 }
130 return ''
131}
132
133module.exports = toDateString