date-format
Advanced tools
Comparing version 1.2.0 to 2.0.0
116
lib/index.js
@@ -21,3 +21,2 @@ 'use strict'; | ||
function offset(timezoneOffset) { | ||
// Difference to Greenwich time (GMT) in hours | ||
var os = Math.abs(timezoneOffset); | ||
@@ -35,5 +34,8 @@ var h = String(Math.floor(os / 60)); | ||
function asString(format, date, timezoneOffset) { | ||
function datePart(date, displayUTC, part) { | ||
return displayUTC ? date['getUTC' + part]() : date['get' + part](); | ||
} | ||
function asString(format, date) { | ||
if (typeof format !== 'string') { | ||
timezoneOffset = date; | ||
date = format; | ||
@@ -43,35 +45,95 @@ format = module.exports.ISO8601_FORMAT; | ||
if (!date) { | ||
date = new Date(); | ||
date = module.exports.now(); | ||
} | ||
// make the date independent of the system timezone by working with UTC | ||
if (timezoneOffset === undefined) { | ||
timezoneOffset = date.getTimezoneOffset(); | ||
} | ||
date.setUTCMinutes(date.getUTCMinutes() - timezoneOffset); | ||
var vDay = addZero(date.getUTCDate()); | ||
var vMonth = addZero(date.getUTCMonth() + 1); | ||
var vYearLong = addZero(date.getUTCFullYear()); | ||
var vYearShort = addZero(date.getUTCFullYear().toString().substring(2, 4)); | ||
var displayUTC = format.indexOf('O') > -1; | ||
var vDay = addZero(datePart(date, displayUTC, 'Date')); | ||
var vMonth = addZero(datePart(date, displayUTC, 'Month') + 1); | ||
var vYearLong = addZero(datePart(date, displayUTC, 'FullYear')); | ||
var vYearShort = addZero(vYearLong.substring(2, 4)); | ||
var vYear = (format.indexOf('yyyy') > -1 ? vYearLong : vYearShort); | ||
var vHour = addZero(date.getUTCHours()); | ||
var vMinute = addZero(date.getUTCMinutes()); | ||
var vSecond = addZero(date.getUTCSeconds()); | ||
var vMillisecond = padWithZeros(date.getUTCMilliseconds(), 3); | ||
var vTimeZone = offset(timezoneOffset); | ||
date.setUTCMinutes(date.getUTCMinutes() + timezoneOffset); | ||
var vHour = addZero(datePart(date, displayUTC, 'Hours')); | ||
var vMinute = addZero(datePart(date, displayUTC, 'Minutes')); | ||
var vSecond = addZero(datePart(date, displayUTC, 'Seconds')); | ||
var vMillisecond = padWithZeros(datePart(date, displayUTC, 'Milliseconds'), 3); | ||
var vTimeZone = offset(date.getTimezoneOffset()); | ||
var formatted = format | ||
.replace(/dd/g, vDay) | ||
.replace(/MM/g, vMonth) | ||
.replace(/y{1,4}/g, vYear) | ||
.replace(/hh/g, vHour) | ||
.replace(/mm/g, vMinute) | ||
.replace(/ss/g, vSecond) | ||
.replace(/SSS/g, vMillisecond) | ||
.replace(/O/g, vTimeZone); | ||
.replace(/dd/g, vDay) | ||
.replace(/MM/g, vMonth) | ||
.replace(/y{1,4}/g, vYear) | ||
.replace(/hh/g, vHour) | ||
.replace(/mm/g, vMinute) | ||
.replace(/ss/g, vSecond) | ||
.replace(/SSS/g, vMillisecond) | ||
.replace(/O/g, vTimeZone); | ||
return formatted; | ||
} | ||
function extractDateParts(pattern, str) { | ||
var matchers = [ | ||
{ pattern: /y{1,4}/, regexp: "\\d{1,4}", fn: function(date, value) { date.setFullYear(value); } }, | ||
{ pattern: /MM/, regexp: "\\d{1,2}", fn: function(date, value) { date.setMonth(value -1); } }, | ||
{ pattern: /dd/, regexp: "\\d{1,2}", fn: function(date, value) { date.setDate(value); } }, | ||
{ pattern: /hh/, regexp: "\\d{1,2}", fn: function(date, value) { date.setHours(value); } }, | ||
{ pattern: /mm/, regexp: "\\d\\d", fn: function(date, value) { date.setMinutes(value); } }, | ||
{ pattern: /ss/, regexp: "\\d\\d", fn: function(date, value) { date.setSeconds(value); } }, | ||
{ pattern: /SSS/, regexp: "\\d\\d\\d", fn: function(date, value) { date.setMilliseconds(value); } }, | ||
{ pattern: /O/, regexp: "[+-]\\d{3,4}|Z", fn: function(date, value) { | ||
if (value === 'Z') { | ||
value = 0; | ||
} | ||
var offset = Math.abs(value); | ||
var minutes = (offset % 100) + (Math.floor(offset / 100) * 60); | ||
date.setMinutes(date.getMinutes() + (value > 0 ? minutes : -minutes)); | ||
} } | ||
]; | ||
var parsedPattern = matchers.reduce(function(p, m) { | ||
if (m.pattern.test(p.regexp)) { | ||
m.index = p.regexp.match(m.pattern).index; | ||
p.regexp = p.regexp.replace(m.pattern, "(" + m.regexp + ")"); | ||
} else { | ||
m.index = -1; | ||
} | ||
return p; | ||
}, { regexp: pattern, index: [] }); | ||
var dateFns = matchers.filter(function(m) { | ||
return m.index > -1; | ||
}); | ||
dateFns.sort(function(a, b) { return a.index - b.index; }); | ||
var matcher = new RegExp(parsedPattern.regexp); | ||
var matches = matcher.exec(str); | ||
if (matches) { | ||
var date = module.exports.now(); | ||
dateFns.forEach(function(f, i) { | ||
f.fn(date, matches[i+1]); | ||
}); | ||
return date; | ||
} | ||
throw new Error('String \'' + str + '\' could not be parsed as \'' + pattern + '\''); | ||
} | ||
function parse(pattern, str) { | ||
if (!pattern) { | ||
throw new Error('pattern must be supplied'); | ||
} | ||
return extractDateParts(pattern, str); | ||
} | ||
/** | ||
* Used for testing - replace this function with a fixed date. | ||
*/ | ||
function now() { | ||
return new Date(); | ||
} | ||
module.exports = asString; | ||
module.exports.asString = asString; | ||
module.exports.parse = parse; | ||
module.exports.now = now; | ||
module.exports.ISO8601_FORMAT = 'yyyy-MM-ddThh:mm:ss.SSS'; | ||
@@ -78,0 +140,0 @@ module.exports.ISO8601_WITH_TZ_OFFSET_FORMAT = 'yyyy-MM-ddThh:mm:ss.SSSO'; |
{ | ||
"name": "date-format", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "Formatting Date objects as strings since 2013", | ||
@@ -28,8 +28,7 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"eslint": "^3.12.0", | ||
"eslint-plugin-import": "^2.2.0", | ||
"eslint-plugin-mocha": "^4.8.0", | ||
"mocha": "^3.2.0", | ||
"should": "^11.1.2" | ||
"eslint": "^5.5.0", | ||
"eslint-plugin-mocha": "^5.2.0", | ||
"mocha": "^5.2.0", | ||
"should": "^13.2.3" | ||
} | ||
} |
@@ -13,3 +13,6 @@ date-format | ||
```js | ||
Formatting dates as strings | ||
---- | ||
```javascript | ||
var format = require('date-format'); | ||
@@ -23,3 +26,3 @@ format.asString(); //defaults to ISO8601 format and current date. | ||
```js | ||
```javascript | ||
var format = require('date-format'); | ||
@@ -40,4 +43,19 @@ format(); //defaults to ISO8601 format and current date. | ||
* SSS - `date.getMilliseconds()` | ||
* O - timezone offset in +hm format | ||
* O - timezone offset in +hm format (note that time will be in UTC if displaying offset) | ||
That's it. | ||
Built-in formats: | ||
* `format.ISO8601_FORMAT` - `2017-03-14T14:10:20.391` (local time used) | ||
* `format.ISO8601_WITH_TZ_OFFSET_FORMAT` - `2017-03-14T03:10:20.391+1100` (UTC + TZ used) | ||
* `format.DATETIME_FORMAT` - `14 03 2017 14:10:20.391` (local time used) | ||
* `format.ABSOLUTETIME_FORMAT` - `14:10:20.391` (local time used) | ||
Parsing strings as dates | ||
---- | ||
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). | ||
```javascript | ||
var format = require('date-format'); | ||
// pass in the format of the string as first argument | ||
format.parse(format.ISO8601_FORMAT, '2017-03-14T14:10:20.391'); | ||
// returns Date | ||
``` |
@@ -37,4 +37,5 @@ 'use strict'; | ||
// when tz offset is in the pattern, the date should be in UTC | ||
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate) | ||
.should.eql('2010-01-11T14:31:30.005+1100'); | ||
.should.eql('2010-01-11T03:31:30.005+1100'); | ||
@@ -48,3 +49,3 @@ tzDate = createFixedDate(); | ||
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate) | ||
.should.eql('2010-01-11T14:31:30.005-0200'); | ||
.should.eql('2010-01-11T16:31:30.005-0200'); | ||
}); | ||
@@ -63,4 +64,4 @@ | ||
dateFormat.asString('O.SSS.ss.mm.hh.dd.MM.yy', customDate).should.eql('-0200.005.30.31.14.11.01.10'); | ||
dateFormat.asString('O.SSS.ss.mm.hh.dd.MM.yy', customDate).should.eql('-0200.005.30.31.16.11.01.10'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
15722
4
283
59
1