date-format 0.0.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.eslintrc +24 -0
- package/.travis.yml +7 -0
- package/README.md +23 -16
- package/lib/index.js +54 -47
- package/package.json +16 -8
- package/test/date_format-test.js +60 -39
package/.eslintrc
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
{
|
2
|
+
"env": {
|
3
|
+
"node": true,
|
4
|
+
"mocha": true
|
5
|
+
},
|
6
|
+
"plugins": [
|
7
|
+
"mocha",
|
8
|
+
"import"
|
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
|
+
}
|
24
|
+
}
|
package/.travis.yml
ADDED
package/README.md
CHANGED
@@ -3,31 +3,38 @@ date-format
|
|
3
3
|
|
4
4
|
node.js formatting of Date objects as strings. Probably exactly the same as some other library out there.
|
5
5
|
|
6
|
-
|
6
|
+
```sh
|
7
|
+
npm install date-format
|
8
|
+
```
|
7
9
|
|
8
10
|
usage
|
9
11
|
=====
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
13
|
+
```js
|
14
|
+
var format = require('date-format');
|
15
|
+
format.asString(); //defaults to ISO8601 format and current date.
|
16
|
+
format.asString(new Date()); //defaults to ISO8601 format
|
17
|
+
format.asString('hh:mm:ss.SSS', new Date()); //just the time
|
18
|
+
```
|
14
19
|
|
15
20
|
or
|
16
21
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
22
|
+
```js
|
23
|
+
var format = require('date-format');
|
24
|
+
format(); //defaults to ISO8601 format and current date.
|
25
|
+
format(new Date());
|
26
|
+
format('hh:mm:ss.SSS', new Date());
|
27
|
+
```
|
21
28
|
|
22
29
|
Format string can be anything, but the following letters will be replaced (and leading zeroes added if necessary):
|
23
|
-
* dd - date.getDate()
|
24
|
-
* MM - date.getMonth() + 1
|
25
|
-
* yy - date.getFullYear().toString().substring(2, 4)
|
26
|
-
* yyyy - date.getFullYear()
|
27
|
-
* hh - date.getHours()
|
28
|
-
* mm - date.getMinutes()
|
29
|
-
* ss - date.getSeconds()
|
30
|
-
* SSS - date.getMilliseconds()
|
30
|
+
* dd - `date.getDate()`
|
31
|
+
* MM - `date.getMonth() + 1`
|
32
|
+
* yy - `date.getFullYear().toString().substring(2, 4)`
|
33
|
+
* yyyy - `date.getFullYear()`
|
34
|
+
* hh - `date.getHours()`
|
35
|
+
* mm - `date.getMinutes()`
|
36
|
+
* ss - `date.getSeconds()`
|
37
|
+
* SSS - `date.getMilliseconds()`
|
31
38
|
* O - timezone offset in +hm format
|
32
39
|
|
33
40
|
That's it.
|
package/lib/index.js
CHANGED
@@ -1,62 +1,63 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module.exports = asString
|
4
|
-
asString.asString = asString
|
5
|
-
|
6
|
-
asString.ISO8601_FORMAT = "yyyy-MM-dd hh:mm:ss.SSS";
|
7
|
-
asString.ISO8601_WITH_TZ_OFFSET_FORMAT = "yyyy-MM-ddThh:mm:ssO";
|
8
|
-
asString.DATETIME_FORMAT = "dd MM yyyy hh:mm:ss.SSS";
|
9
|
-
asString.ABSOLUTETIME_FORMAT = "hh:mm:ss.SSS";
|
1
|
+
'use strict';
|
10
2
|
|
11
3
|
function padWithZeros(vNumber, width) {
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
4
|
+
var numAsString = vNumber.toString();
|
5
|
+
while (numAsString.length < width) {
|
6
|
+
numAsString = '0' + numAsString;
|
7
|
+
}
|
8
|
+
return numAsString;
|
17
9
|
}
|
18
|
-
|
10
|
+
|
19
11
|
function addZero(vNumber) {
|
20
|
-
|
12
|
+
return padWithZeros(vNumber, 2);
|
21
13
|
}
|
22
14
|
|
23
15
|
/**
|
24
|
-
* Formats the
|
16
|
+
* Formats the TimeOffset
|
25
17
|
* Thanks to http://www.svendtofte.com/code/date_format/
|
26
18
|
* @private
|
27
19
|
*/
|
28
|
-
function offset(
|
20
|
+
function offset(timezoneOffset) {
|
29
21
|
// Difference to Greenwich time (GMT) in hours
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
22
|
+
var os = Math.abs(timezoneOffset);
|
23
|
+
var h = String(Math.floor(os / 60));
|
24
|
+
var m = String(os % 60);
|
25
|
+
if (h.length === 1) {
|
26
|
+
h = '0' + h;
|
27
|
+
}
|
28
|
+
if (m.length === 1) {
|
29
|
+
m = '0' + m;
|
30
|
+
}
|
31
|
+
return timezoneOffset < 0 ? '+' + h + m : '-' + h + m;
|
40
32
|
}
|
41
33
|
|
42
|
-
function asString(
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
34
|
+
function asString(format, date, timezoneOffset) {
|
35
|
+
if (typeof format !== 'string') {
|
36
|
+
timezoneOffset = date;
|
37
|
+
date = format;
|
38
|
+
format = module.exports.ISO8601_FORMAT;
|
39
|
+
}
|
40
|
+
if (!date) {
|
41
|
+
date = new Date();
|
42
|
+
}
|
43
|
+
// make the date independent of the system timezone by working with UTC
|
44
|
+
if (timezoneOffset === undefined) {
|
45
|
+
timezoneOffset = date.getTimezoneOffset();
|
46
|
+
}
|
48
47
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
48
|
+
date.setUTCMinutes(date.getUTCMinutes() - timezoneOffset);
|
49
|
+
var vDay = addZero(date.getUTCDate());
|
50
|
+
var vMonth = addZero(date.getUTCMonth() + 1);
|
51
|
+
var vYearLong = addZero(date.getUTCFullYear());
|
52
|
+
var vYearShort = addZero(date.getUTCFullYear().toString().substring(2, 4));
|
53
|
+
var vYear = (format.indexOf('yyyy') > -1 ? vYearLong : vYearShort);
|
54
|
+
var vHour = addZero(date.getUTCHours());
|
55
|
+
var vMinute = addZero(date.getUTCMinutes());
|
56
|
+
var vSecond = addZero(date.getUTCSeconds());
|
57
|
+
var vMillisecond = padWithZeros(date.getUTCMilliseconds(), 3);
|
58
|
+
var vTimeZone = offset(timezoneOffset);
|
59
|
+
date.setUTCMinutes(date.getUTCMinutes() + timezoneOffset);
|
60
|
+
var formatted = format
|
60
61
|
.replace(/dd/g, vDay)
|
61
62
|
.replace(/MM/g, vMonth)
|
62
63
|
.replace(/y{1,4}/g, vYear)
|
@@ -65,6 +66,12 @@ function asString(/*format,*/ date) {
|
|
65
66
|
.replace(/ss/g, vSecond)
|
66
67
|
.replace(/SSS/g, vMillisecond)
|
67
68
|
.replace(/O/g, vTimeZone);
|
68
|
-
|
69
|
+
return formatted;
|
70
|
+
}
|
69
71
|
|
70
|
-
|
72
|
+
module.exports = asString;
|
73
|
+
module.exports.asString = asString;
|
74
|
+
module.exports.ISO8601_FORMAT = 'yyyy-MM-ddThh:mm:ss.SSS';
|
75
|
+
module.exports.ISO8601_WITH_TZ_OFFSET_FORMAT = 'yyyy-MM-ddThh:mm:ss.SSSO';
|
76
|
+
module.exports.DATETIME_FORMAT = 'dd MM yyyy hh:mm:ss.SSS';
|
77
|
+
module.exports.ABSOLUTETIME_FORMAT = 'hh:mm:ss.SSS';
|
package/package.json
CHANGED
@@ -1,26 +1,34 @@
|
|
1
1
|
{
|
2
2
|
"name": "date-format",
|
3
|
-
"version": "
|
4
|
-
"description": "
|
3
|
+
"version": "1.2.0",
|
4
|
+
"description": "Formatting Date objects as strings since 2013",
|
5
5
|
"main": "lib/index.js",
|
6
|
-
"scripts": {
|
7
|
-
"test": "mocha"
|
8
|
-
},
|
9
6
|
"repository": {
|
10
7
|
"type": "git",
|
11
8
|
"url": "https://github.com/nomiddlename/date-format.git"
|
12
9
|
},
|
10
|
+
"engines": {
|
11
|
+
"node": ">=4.0"
|
12
|
+
},
|
13
|
+
"scripts": {
|
14
|
+
"lint": "eslint lib/* test/*",
|
15
|
+
"pretest": "eslint lib/* test/*",
|
16
|
+
"test": "mocha"
|
17
|
+
},
|
13
18
|
"keywords": [
|
14
19
|
"date",
|
15
20
|
"format",
|
16
21
|
"string"
|
17
22
|
],
|
18
|
-
"author": "Gareth Jones <gareth.
|
23
|
+
"author": "Gareth Jones <gareth.nomiddlename@gmail.com>",
|
19
24
|
"license": "MIT",
|
20
25
|
"readmeFilename": "README.md",
|
21
26
|
"gitHead": "bf59015ab6c9e86454b179374f29debbdb403522",
|
22
27
|
"devDependencies": {
|
23
|
-
"
|
24
|
-
"
|
28
|
+
"eslint": "^3.12.0",
|
29
|
+
"eslint-plugin-import": "^2.2.0",
|
30
|
+
"eslint-plugin-mocha": "^4.8.0",
|
31
|
+
"mocha": "^3.2.0",
|
32
|
+
"should": "^11.1.2"
|
25
33
|
}
|
26
34
|
}
|
package/test/date_format-test.js
CHANGED
@@ -1,42 +1,63 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
require('should');
|
4
|
+
|
5
|
+
var dateFormat = require('../lib');
|
6
|
+
|
7
|
+
function createFixedDate() {
|
8
|
+
return new Date(2010, 0, 11, 14, 31, 30, 5);
|
9
|
+
}
|
4
10
|
|
5
11
|
describe('date_format', function() {
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
12
|
+
var date = createFixedDate();
|
13
|
+
|
14
|
+
it('should default to now when a date is not provided', function() {
|
15
|
+
dateFormat.asString(dateFormat.DATETIME_FORMAT).should.not.be.empty();
|
16
|
+
});
|
17
|
+
|
18
|
+
it('should be usable directly without calling asString', function() {
|
19
|
+
dateFormat(dateFormat.DATETIME_FORMAT, date).should.eql('11 01 2010 14:31:30.005');
|
20
|
+
});
|
21
|
+
|
22
|
+
it('should format a date as string using a pattern', function() {
|
23
|
+
dateFormat.asString(dateFormat.DATETIME_FORMAT, date).should.eql('11 01 2010 14:31:30.005');
|
24
|
+
});
|
25
|
+
|
26
|
+
it('should default to the ISO8601 format', function() {
|
27
|
+
dateFormat.asString(date).should.eql('2010-01-11T14:31:30.005');
|
28
|
+
});
|
29
|
+
|
30
|
+
it('should provide a ISO8601 with timezone offset format', function() {
|
31
|
+
var tzDate = createFixedDate();
|
32
|
+
tzDate.setMinutes(tzDate.getMinutes() - tzDate.getTimezoneOffset() - 660);
|
33
|
+
tzDate.getTimezoneOffset = function () {
|
34
|
+
return -660;
|
35
|
+
};
|
36
|
+
|
37
|
+
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
|
38
|
+
.should.eql('2010-01-11T14:31:30.005+1100');
|
39
|
+
|
40
|
+
tzDate = createFixedDate();
|
41
|
+
tzDate.setMinutes((tzDate.getMinutes() - tzDate.getTimezoneOffset()) + 120);
|
42
|
+
tzDate.getTimezoneOffset = function () {
|
43
|
+
return 120;
|
44
|
+
};
|
45
|
+
|
46
|
+
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
|
47
|
+
.should.eql('2010-01-11T14:31:30.005-0200');
|
48
|
+
});
|
49
|
+
|
50
|
+
it('should provide a just-the-time format', function() {
|
51
|
+
dateFormat.asString(dateFormat.ABSOLUTETIME_FORMAT, date).should.eql('14:31:30.005');
|
52
|
+
});
|
53
|
+
|
54
|
+
it('should provide a custom format', function() {
|
55
|
+
var customDate = createFixedDate();
|
56
|
+
customDate.setMinutes((customDate.getMinutes() - customDate.getTimezoneOffset()) + 120);
|
57
|
+
customDate.getTimezoneOffset = function () {
|
58
|
+
return 120;
|
59
|
+
};
|
60
|
+
|
61
|
+
dateFormat.asString('O.SSS.ss.mm.hh.dd.MM.yy', customDate).should.eql('-0200.005.30.31.14.11.01.10');
|
62
|
+
});
|
42
63
|
});
|