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 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
@@ -0,0 +1,7 @@
1
+ language: node_js
2
+ sudo: false
3
+ node_js:
4
+ - "7"
5
+ - "6"
6
+ - "5"
7
+ - "4"
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
- npm install date-format
6
+ ```sh
7
+ npm install date-format
8
+ ```
7
9
 
8
10
  usage
9
11
  =====
10
12
 
11
- var format = require('date-format');
12
- format.asString(new Date()); //defaults to ISO8601 format
13
- format.asString('hh:mm:ss.SSS', new Date()); //just the time
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
- var format = require('date-format');
18
- format(new Date());
19
- format('hh:mm:ss.SSS', new Date());
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
- "use strict";
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
- var numAsString = vNumber + "";
13
- while (numAsString.length < width) {
14
- numAsString = "0" + numAsString;
15
- }
16
- return numAsString;
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
- return padWithZeros(vNumber, 2);
12
+ return padWithZeros(vNumber, 2);
21
13
  }
22
14
 
23
15
  /**
24
- * Formats the TimeOffest
16
+ * Formats the TimeOffset
25
17
  * Thanks to http://www.svendtofte.com/code/date_format/
26
18
  * @private
27
19
  */
28
- function offset(date) {
20
+ function offset(timezoneOffset) {
29
21
  // Difference to Greenwich time (GMT) in hours
30
- var os = Math.abs(date.getTimezoneOffset());
31
- var h = String(Math.floor(os/60));
32
- var m = String(os%60);
33
- if (h.length == 1) {
34
- h = "0" + h;
35
- }
36
- if (m.length == 1) {
37
- m = "0" + m;
38
- }
39
- return date.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m;
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(/*format,*/ date) {
43
- var format = asString.ISO8601_FORMAT;
44
- if (typeof(date) === "string") {
45
- format = arguments[0];
46
- date = arguments[1];
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
- var vDay = addZero(date.getDate());
50
- var vMonth = addZero(date.getMonth()+1);
51
- var vYearLong = addZero(date.getFullYear());
52
- var vYearShort = addZero(date.getFullYear().toString().substring(2,4));
53
- var vYear = (format.indexOf("yyyy") > -1 ? vYearLong : vYearShort);
54
- var vHour = addZero(date.getHours());
55
- var vMinute = addZero(date.getMinutes());
56
- var vSecond = addZero(date.getSeconds());
57
- var vMillisecond = padWithZeros(date.getMilliseconds(), 3);
58
- var vTimeZone = offset(date);
59
- var formatted = format
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
- return formatted;
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": "0.0.1",
4
- "description": "formatting Date objects as strings since 2013",
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.jones@sensis.com.au>",
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
- "should": "~1.2.2",
24
- "mocha": "~1.12.0"
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
  }
@@ -1,42 +1,63 @@
1
- "use strict";
2
- var should = require('should')
3
- , dateFormat = require('../lib');
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
- var date = new Date(2010, 0, 11, 14, 31, 30, 5);
7
-
8
- it('should format a date as string using a pattern', function() {
9
- dateFormat.asString(dateFormat.DATETIME_FORMAT, date).should.eql("11 01 2010 14:31:30.005");
10
- });
11
-
12
- it('should default to the ISO8601 format', function() {
13
- dateFormat.asString(date).should.eql('2010-01-11 14:31:30.005');
14
- });
15
-
16
- it('should provide a ISO8601 with timezone offset format', function() {
17
- date.getTimezoneOffset = function() { return -660; };
18
- dateFormat.asString(
19
- dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT,
20
- date
21
- ).should.eql(
22
- "2010-01-11T14:31:30+1100"
23
- );
24
-
25
- date.getTimezoneOffset = function() { return 120; };
26
- dateFormat.asString(
27
- dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT,
28
- date
29
- ).should.eql(
30
- "2010-01-11T14:31:30-0200"
31
- );
32
- });
33
-
34
- it('should provide a just-the-time format', function() {
35
- dateFormat.asString(dateFormat.ABSOLUTETIME_FORMAT, date).should.eql('14:31:30.005');
36
- });
37
-
38
- it('should provide a custom format', function() {
39
- date.getTimezoneOffset = function() { return 120; };
40
- dateFormat.asString("O.SSS.ss.mm.hh.dd.MM.yy", date).should.eql('-0200.005.30.31.14.11.01.10');
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
  });