diff --git a/README.md b/README.md index 86f5dda..d472221 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,6 @@ This `npm` module focuses on the date formatting of the `new Date()` JS object. It parses the result to a legible `string` state for both data manipulation and display. -**Currently it only parses the current date.** - ## Usage & Installation To use this module install it with either `npm` @@ -48,7 +46,18 @@ const nonCompactFullDate = cdate('mm/dd/yy', { compact: false, fullDate: true }) // Monday, February 24 2020 ``` -If either the `format` of the date, or the `options` are wrong in some way, `cdate` will return `undefined` +Additionally, you can specify a custom date to parse with the same format passing a `instanceof` Date; the default parameter of this option will parse the current date: + +```javascript +const dateUnspecified = cdate('mm/dd/yy', { compact: true, fullDate: false }); +// Feb 24 2020 + +const dateSpecified = cdate('mm/dd/yy', { compact: true, fullDate: false }, new Date (2019, 10, 15)); +// Nov 15 2019 + +``` + +If either the `format` of the date, the `options` are wrong in some way, or the `date` variable is not a valid instance of Date or an invalid date, `cdate` will return `undefined`. #### Created by: diff --git a/lib/cdate.js b/lib/cdate.js index e4266bd..da305d7 100644 --- a/lib/cdate.js +++ b/lib/cdate.js @@ -3,13 +3,17 @@ import formatter from './helpers/formatter'; import getDays from './helpers/days'; -export default (format = 'mm/dd/yy', options = { compact: true, fullDate: false }) => { +export default (format = 'mm/dd/yy', options = { compact: true, fullDate: false }, date = new Date()) => { if (typeof options.compact !== 'boolean' || typeof options.fullDate !== 'boolean') { console.error('You can only pass booleans in the options object'); return undefined; } + if (!(date instanceof Date) || date === 'Invalid Date') { + console.error('The date attribute must be a Date type'); + return undefined; + } format.toLowerCase(); - const d = new Date().toString().split(' '); + const d = date.toString().split(' '); let date = formatter(d, format, options); if (options.fullDate) { return `${options.compact ? d[0] : getDays(d[0], true)}, ${date}`; diff --git a/package.json b/package.json index 73aa4c9..119e430 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cdate-formatting", - "version": "0.5.6", + "version": "1.0.0", "description": "A simple Node package library for quick JS Date formatting", "main": "index.js", "type": "module",