date-and-time 0.7.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LOCALE.md +85 -0
- package/PLUGINS.md +207 -0
- package/README.md +440 -199
- package/benchmark/bench.js +40 -0
- package/date-and-time.js +263 -192
- package/date-and-time.min.js +12 -10
- package/locale/ar.js +16 -14
- package/locale/az.js +19 -16
- package/locale/bn.js +20 -17
- package/locale/cs.js +13 -10
- package/locale/de.js +14 -11
- package/locale/dk.js +13 -10
- package/locale/el.js +18 -15
- package/locale/es.js +18 -15
- package/locale/fa.js +16 -14
- package/locale/fr.js +14 -11
- package/locale/hi.js +20 -17
- package/locale/hu.js +14 -11
- package/locale/id.js +19 -16
- package/locale/it.js +14 -11
- package/locale/ja.js +14 -11
- package/locale/jv.js +19 -16
- package/locale/ko.js +14 -11
- package/locale/my.js +15 -13
- package/locale/nl.js +17 -14
- package/locale/pa-in.js +22 -20
- package/locale/pl.js +17 -14
- package/locale/pt.js +19 -16
- package/locale/ro.js +13 -10
- package/locale/ru.js +19 -16
- package/locale/sr.js +13 -10
- package/locale/th.js +14 -11
- package/locale/tr.js +13 -10
- package/locale/uk.js +21 -20
- package/locale/uz.js +13 -10
- package/locale/vi.js +14 -11
- package/locale/zh-cn.js +21 -18
- package/locale/zh-tw.js +20 -17
- package/package.json +4 -1
- package/plugin/meridiem.js +42 -0
- package/plugin/ordinal.js +38 -0
package/LOCALE.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# Locale
|
|
2
|
+
|
|
3
|
+
Month, day of week, and meridiem (am / pm) the `format()` outputs are usually in English, and the `parse()` also assumes that a passed date string is English.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
If you would like to use any other language in these functions, switch as follows:
|
|
8
|
+
|
|
9
|
+
- Node.js:
|
|
10
|
+
|
|
11
|
+
```javascript
|
|
12
|
+
const date = require('date-and-time');
|
|
13
|
+
require('date-and-time/locale/fr');
|
|
14
|
+
|
|
15
|
+
date.locale('fr'); // French
|
|
16
|
+
date.format(new Date(), 'dddd D MMMM'); // => 'lundi 11 janvier'
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
- With a transpiler:
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
import date from 'date-and-time';
|
|
23
|
+
import 'date-and-time/locale/it';
|
|
24
|
+
|
|
25
|
+
date.locale('it'); // Italian
|
|
26
|
+
date.format(new Date(), 'dddd D MMMM'); // => 'Lunedì 11 gennaio'
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
- With an older browser:
|
|
30
|
+
|
|
31
|
+
```html
|
|
32
|
+
<script src="/path/to/date-and-time.min.js"></script>
|
|
33
|
+
<script src="/path/to/locale/zh-cn.js"></script>
|
|
34
|
+
|
|
35
|
+
<script>
|
|
36
|
+
date.locale('zh-cn'); // Chinese
|
|
37
|
+
date.format(new Date(), 'MMMD日dddd'); // => '1月11日星期一'
|
|
38
|
+
</script>
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### NOTE
|
|
42
|
+
|
|
43
|
+
- You have to import (or require) in advance the all locale modules that you are going to switch to.
|
|
44
|
+
- The locale will be actually switched after executing `locale('xx')`.
|
|
45
|
+
- You could return the locale to English by executing `locale ('en')`.
|
|
46
|
+
|
|
47
|
+
## Supported List
|
|
48
|
+
|
|
49
|
+
For now, it supports the following languages:
|
|
50
|
+
|
|
51
|
+
```text
|
|
52
|
+
Arabic (ar)
|
|
53
|
+
Azerbaijani (az)
|
|
54
|
+
Bengali (bn)
|
|
55
|
+
Burmese (my)
|
|
56
|
+
Chinese (zh-cn)
|
|
57
|
+
Chinese (zh-tw)
|
|
58
|
+
Czech (cs)
|
|
59
|
+
Danish (dk)
|
|
60
|
+
Dutch (nl)
|
|
61
|
+
English (en)
|
|
62
|
+
French (fr)
|
|
63
|
+
German (de)
|
|
64
|
+
Greek (el)
|
|
65
|
+
Hindi (hi)
|
|
66
|
+
Hungarian (hu)
|
|
67
|
+
Indonesian (id)
|
|
68
|
+
Italian (it)
|
|
69
|
+
Japanese (ja)
|
|
70
|
+
Javanese (jv)
|
|
71
|
+
Korean (ko)
|
|
72
|
+
Persian (fa)
|
|
73
|
+
Polish (pl)
|
|
74
|
+
Portuguese (pt)
|
|
75
|
+
Punjabi (pa-in)
|
|
76
|
+
Romanian (ro)
|
|
77
|
+
Russian (ru)
|
|
78
|
+
Serbian (sr)
|
|
79
|
+
Spanish (es)
|
|
80
|
+
Thai (th)
|
|
81
|
+
Turkish (tr)
|
|
82
|
+
Ukrainian (uk)
|
|
83
|
+
Uzbek (uz)
|
|
84
|
+
Vietnamese (vi)
|
|
85
|
+
```
|
package/PLUGINS.md
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Plugins
|
|
2
|
+
|
|
3
|
+
As this library is oriented towards minimalism, it might be lacking in functionality for some developers. Plugin is the most realistic solution for solving such dissatisfaction.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
In this section it describes how to use official plugins.
|
|
8
|
+
|
|
9
|
+
- Node.js:
|
|
10
|
+
|
|
11
|
+
```javascript
|
|
12
|
+
const date = require('date-and-time');
|
|
13
|
+
require('date-and-time/plugin/foobar');
|
|
14
|
+
|
|
15
|
+
date.plugin('foobar');
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
- With a transpiler:
|
|
19
|
+
|
|
20
|
+
```javascript
|
|
21
|
+
import date from 'date-and-time';
|
|
22
|
+
import 'date-and-time/plugin/foobar';
|
|
23
|
+
|
|
24
|
+
date.plugin('foobar');
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
- With an older browser:
|
|
28
|
+
|
|
29
|
+
```html
|
|
30
|
+
<script src="/path/to/date-and-time.min.js"></script>
|
|
31
|
+
<script src="/path/to/plugin/foobar.js"></script>
|
|
32
|
+
|
|
33
|
+
<script>
|
|
34
|
+
date.plugin('foobar');
|
|
35
|
+
</script>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Official Plugins
|
|
41
|
+
|
|
42
|
+
### 1. Meridiem
|
|
43
|
+
|
|
44
|
+
This plugin extends meridiem notation (`AA`, `a` and `aa`).
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
// Import "medidiem" plugin.
|
|
48
|
+
date.plugin('meridiem');
|
|
49
|
+
|
|
50
|
+
// This is a default A token.
|
|
51
|
+
date.format(new Date(), 'hh:mm A'); // => '12:34 p.m.'
|
|
52
|
+
|
|
53
|
+
// These are extended tokens.
|
|
54
|
+
date.format(new Date(), 'hh:mm AA'); // => '12:34 PM'
|
|
55
|
+
date.format(new Date(), 'hh:mm a'); // => '12:34 P.M.'
|
|
56
|
+
date.format(new Date(), 'hh:mm aa'); // => '12:34 pm'
|
|
57
|
+
|
|
58
|
+
// The parse() comes to interpret all these meridiem notation with only A token.
|
|
59
|
+
date.parse('12:34 p.m.', 'hh:mm A'); // => Jan. 1 1970 12:34:00
|
|
60
|
+
date.parse('12:34 PM', 'hh:mm A'); // => Jan. 1 1970 12:34:00
|
|
61
|
+
date.parse('12:34 P.M.', 'hh:mm A'); // => Jan. 1 1970 12:34:00
|
|
62
|
+
date.parse('12:34 pm', 'hh:mm A'); // => Jan. 1 1970 12:34:00
|
|
63
|
+
|
|
64
|
+
// The new tokens cannot be used unlike the format().
|
|
65
|
+
date.parse('12:34 PM', 'hh:mm AA'); // => Invalid Date
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 2. Ordinal
|
|
69
|
+
|
|
70
|
+
This plugin adds ordinal notation (`DDD`).
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
// Import "ordinal" plugin.
|
|
74
|
+
date.plugin('ordinal');
|
|
75
|
+
|
|
76
|
+
// These are default D/DD tokens.
|
|
77
|
+
date.format(new Date(), 'MMM. D YYYY'); // => Jan. 1 2019
|
|
78
|
+
date.format(new Date(), 'MMM. DD YYYY'); // => Jan. 01 2019
|
|
79
|
+
|
|
80
|
+
// The DDD token outputs ordinal number of day.
|
|
81
|
+
date.format(new Date(), 'MMM. DDD YYYY'); // => Jan. 1st 2019
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## Writing a Plugin
|
|
87
|
+
|
|
88
|
+
You could not only use official plugins, but define your own tokens or modify the behavior of existing tokens. Hereafter, it describes about how to write your own plugin.
|
|
89
|
+
|
|
90
|
+
Tokens in this library have the following rules:
|
|
91
|
+
|
|
92
|
+
- All of the characters must be the same alphabet (`A-Z, a-z`).
|
|
93
|
+
|
|
94
|
+
```javascript
|
|
95
|
+
'E' // Good
|
|
96
|
+
'EE' // Good
|
|
97
|
+
'EEEEEEEEEE' // Good, but why so long!?
|
|
98
|
+
'EES' // Not good
|
|
99
|
+
'???' // Not good
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
- It is case sensitive.
|
|
103
|
+
|
|
104
|
+
```javascript
|
|
105
|
+
'eee' // Good
|
|
106
|
+
'Eee' // Not good
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
- To the parser, it is not able to add new alphabet's token.
|
|
110
|
+
|
|
111
|
+
```javascript
|
|
112
|
+
'EEE' // This is not able to add.
|
|
113
|
+
'YYY' // This is OK because a `Y` token is existing in the parser.
|
|
114
|
+
'SSS' // This is modifying, not adding. Because the same token is existing.
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Example 1
|
|
118
|
+
|
|
119
|
+
This is a plugin named `AMPM`. It replaces the meridiem words from `a.m./p.m.` to `AM/PM`.
|
|
120
|
+
|
|
121
|
+
```javascript
|
|
122
|
+
(global => {
|
|
123
|
+
|
|
124
|
+
const exec = date => {
|
|
125
|
+
// This is the body of the plugin.
|
|
126
|
+
date.plugin('AMPM', {
|
|
127
|
+
res: {
|
|
128
|
+
A: ['AM', 'PM']
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
if (typeof module === 'object' && typeof module.exports === 'object') {
|
|
134
|
+
(module.paths || []).push('./');
|
|
135
|
+
exec(require('date-and-time'));
|
|
136
|
+
} else if (typeof define === 'function' && define.amd) {
|
|
137
|
+
define(['date-and-time'], exec);
|
|
138
|
+
} else {
|
|
139
|
+
exec(global.date);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
})(this);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Example 2
|
|
146
|
+
|
|
147
|
+
This is the above `ordinal` plugin. This plugin adds `DDD` token to return ordinal number of day.
|
|
148
|
+
|
|
149
|
+
```javascript
|
|
150
|
+
(global => {
|
|
151
|
+
|
|
152
|
+
const exec = date => {
|
|
153
|
+
// This is the body of the plugin.
|
|
154
|
+
date.plugin('ordinal', {
|
|
155
|
+
formatter: {
|
|
156
|
+
DDD: function (d) {
|
|
157
|
+
var day = d.getDate();
|
|
158
|
+
|
|
159
|
+
switch (day) {
|
|
160
|
+
case 1:
|
|
161
|
+
case 21:
|
|
162
|
+
case 31:
|
|
163
|
+
return day + 'st';
|
|
164
|
+
case 2:
|
|
165
|
+
case 22:
|
|
166
|
+
return day + 'nd';
|
|
167
|
+
case 3:
|
|
168
|
+
case 23:
|
|
169
|
+
return day + 'rd';
|
|
170
|
+
default:
|
|
171
|
+
return day + 'th';
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
if (typeof module === 'object' && typeof module.exports === 'object') {
|
|
179
|
+
(module.paths || []).push('./');
|
|
180
|
+
exec(require('date-and-time'));
|
|
181
|
+
} else if (typeof define === 'function' && define.amd) {
|
|
182
|
+
define(['date-and-time'], exec);
|
|
183
|
+
} else {
|
|
184
|
+
exec(global.date);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
})(this);
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
## Direct Replacement
|
|
193
|
+
|
|
194
|
+
All you are enough to change a bit the default behavior of this library? You are not going to write a plugin? Of course, you could replace the default behavior directly like this:
|
|
195
|
+
|
|
196
|
+
```javascript
|
|
197
|
+
date.format(new Date(), 'hh:mm A'); // => 11:20 p.m.
|
|
198
|
+
|
|
199
|
+
// Replace the words that a.m./p.m. to AM/PM.
|
|
200
|
+
date.extend({ res: { A: ['AM', 'PM'] } });
|
|
201
|
+
|
|
202
|
+
date.format(new Date(), 'hh:mm A'); // => 11:20 PM
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Hint
|
|
206
|
+
|
|
207
|
+
The `extend()` can be regarded as an unnamed `plugin()`.
|