date-format 1.2.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.eslintrc +5 -17
- package/.travis.yml +2 -3
- package/README.md +22 -4
- package/lib/index.js +89 -27
- package/package.json +5 -6
- package/test/date_format-test.js +4 -3
- package/test/parse-test.js +139 -0
- package/.npmignore +0 -15
package/.eslintrc
CHANGED
@@ -1,24 +1,12 @@
|
|
1
1
|
{
|
2
|
+
"extends": [
|
3
|
+
"eslint:recommended"
|
4
|
+
],
|
2
5
|
"env": {
|
3
6
|
"node": true,
|
4
7
|
"mocha": true
|
5
8
|
},
|
6
9
|
"plugins": [
|
7
|
-
"mocha"
|
8
|
-
|
9
|
-
],
|
10
|
-
"rules": {
|
11
|
-
"comma-dangle": 0,
|
12
|
-
"indent": 2,
|
13
|
-
"func-names": 0,
|
14
|
-
"max-len": [1, 120, 2],
|
15
|
-
"no-use-before-define": 1,
|
16
|
-
"no-param-reassign": 0,
|
17
|
-
"strict": 0,
|
18
|
-
"import/no-extraneous-dependencies": ["error", {"devDependencies": ["test/*.js"]}],
|
19
|
-
"mocha/no-exclusive-tests": "error"
|
20
|
-
},
|
21
|
-
"parserOptions": {
|
22
|
-
"ecmaVersion": 5
|
23
|
-
}
|
10
|
+
"mocha"
|
11
|
+
]
|
24
12
|
}
|
package/.travis.yml
CHANGED
package/README.md
CHANGED
@@ -10,7 +10,10 @@ npm install date-format
|
|
10
10
|
usage
|
11
11
|
=====
|
12
12
|
|
13
|
-
|
13
|
+
Formatting dates as strings
|
14
|
+
----
|
15
|
+
|
16
|
+
```javascript
|
14
17
|
var format = require('date-format');
|
15
18
|
format.asString(); //defaults to ISO8601 format and current date.
|
16
19
|
format.asString(new Date()); //defaults to ISO8601 format
|
@@ -19,7 +22,7 @@ format.asString('hh:mm:ss.SSS', new Date()); //just the time
|
|
19
22
|
|
20
23
|
or
|
21
24
|
|
22
|
-
```
|
25
|
+
```javascript
|
23
26
|
var format = require('date-format');
|
24
27
|
format(); //defaults to ISO8601 format and current date.
|
25
28
|
format(new Date());
|
@@ -35,6 +38,21 @@ Format string can be anything, but the following letters will be replaced (and l
|
|
35
38
|
* mm - `date.getMinutes()`
|
36
39
|
* ss - `date.getSeconds()`
|
37
40
|
* SSS - `date.getMilliseconds()`
|
38
|
-
* O - timezone offset in +hm format
|
41
|
+
* O - timezone offset in +hm format (note that time will be in UTC if displaying offset)
|
42
|
+
|
43
|
+
Built-in formats:
|
44
|
+
* `format.ISO8601_FORMAT` - `2017-03-14T14:10:20.391` (local time used)
|
45
|
+
* `format.ISO8601_WITH_TZ_OFFSET_FORMAT` - `2017-03-14T03:10:20.391+1100` (UTC + TZ used)
|
46
|
+
* `format.DATETIME_FORMAT` - `14 03 2017 14:10:20.391` (local time used)
|
47
|
+
* `format.ABSOLUTETIME_FORMAT` - `14:10:20.391` (local time used)
|
48
|
+
|
49
|
+
Parsing strings as dates
|
50
|
+
----
|
51
|
+
The date format library has limited ability to parse strings into dates. It can convert strings created using date format patterns (as above), but if you're looking for anything more sophisticated than that you should probably look for a better library ([momentjs](https://momentjs.com) does pretty much everything).
|
39
52
|
|
40
|
-
|
53
|
+
```javascript
|
54
|
+
var format = require('date-format');
|
55
|
+
// pass in the format of the string as first argument
|
56
|
+
format.parse(format.ISO8601_FORMAT, '2017-03-14T14:10:20.391');
|
57
|
+
// returns Date
|
58
|
+
```
|
package/lib/index.js
CHANGED
@@ -18,7 +18,6 @@ function addZero(vNumber) {
|
|
18
18
|
* @private
|
19
19
|
*/
|
20
20
|
function offset(timezoneOffset) {
|
21
|
-
// Difference to Greenwich time (GMT) in hours
|
22
21
|
var os = Math.abs(timezoneOffset);
|
23
22
|
var h = String(Math.floor(os / 60));
|
24
23
|
var m = String(os % 60);
|
@@ -31,46 +30,109 @@ function offset(timezoneOffset) {
|
|
31
30
|
return timezoneOffset < 0 ? '+' + h + m : '-' + h + m;
|
32
31
|
}
|
33
32
|
|
34
|
-
function
|
33
|
+
function datePart(date, displayUTC, part) {
|
34
|
+
return displayUTC ? date['getUTC' + part]() : date['get' + part]();
|
35
|
+
}
|
36
|
+
|
37
|
+
function asString(format, date) {
|
35
38
|
if (typeof format !== 'string') {
|
36
|
-
timezoneOffset = date;
|
37
39
|
date = format;
|
38
40
|
format = module.exports.ISO8601_FORMAT;
|
39
41
|
}
|
40
42
|
if (!date) {
|
41
|
-
date =
|
42
|
-
}
|
43
|
-
// make the date independent of the system timezone by working with UTC
|
44
|
-
if (timezoneOffset === undefined) {
|
45
|
-
timezoneOffset = date.getTimezoneOffset();
|
43
|
+
date = module.exports.now();
|
46
44
|
}
|
47
45
|
|
48
|
-
|
49
|
-
|
50
|
-
var
|
51
|
-
var
|
52
|
-
var
|
46
|
+
var displayUTC = format.indexOf('O') > -1;
|
47
|
+
|
48
|
+
var vDay = addZero(datePart(date, displayUTC, 'Date'));
|
49
|
+
var vMonth = addZero(datePart(date, displayUTC, 'Month') + 1);
|
50
|
+
var vYearLong = addZero(datePart(date, displayUTC, 'FullYear'));
|
51
|
+
var vYearShort = addZero(vYearLong.substring(2, 4));
|
53
52
|
var vYear = (format.indexOf('yyyy') > -1 ? vYearLong : vYearShort);
|
54
|
-
var vHour = addZero(date
|
55
|
-
var vMinute = addZero(date
|
56
|
-
var vSecond = addZero(date
|
57
|
-
var vMillisecond = padWithZeros(date
|
58
|
-
var vTimeZone = offset(
|
59
|
-
date.setUTCMinutes(date.getUTCMinutes() + timezoneOffset);
|
53
|
+
var vHour = addZero(datePart(date, displayUTC, 'Hours'));
|
54
|
+
var vMinute = addZero(datePart(date, displayUTC, 'Minutes'));
|
55
|
+
var vSecond = addZero(datePart(date, displayUTC, 'Seconds'));
|
56
|
+
var vMillisecond = padWithZeros(datePart(date, displayUTC, 'Milliseconds'), 3);
|
57
|
+
var vTimeZone = offset(date.getTimezoneOffset());
|
60
58
|
var formatted = format
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
59
|
+
.replace(/dd/g, vDay)
|
60
|
+
.replace(/MM/g, vMonth)
|
61
|
+
.replace(/y{1,4}/g, vYear)
|
62
|
+
.replace(/hh/g, vHour)
|
63
|
+
.replace(/mm/g, vMinute)
|
64
|
+
.replace(/ss/g, vSecond)
|
65
|
+
.replace(/SSS/g, vMillisecond)
|
66
|
+
.replace(/O/g, vTimeZone);
|
69
67
|
return formatted;
|
70
68
|
}
|
71
69
|
|
70
|
+
function extractDateParts(pattern, str) {
|
71
|
+
var matchers = [
|
72
|
+
{ pattern: /y{1,4}/, regexp: "\\d{1,4}", fn: function(date, value) { date.setFullYear(value); } },
|
73
|
+
{ pattern: /MM/, regexp: "\\d{1,2}", fn: function(date, value) { date.setMonth(value -1); } },
|
74
|
+
{ pattern: /dd/, regexp: "\\d{1,2}", fn: function(date, value) { date.setDate(value); } },
|
75
|
+
{ pattern: /hh/, regexp: "\\d{1,2}", fn: function(date, value) { date.setHours(value); } },
|
76
|
+
{ pattern: /mm/, regexp: "\\d\\d", fn: function(date, value) { date.setMinutes(value); } },
|
77
|
+
{ pattern: /ss/, regexp: "\\d\\d", fn: function(date, value) { date.setSeconds(value); } },
|
78
|
+
{ pattern: /SSS/, regexp: "\\d\\d\\d", fn: function(date, value) { date.setMilliseconds(value); } },
|
79
|
+
{ pattern: /O/, regexp: "[+-]\\d{3,4}|Z", fn: function(date, value) {
|
80
|
+
if (value === 'Z') {
|
81
|
+
value = 0;
|
82
|
+
}
|
83
|
+
var offset = Math.abs(value);
|
84
|
+
var minutes = (offset % 100) + (Math.floor(offset / 100) * 60);
|
85
|
+
date.setMinutes(date.getMinutes() + (value > 0 ? minutes : -minutes));
|
86
|
+
} }
|
87
|
+
];
|
88
|
+
|
89
|
+
var parsedPattern = matchers.reduce(function(p, m) {
|
90
|
+
if (m.pattern.test(p.regexp)) {
|
91
|
+
m.index = p.regexp.match(m.pattern).index;
|
92
|
+
p.regexp = p.regexp.replace(m.pattern, "(" + m.regexp + ")");
|
93
|
+
} else {
|
94
|
+
m.index = -1;
|
95
|
+
}
|
96
|
+
return p;
|
97
|
+
}, { regexp: pattern, index: [] });
|
98
|
+
|
99
|
+
var dateFns = matchers.filter(function(m) {
|
100
|
+
return m.index > -1;
|
101
|
+
});
|
102
|
+
dateFns.sort(function(a, b) { return a.index - b.index; });
|
103
|
+
|
104
|
+
var matcher = new RegExp(parsedPattern.regexp);
|
105
|
+
var matches = matcher.exec(str);
|
106
|
+
if (matches) {
|
107
|
+
var date = module.exports.now();
|
108
|
+
dateFns.forEach(function(f, i) {
|
109
|
+
f.fn(date, matches[i+1]);
|
110
|
+
});
|
111
|
+
return date;
|
112
|
+
}
|
113
|
+
|
114
|
+
throw new Error('String \'' + str + '\' could not be parsed as \'' + pattern + '\'');
|
115
|
+
}
|
116
|
+
|
117
|
+
function parse(pattern, str) {
|
118
|
+
if (!pattern) {
|
119
|
+
throw new Error('pattern must be supplied');
|
120
|
+
}
|
121
|
+
|
122
|
+
return extractDateParts(pattern, str);
|
123
|
+
}
|
124
|
+
|
125
|
+
/**
|
126
|
+
* Used for testing - replace this function with a fixed date.
|
127
|
+
*/
|
128
|
+
function now() {
|
129
|
+
return new Date();
|
130
|
+
}
|
131
|
+
|
72
132
|
module.exports = asString;
|
73
133
|
module.exports.asString = asString;
|
134
|
+
module.exports.parse = parse;
|
135
|
+
module.exports.now = now;
|
74
136
|
module.exports.ISO8601_FORMAT = 'yyyy-MM-ddThh:mm:ss.SSS';
|
75
137
|
module.exports.ISO8601_WITH_TZ_OFFSET_FORMAT = 'yyyy-MM-ddThh:mm:ss.SSSO';
|
76
138
|
module.exports.DATETIME_FORMAT = 'dd MM yyyy hh:mm:ss.SSS';
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "date-format",
|
3
|
-
"version": "
|
3
|
+
"version": "2.0.0",
|
4
4
|
"description": "Formatting Date objects as strings since 2013",
|
5
5
|
"main": "lib/index.js",
|
6
6
|
"repository": {
|
@@ -25,10 +25,9 @@
|
|
25
25
|
"readmeFilename": "README.md",
|
26
26
|
"gitHead": "bf59015ab6c9e86454b179374f29debbdb403522",
|
27
27
|
"devDependencies": {
|
28
|
-
"eslint": "^
|
29
|
-
"eslint-plugin-
|
30
|
-
"
|
31
|
-
"
|
32
|
-
"should": "^11.1.2"
|
28
|
+
"eslint": "^5.5.0",
|
29
|
+
"eslint-plugin-mocha": "^5.2.0",
|
30
|
+
"mocha": "^5.2.0",
|
31
|
+
"should": "^13.2.3"
|
33
32
|
}
|
34
33
|
}
|
package/test/date_format-test.js
CHANGED
@@ -34,8 +34,9 @@ describe('date_format', function() {
|
|
34
34
|
return -660;
|
35
35
|
};
|
36
36
|
|
37
|
+
// when tz offset is in the pattern, the date should be in UTC
|
37
38
|
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
|
38
|
-
|
39
|
+
.should.eql('2010-01-11T03:31:30.005+1100');
|
39
40
|
|
40
41
|
tzDate = createFixedDate();
|
41
42
|
tzDate.setMinutes((tzDate.getMinutes() - tzDate.getTimezoneOffset()) + 120);
|
@@ -44,7 +45,7 @@ describe('date_format', function() {
|
|
44
45
|
};
|
45
46
|
|
46
47
|
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
|
47
|
-
|
48
|
+
.should.eql('2010-01-11T16:31:30.005-0200');
|
48
49
|
});
|
49
50
|
|
50
51
|
it('should provide a just-the-time format', function() {
|
@@ -58,6 +59,6 @@ describe('date_format', function() {
|
|
58
59
|
return 120;
|
59
60
|
};
|
60
61
|
|
61
|
-
dateFormat.asString('O.SSS.ss.mm.hh.dd.MM.yy', customDate).should.eql('-0200.005.30.31.
|
62
|
+
dateFormat.asString('O.SSS.ss.mm.hh.dd.MM.yy', customDate).should.eql('-0200.005.30.31.16.11.01.10');
|
62
63
|
});
|
63
64
|
});
|
@@ -0,0 +1,139 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
require('should');
|
4
|
+
var dateFormat = require('../lib');
|
5
|
+
|
6
|
+
describe('dateFormat.parse', function() {
|
7
|
+
it('should require a pattern', function() {
|
8
|
+
(function() { dateFormat.parse() }).should.throw(/pattern must be supplied/);
|
9
|
+
(function() { dateFormat.parse(null) }).should.throw(/pattern must be supplied/);
|
10
|
+
(function() { dateFormat.parse('') }).should.throw(/pattern must be supplied/);
|
11
|
+
});
|
12
|
+
|
13
|
+
describe('with a pattern that has no replacements', function() {
|
14
|
+
it('should return a new date when the string matches', function() {
|
15
|
+
dateFormat.parse('cheese', 'cheese').should.be.a.Date()
|
16
|
+
});
|
17
|
+
|
18
|
+
it('should throw if the string does not match', function() {
|
19
|
+
(function() {
|
20
|
+
dateFormat.parse('cheese', 'biscuits');
|
21
|
+
}).should.throw(/String 'biscuits' could not be parsed as 'cheese'/);
|
22
|
+
});
|
23
|
+
});
|
24
|
+
|
25
|
+
describe('with a full pattern', function() {
|
26
|
+
var pattern = 'yyyy-MM-dd hh:mm:ss.SSSO';
|
27
|
+
|
28
|
+
it('should return the correct date if the string matches', function() {
|
29
|
+
var testDate = new Date();
|
30
|
+
testDate.setFullYear(2018);
|
31
|
+
testDate.setMonth(8);
|
32
|
+
testDate.setDate(13);
|
33
|
+
testDate.setHours(18);
|
34
|
+
testDate.setMinutes(10);
|
35
|
+
testDate.setSeconds(12);
|
36
|
+
testDate.setMilliseconds(392);
|
37
|
+
testDate.getTimezoneOffset = function() { return 600; };
|
38
|
+
|
39
|
+
dateFormat.parse(pattern, '2018-09-13 08:10:12.392+1000').getTime().should.eql(testDate.getTime());
|
40
|
+
});
|
41
|
+
|
42
|
+
it('should throw if the string does not match', function() {
|
43
|
+
(function() {
|
44
|
+
dateFormat.parse(pattern, 'biscuits')
|
45
|
+
}).should.throw(/String 'biscuits' could not be parsed as 'yyyy-MM-dd hh:mm:ss.SSSO'/);
|
46
|
+
});
|
47
|
+
});
|
48
|
+
|
49
|
+
describe('with a partial pattern', function() {
|
50
|
+
var testDate = new Date();
|
51
|
+
dateFormat.now = function() { return testDate; };
|
52
|
+
|
53
|
+
function verifyDate(actual, expected) {
|
54
|
+
actual.getFullYear().should.eql(expected.year || testDate.getFullYear());
|
55
|
+
actual.getMonth().should.eql(expected.month || testDate.getMonth());
|
56
|
+
actual.getDate().should.eql(expected.day || testDate.getDate());
|
57
|
+
actual.getHours().should.eql(expected.hours || testDate.getHours());
|
58
|
+
actual.getMinutes().should.eql(expected.minutes || testDate.getMinutes());
|
59
|
+
actual.getSeconds().should.eql(expected.seconds || testDate.getSeconds());
|
60
|
+
actual.getMilliseconds().should.eql(expected.milliseconds || testDate.getMilliseconds());
|
61
|
+
}
|
62
|
+
|
63
|
+
it('should return a date with missing values defaulting to current time', function() {
|
64
|
+
var date = dateFormat.parse('yyyy-MM', '2015-09');
|
65
|
+
verifyDate(date, { year: 2015, month: 8 });
|
66
|
+
});
|
67
|
+
|
68
|
+
it('should handle variations on the same pattern', function() {
|
69
|
+
var date = dateFormat.parse('MM-yyyy', '09-2015');
|
70
|
+
verifyDate(date, { year: 2015, month: 8 });
|
71
|
+
|
72
|
+
date = dateFormat.parse('yyyy MM', '2015 09');
|
73
|
+
verifyDate(date, { year: 2015, month: 8 });
|
74
|
+
|
75
|
+
date = dateFormat.parse('MM, yyyy.', '09, 2015.');
|
76
|
+
verifyDate(date, { year: 2015, month: 8 });
|
77
|
+
});
|
78
|
+
|
79
|
+
it('should match all the date parts', function() {
|
80
|
+
var date = dateFormat.parse('dd', '21');
|
81
|
+
verifyDate(date, { day: 21 });
|
82
|
+
|
83
|
+
date = dateFormat.parse('hh', '12');
|
84
|
+
verifyDate(date, { hours: 12 });
|
85
|
+
|
86
|
+
date = dateFormat.parse('mm', '34');
|
87
|
+
verifyDate(date, { minutes: 34 });
|
88
|
+
|
89
|
+
date = dateFormat.parse('ss', '59');
|
90
|
+
verifyDate(date, { seconds: 59 });
|
91
|
+
|
92
|
+
date = dateFormat.parse('ss.SSS', '23.452');
|
93
|
+
verifyDate(date, { seconds: 23, milliseconds: 452 });
|
94
|
+
|
95
|
+
date = dateFormat.parse('hh:mm O', '05:23 +1000');
|
96
|
+
verifyDate(date, { hours: 15, minutes: 23 });
|
97
|
+
|
98
|
+
date = dateFormat.parse('hh:mm O', '05:23 -200');
|
99
|
+
verifyDate(date, { hours: 3, minutes: 23 });
|
100
|
+
|
101
|
+
date = dateFormat.parse('hh:mm O', '05:23 +0930');
|
102
|
+
verifyDate(date, { hours: 14, minutes: 53 });
|
103
|
+
});
|
104
|
+
});
|
105
|
+
|
106
|
+
describe('with a date formatted by this library', function() {
|
107
|
+
var testDate = new Date();
|
108
|
+
testDate.setUTCFullYear(2018);
|
109
|
+
testDate.setUTCMonth(8);
|
110
|
+
testDate.setUTCDate(13);
|
111
|
+
testDate.setUTCHours(18);
|
112
|
+
testDate.setUTCMinutes(10);
|
113
|
+
testDate.setUTCSeconds(12);
|
114
|
+
testDate.setUTCMilliseconds(392);
|
115
|
+
|
116
|
+
it('should format and then parse back to the same date', function() {
|
117
|
+
dateFormat.parse(
|
118
|
+
dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT,
|
119
|
+
dateFormat(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, testDate)
|
120
|
+
).should.eql(testDate);
|
121
|
+
|
122
|
+
dateFormat.parse(
|
123
|
+
dateFormat.ISO8601_FORMAT,
|
124
|
+
dateFormat(dateFormat.ISO8601_FORMAT, testDate)
|
125
|
+
).should.eql(testDate);
|
126
|
+
|
127
|
+
dateFormat.parse(
|
128
|
+
dateFormat.DATETIME_FORMAT,
|
129
|
+
dateFormat(dateFormat.DATETIME_FORMAT, testDate)
|
130
|
+
).should.eql(testDate);
|
131
|
+
|
132
|
+
dateFormat.parse(
|
133
|
+
dateFormat.ABSOLUTETIME_FORMAT,
|
134
|
+
dateFormat(dateFormat.ABSOLUTETIME_FORMAT, testDate)
|
135
|
+
).should.eql(testDate);
|
136
|
+
});
|
137
|
+
|
138
|
+
});
|
139
|
+
});
|