1 | var setupDefaults = require('./setupDefaults')
|
2 |
|
3 | var helperStringUpperCase = require('./helperStringUpperCase')
|
4 | var helperGetDateFullYear = require('./helperGetDateFullYear')
|
5 | var helperGetDateMonth = require('./helperGetDateMonth')
|
6 |
|
7 | var toStringDate = require('./toStringDate')
|
8 | var getYearWeek = require('./getYearWeek')
|
9 | var getYearDay = require('./getYearDay')
|
10 |
|
11 | var assign = require('./assign')
|
12 |
|
13 | var isValidDate = require('./isValidDate')
|
14 | var isFunction = require('./isFunction')
|
15 |
|
16 | var padStart = require('./padStart')
|
17 |
|
18 | function 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 |
|
30 | function formatDayE (day) {
|
31 | return day === 0 ? 7 : day
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | var 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
|
43 | function 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 |
|
133 | module.exports = toDateString
|