date-and-time 0.4.2 → 0.6.3
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/README.md +22 -9
- package/date-and-time.js +8 -9
- package/date-and-time.min.js +8 -8
- package/locale/es.js +5 -5
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
[](https://circleci.com/gh/knowledgecode/date-and-time)
|
|
3
3
|
|
|
4
4
|
## WHY
|
|
5
|
-
|
|
5
|
+
Since JS modules are usually used in combination, we think trying to keep the size of each module small is important. This date time utility is one of the modules aiming for minimal and efficient.
|
|
6
6
|
|
|
7
7
|
## Features
|
|
8
8
|
- Minimalist. Only has **1.9k** (minified and gzipped)
|
|
9
9
|
- Universal (Isomorphic)
|
|
10
10
|
- Multi language support
|
|
11
11
|
- Not extending built-in Date object
|
|
12
|
+
- Browserify support
|
|
12
13
|
- Legacy IE support. IE6+
|
|
13
14
|
|
|
14
15
|
## Installation
|
|
@@ -16,7 +17,7 @@ via npm:
|
|
|
16
17
|
```shell
|
|
17
18
|
$ npm install date-and-time --save
|
|
18
19
|
```
|
|
19
|
-
via Bower:
|
|
20
|
+
via Bower (DEPRECATED):
|
|
20
21
|
```shell
|
|
21
22
|
$ bower install date-and-time
|
|
22
23
|
```
|
|
@@ -25,11 +26,21 @@ directly:
|
|
|
25
26
|
<script src="date-and-time.min.js"></script>
|
|
26
27
|
```
|
|
27
28
|
|
|
29
|
+
## Changes
|
|
30
|
+
- `parse()`
|
|
31
|
+
- Parsing a string stricter
|
|
32
|
+
- Added white space as a wildcard character
|
|
33
|
+
- Fixed a daylight saving time issue
|
|
34
|
+
|
|
28
35
|
## Usage
|
|
29
36
|
Node.js:
|
|
30
37
|
```javascript
|
|
31
38
|
let date = require('date-and-time');
|
|
32
39
|
```
|
|
40
|
+
babelify:
|
|
41
|
+
```javascript
|
|
42
|
+
import date from './date-and-time';
|
|
43
|
+
```
|
|
33
44
|
AMD:
|
|
34
45
|
```javascript
|
|
35
46
|
require(['date-and-time'], function (date) {
|
|
@@ -40,13 +51,6 @@ the browser:
|
|
|
40
51
|
window.date; // global object
|
|
41
52
|
```
|
|
42
53
|
|
|
43
|
-
## Breaking changes in 0.4.0
|
|
44
|
-
- `parse`
|
|
45
|
-
- It comes to return a `NaN` object in case of parse error.
|
|
46
|
-
- If year is not supplied in a date string, the default year of the returning date object is `1970`.
|
|
47
|
-
- `locale`
|
|
48
|
-
- Slightly changed the internal structure.
|
|
49
|
-
|
|
50
54
|
## API
|
|
51
55
|
|
|
52
56
|
### format(dateObj, formatString[, utc])
|
|
@@ -108,6 +112,7 @@ date.parse('02-01-2015', 'DD-MM-YYYY'); // => date object
|
|
|
108
112
|
date.parse('11:14:05 p.m.', 'hh:mm:ss A'); // => (Jan 1 1970 23:14:05 GMT-0800)
|
|
109
113
|
date.parse('11:14:05 p.m.', 'hh:mm:ss A', true); // => (Jan 1 1970 15:14:05 GMT-0800)
|
|
110
114
|
date.parse('Jam 1 2017', 'MMM D YYYY'); // => NaN
|
|
115
|
+
date.parse('Feb 29 2016', 'MMM D YYYY'); // => date object
|
|
111
116
|
date.parse('Feb 29 2017', 'MMM D YYYY'); // => NaN
|
|
112
117
|
```
|
|
113
118
|
|
|
@@ -289,6 +294,14 @@ let date = require('date-and-time');
|
|
|
289
294
|
date.locale('fr'); // French
|
|
290
295
|
date.format(new Date(), 'dddd D MMMM'); // => 'lundi 11 janvier'
|
|
291
296
|
```
|
|
297
|
+
babelify:
|
|
298
|
+
```javascript
|
|
299
|
+
import date from './date-and-time';
|
|
300
|
+
import './locale/it';
|
|
301
|
+
|
|
302
|
+
date.locale('it'); // Italian
|
|
303
|
+
date.format(new Date(), 'dddd D MMMM'); // => 'Lunedì 11 gennaio'
|
|
304
|
+
```
|
|
292
305
|
AMD:
|
|
293
306
|
```javascript
|
|
294
307
|
require(['date-and-time', 'locale/de'], function (date) {
|
package/date-and-time.js
CHANGED
|
@@ -71,9 +71,6 @@
|
|
|
71
71
|
pre: function (str) { return str; }
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
-
},
|
|
75
|
-
isCommonJS = function () {
|
|
76
|
-
return typeof module === 'object' && typeof module.exports === 'object';
|
|
77
74
|
};
|
|
78
75
|
|
|
79
76
|
/**
|
|
@@ -106,6 +103,7 @@
|
|
|
106
103
|
offset = 0, keys, i, token, length, p, str, result, dateObj,
|
|
107
104
|
re = /(MMMM?|A)|(YYYY)|(SSS)|(MM|DD|HH|hh|mm|ss)|(YY|M|D|H|h|m|s|SS)|(S)|(.)/g,
|
|
108
105
|
exp = { 2: /^\d{1,4}/, 3: /^\d{1,3}/, 4: /^\d\d/, 5: /^\d\d?/, 6: /^\d/ },
|
|
106
|
+
last = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
|
|
109
107
|
dt = { Y: 1970, M: 1, D: 1, H: 0, m: 0, s: 0, S: 0 };
|
|
110
108
|
|
|
111
109
|
while ((keys = re.exec(formatString))) {
|
|
@@ -125,6 +123,8 @@
|
|
|
125
123
|
result = (str.match(exp[i]) || [''])[0];
|
|
126
124
|
dt[p] = (p === 'S' ? (result + '000').slice(0, -token.length) : result) | 0;
|
|
127
125
|
length = result.length;
|
|
126
|
+
} else if (p !== ' ' && p !== str[0]) {
|
|
127
|
+
return NaN;
|
|
128
128
|
}
|
|
129
129
|
if (!length) {
|
|
130
130
|
return NaN;
|
|
@@ -138,9 +138,8 @@
|
|
|
138
138
|
dt.H = dt.H || locale.parser.h(dt.h || 0, dt.A || 0);
|
|
139
139
|
|
|
140
140
|
dateObj = new Date(dt.Y, dt.M - 1, dt.D, dt.H, dt.m, dt.s, dt.S);
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|| dt.S !== dateObj.getMilliseconds()) {
|
|
141
|
+
last[1] += date.isLeapYear(dateObj) | 0;
|
|
142
|
+
if (dt.M < 1 || dt.M > 12 || dt.D < 1 || dt.D > last[dt.M - 1] || dt.H > 23 || dt.m > 59 || dt.s > 59) {
|
|
144
143
|
return NaN;
|
|
145
144
|
}
|
|
146
145
|
return utc ? date.addMinutes(dateObj, -dateObj.getTimezoneOffset()) : dateObj;
|
|
@@ -287,7 +286,7 @@
|
|
|
287
286
|
*/
|
|
288
287
|
date.locale = function (code) {
|
|
289
288
|
if (code) {
|
|
290
|
-
if (code
|
|
289
|
+
if (!locales[code] && typeof require === 'function' && global) {
|
|
291
290
|
require('./locale/' + code);
|
|
292
291
|
}
|
|
293
292
|
lang = code;
|
|
@@ -297,7 +296,7 @@
|
|
|
297
296
|
|
|
298
297
|
/**
|
|
299
298
|
* getting a definition of locale
|
|
300
|
-
* @param {String} code - language code
|
|
299
|
+
* @param {String} [code] - language code
|
|
301
300
|
* @returns {Object} definition of locale
|
|
302
301
|
*/
|
|
303
302
|
date.getLocales = function (code) {
|
|
@@ -335,7 +334,7 @@
|
|
|
335
334
|
locales[code] = locale;
|
|
336
335
|
};
|
|
337
336
|
|
|
338
|
-
if (
|
|
337
|
+
if (typeof module === 'object' && typeof module.exports === 'object') {
|
|
339
338
|
module.exports = date;
|
|
340
339
|
} else if (typeof define === 'function' && define.amd) {
|
|
341
340
|
define([], function () {
|
package/date-and-time.min.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/*
|
|
2
2
|
date-and-time.js (c) KNOWLEDGECODE | MIT
|
|
3
3
|
*/
|
|
4
|
-
(function(
|
|
4
|
+
(function(p){var b={},n="en",k={en:{MMMM:"January February March April May June July August September October November December".split(" "),MMM:"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),dddd:"Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),ddd:"Sun Mon Tue Wed Thu Fri Sat".split(" "),dd:"Su Mo Tu We Th Fr Sa".split(" "),A:["a.m.","p.m."],formatter:{YYYY:function(a){return("000"+a.getFullYear()).slice(-4)},YY:function(a){return("0"+a.getFullYear()).slice(-2)},
|
|
5
5
|
Y:function(a){return""+a.getFullYear()},MMMM:function(a){return this.MMMM[a.getMonth()]},MMM:function(a){return this.MMM[a.getMonth()]},MM:function(a){return("0"+(a.getMonth()+1)).slice(-2)},M:function(a){return""+(a.getMonth()+1)},DD:function(a){return("0"+a.getDate()).slice(-2)},D:function(a){return""+a.getDate()},HH:function(a){return("0"+a.getHours()).slice(-2)},H:function(a){return""+a.getHours()},A:function(a){return this.A[11<a.getHours()|0]},hh:function(a){return("0"+(a.getHours()%12||12)).slice(-2)},
|
|
6
6
|
h:function(a){return""+(a.getHours()%12||12)},mm:function(a){return("0"+a.getMinutes()).slice(-2)},m:function(a){return""+a.getMinutes()},ss:function(a){return("0"+a.getSeconds()).slice(-2)},s:function(a){return""+a.getSeconds()},SSS:function(a){return("00"+a.getMilliseconds()).slice(-3)},SS:function(a){return("0"+(a.getMilliseconds()/10|0)).slice(-2)},S:function(a){return""+(a.getMilliseconds()/100|0)},dddd:function(a){return this.dddd[a.getDay()]},ddd:function(a){return this.ddd[a.getDay()]},dd:function(a){return this.dd[a.getDay()]},
|
|
7
|
-
Z:function(a){a=a.utc?0:a.getTimezoneOffset()/.6;return(0<a?"-":"+")+("000"+Math.abs(a-a%100*.4)).slice(-4)},post:function(a){return a}},parser:{find:function(a,c){for(var b=-1,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
c){return b.addMilliseconds(a,
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
Z:function(a){a=a.utc?0:a.getTimezoneOffset()/.6;return(0<a?"-":"+")+("000"+Math.abs(a-a%100*.4)).slice(-4)},post:function(a){return a}},parser:{find:function(a,c){for(var b=-1,e=0,g=0,h=a.length,f;g<h;g++)f=a[g],!c.indexOf(f)&&f.length>e&&(b=g,e=f.length);return{index:b,length:e}},MMMM:function(a){return this.parser.find(this.MMMM,a)},MMM:function(a){return this.parser.find(this.MMM,a)},A:function(a){return this.parser.find(this.A,a)},h:function(a,c){return(12===a?0:a)+12*c},pre:function(a){return a}}}};
|
|
8
|
+
b.format=function(a,c,q){var e=b.addMinutes(a,q?a.getTimezoneOffset():0),g=k[n],h=g.formatter;e.utc=q;return c.replace(/(\[[^\[\]]*]|\[.*\][^\[]*\]|YYYY|YY|MMM?M?|DD|HH|hh|mm|ss|SSS?|ddd?d?|.)/g,function(a){var b=h[a];return b?h.post(b.call(g,e,c)):a.replace(/\[(.*)]/,"$1")})};b.parse=function(a,c,q){var e=k[n],g=e.parser.pre(a),h=0,f,m,v=/(MMMM?|A)|(YYYY)|(SSS)|(MM|DD|HH|hh|mm|ss)|(YY|M|D|H|h|m|s|SS)|(S)|(.)/g,p={2:/^\d{1,4}/,3:/^\d{1,3}/,4:/^\d\d/,5:/^\d\d?/,6:/^\d/};a=[31,28,31,30,31,30,31,31,
|
|
9
|
+
30,31,30,31];for(var d={Y:1970,M:1,D:1,H:0,m:0,s:0,S:0};f=v.exec(c);){var r=0;var t=1;for(m="";!m;)m=f[++r];f=m.charAt(0);var u=g.slice(h);if(2>r){var l=e.parser[m].call(e,u,c);d[f]=l.index;"M"===f&&d[f]++;t=l.length}else if(7>r)l=(u.match(p[r])||[""])[0],d[f]=("S"===f?(l+"000").slice(0,-m.length):l)|0,t=l.length;else if(" "!==f&&f!==u[0])return NaN;if(!t)return NaN;h+=t}if(h!==g.length||!l)return NaN;d.Y+=70>d.Y?2E3:100>d.Y?1900:0;d.H=d.H||e.parser.h(d.h||0,d.A||0);c=new Date(d.Y,d.M-1,d.D,d.H,d.m,
|
|
10
|
+
d.s,d.S);a[1]+=b.isLeapYear(c)|0;return 1>d.M||12<d.M||1>d.D||d.D>a[d.M-1]||23<d.H||59<d.m||59<d.s?NaN:q?b.addMinutes(c,-c.getTimezoneOffset()):c};b.isValid=function(a,c){return!!b.parse(a,c)};b.addYears=function(a,c){return b.addMonths(a,12*c)};b.addMonths=function(a,c){var b=new Date(a.getTime());b.setMonth(b.getMonth()+c);return b};b.addDays=function(a,c){var b=new Date(a.getTime());b.setDate(b.getDate()+c);return b};b.addHours=function(a,c){return b.addMilliseconds(a,36E5*c)};b.addMinutes=function(a,
|
|
11
|
+
c){return b.addMilliseconds(a,6E4*c)};b.addSeconds=function(a,c){return b.addMilliseconds(a,1E3*c)};b.addMilliseconds=function(a,b){return new Date(a.getTime()+b)};b.subtract=function(a,b){var c=a.getTime()-b.getTime();return{toMilliseconds:function(){return c},toSeconds:function(){return c/1E3|0},toMinutes:function(){return c/6E4|0},toHours:function(){return c/36E5|0},toDays:function(){return c/864E5|0}}};b.isLeapYear=function(a){a=a.getFullYear();return!(a%4)&&!!(a%100)||!(a%400)};b.isSameDay=function(a,
|
|
12
|
+
c){return b.format(a,"YYYYMMDD")===b.format(c,"YYYYMMDD")};b.locale=function(a){a&&(!k[a]&&"function"===typeof require&&p&&require("./locale/"+a),n=a);return n};b.getLocales=function(a){return k[a||n]};b.setLocales=function(a,b){var c=function(a,b){var c=function(){},e;c.prototype=b;c=new c;for(e in a)a.hasOwnProperty(e)&&(c[e]=a[e]);return c},e=k[a]||k.en,g=c(b,e);b.formatter&&(g.formatter=c(b.formatter,e.formatter));b.parser&&(g.parser=c(b.parser,e.parser));k[a]=g};"object"===typeof module&&"object"===
|
|
13
|
+
typeof module.exports?module.exports=b:"function"===typeof define&&define.amd?define([],function(){return b}):p.date=b})(this);
|
package/locale/es.js
CHANGED
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
|
|
9
9
|
var locale = function (date) {
|
|
10
10
|
date.setLocales('es', {
|
|
11
|
-
MMMM: ['
|
|
12
|
-
MMM: ['
|
|
13
|
-
dddd: ['
|
|
14
|
-
ddd: ['
|
|
15
|
-
dd: ['
|
|
11
|
+
MMMM: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
|
|
12
|
+
MMM: ['Ene.', 'Feb.', 'Mar.', 'Abr.', 'May.', 'Jun.', 'Jul.', 'Ago.', 'Sep.', 'Oct.', 'Nov.', 'Dic.'],
|
|
13
|
+
dddd: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'],
|
|
14
|
+
ddd: ['Dom.', 'Lun.', 'Mar.', 'Mié.', 'Jue.', 'Vie.', 'Sáb.'],
|
|
15
|
+
dd: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sá'],
|
|
16
16
|
A: ['de la mañana', 'de la tarde', 'de la noche'],
|
|
17
17
|
formatter: {
|
|
18
18
|
A: function (d) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "date-and-time",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.3",
|
|
4
4
|
"description": "A Minimalist DateTime utility for Node.js and the browser",
|
|
5
5
|
"main": "date-and-time.js",
|
|
6
6
|
"scripts": {
|
|
@@ -25,9 +25,12 @@
|
|
|
25
25
|
},
|
|
26
26
|
"homepage": "https://github.com/knowledgecode/date-and-time",
|
|
27
27
|
"devDependencies": {
|
|
28
|
+
"babel-preset-env": "^1.7.0",
|
|
29
|
+
"babelify": "^7.3.0",
|
|
30
|
+
"browserify": "^14.5.0",
|
|
28
31
|
"expect.js": "^0.3.1",
|
|
29
|
-
"mocha": "^
|
|
30
|
-
"mocha-phantomjs": "^
|
|
31
|
-
"phantomjs": "^2.1.
|
|
32
|
+
"mocha": "^5.2.0",
|
|
33
|
+
"mocha-phantomjs-core": "^2.1.2",
|
|
34
|
+
"phantomjs-prebuilt": "^2.1.16"
|
|
32
35
|
}
|
|
33
36
|
}
|