UNPKG

3.09 kBJavaScriptView Raw
1"use strict";
2
3var months = require("months"),
4 days = require("days"),
5 fillo = require("fillo"),
6 ParseIt = require("parse-it").Parser;
7
8var parser = new ParseIt({
9 // Years
10 /// 2015
11 "YYYY": function YYYY(i) {
12 return i.getFullYear();
13 }
14
15 // 15
16 , "YY": function YY(i) {
17 return i.getFullYear() % 100;
18 }
19
20 // Months
21 // January
22 , "MMMM": function MMMM(i) {
23 return months[i.getMonth()];
24 }
25
26 // Jan
27 , "MMM": function MMM(i) {
28 return months.abbr[i.getMonth()];
29 }
30
31 // 01
32 , "MM": function MM(i) {
33 return fillo(i.getMonth() + 1);
34 }
35
36 // 1
37 , "M": function M(i) {
38 return i.getMonth() + 1;
39 }
40
41 // Days
42 // Sunday
43 , "dddd": function dddd(i) {
44 return days[i.getDay()];
45 }
46 // Sun
47 , "ddd": function ddd(i) {
48 return days.abbr[i.getDay()];
49 }
50 // Su
51 , "dd": function dd(i) {
52 return days.short[i.getDay()];
53 }
54 // 0
55 , "d": function d(i) {
56 return i.getDay();
57 }
58
59 // Dates
60 // 06 Day in month
61 , "DD": function DD(i) {
62 return fillo(i.getDate());
63 }
64 // 6 Day in month
65 , "D": function D(i) {
66 return i.getDate();
67 }
68
69 // AM/PM
70 // AM/PM
71 , "A": function A(i) {
72 return i.getHours() >= 12 ? "PM" : "AM";
73 }
74 // am/pm
75 , "a": function a(i) {
76 return i.getHours() >= 12 ? "pm" : "am";
77 }
78
79 // Hours
80 // 08 Hour
81 , "hh": function hh(i) {
82 return fillo(i.getHours() % 12 || 12);
83 }
84 // 8 Hour
85 , "h": function h(i) {
86 return i.getHours() % 12 || 12;
87 }
88 // (alias)
89 , "HH": function HH(i) {
90 return fillo(i.getHours());
91 }
92 // (alias)
93 , "H": function H(i) {
94 return i.getHours();
95 }
96
97 // Minutes
98 // 09 Minute
99 , "mm": function mm(i) {
100 return fillo(i.getMinutes());
101 }
102 // 9 Minute
103 , "m": function m(i) {
104 return i.getMinutes();
105 }
106
107 // Seconds
108 // 09 Seconds
109 , "ss": function ss(i) {
110 return fillo(i.getSeconds());
111 }
112
113 // 9 Seconds
114 , "s": function s(i) {
115 return i.getSeconds();
116 }
117});
118
119/**
120 * formatoid
121 * Formats the date into a given format.
122 *
123 * Usable format fields:
124 *
125 * - **Years**
126 * - `YYYY` (e.g. `"2015"`)
127 * - `YY` (e.g. `"15"`)
128 * - **Months**
129 * - `MMMM` (e.g. `"January"`)
130 * - `MMM` (e.g. `"Jan"`)
131 * - `MM` (e.g. `"01"`)
132 * - `M` (e.g. `"1"`)
133 * - **Days**
134 * - `dddd` (e.g. `"Sunday"`)
135 * - `ddd` (e.g. `"Sun"`)
136 * - `dd` (e.g. `"Su"`)
137 * - `d` (e.g. `"Su"`)
138 * - **Dates**
139 * - `DD` (e.g. `"07"`)
140 * - `D` (e.g. `"7"`)
141 * - **AM/PM**
142 * - `A` (e.g. `"AM"`)
143 * - `a` (e.g. `"pm"`)
144 * - **Hours**
145 * - `hh` (e.g. `"07"`)–12 hour format
146 * - `h` (e.g. `"7"`)
147 * - `HH` (e.g. `"07"`)–24 hour format
148 * - `H` (e.g. `"7"`)
149 * - **Minutes**
150 * - `mm` (e.g. `"07"`)
151 * - `m` (e.g. `"7"`)
152 * - **Seconds**
153 * - `ss` (e.g. `"07"`)
154 * - `s` (e.g. `"7"`)
155 *
156 * @name formatoid
157 * @function
158 * @param {Date} i The date object.
159 * @param {String} f The date format.
160 * @return {String} The formatted date (as string).
161 */
162module.exports = function formatoid(i, f) {
163 return parser.run(f, [i]);
164};
\No newline at end of file