Comparing version 1.0.2 to 1.1.0
'use strict'; | ||
function tidyNumber(value) { | ||
if (value < 10) { | ||
return '0' + String(value); | ||
module.exports = function (pattern, setDate) { | ||
var date; | ||
function tidyNumber(value) { | ||
if (value < 10) { | ||
return '0' + String(value); | ||
} | ||
return String(value); | ||
} | ||
return String(value); | ||
} | ||
function tidyMs(value) { | ||
if (value < 10) { | ||
return '00' + String(value); | ||
function tidyMs(value) { | ||
if (value < 10) { | ||
return '00' + String(value); | ||
} | ||
if (value < 100) { | ||
return '0' + String(value); | ||
} | ||
return String(value); | ||
} | ||
if (value < 100) { | ||
return '0' + String(value); | ||
if (setDate) { | ||
if (String(Date.parse(setDate)) === 'NaN') { | ||
throw new Error('The supplied date string was not formatted correctly.'); | ||
} | ||
date = new Date(setDate); | ||
} else { | ||
date = new Date(); | ||
} | ||
return String(value); | ||
} | ||
module.exports = function (pattern) { | ||
var date = new Date(); | ||
return pattern | ||
@@ -33,3 +37,2 @@ .replace('d', tidyNumber(date.getUTCDate())) | ||
.replace('l', tidyMs(date.getMilliseconds())); | ||
}; |
@@ -6,7 +6,11 @@ { | ||
"easydate.js", | ||
"test.js" | ||
"test.js", | ||
"LICENSE.md" | ||
], | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "get date and/or time by pattern", | ||
"keywords": [ | ||
"datetime", | ||
"dateformat", | ||
"formatting", | ||
"date", | ||
@@ -13,0 +17,0 @@ "time" |
@@ -1,11 +0,7 @@ | ||
# easydate | ||
# easydate [![Build Status](https://travis-ci.org/roryrjb/easydate.svg?branch=master)](https://travis-ci.org/roryrjb/easydate) | ||
### Overview | ||
> Returns the date according to a pattern. | ||
Returns the date according to a pattern. | ||
### Installation | ||
Install using npm: | ||
``` | ||
@@ -15,7 +11,20 @@ $ npm install easydate | ||
### API | ||
__Test:__ | ||
``` | ||
$ npm test | ||
``` | ||
### Usage/API | ||
__easydate(patternString, [inputDate])__ | ||
The single exported function has two arguments. The first and only required argument is the pattern string (see _Pattern Options_ below). If only including the pattern string it will return a formatted string for the current date-time, if the optional input date string is supplied, that particular date will be returned formatted. This input date string must be parseable by JavaScript's `Date.parse` function; see below for acceptable examples. | ||
__Examples:__ | ||
```javascript | ||
var easydate = require('easydate'); | ||
// current date/time | ||
easydate('d-M-y'); // "28-01-14" | ||
@@ -26,2 +35,7 @@ easydate('d/M/Y'); // "28/01/2014" | ||
easydate('d-M-Y @ h:m:s.l'); // "29-01-2014 @ 07:22:37.418" | ||
// specified date/time | ||
easydate('d-M-Y @ h:m', '2015-11-03T16:06:00.000Z'); // "03-11-2015 @ 16:06" | ||
easydate('h:m:s.l', '2015-11-03T16:06:08.123Z'); // "16:06:08.123" | ||
easydate('M~d~Y', '03-01-2017'); // "03~01~2017" | ||
``` | ||
@@ -44,2 +58,6 @@ | ||
Any instances of the above characters will be replaced with the relevant numbers. It is recommended to not use words within the pattern string. | ||
Any instances of the above characters will be replaced with the relevant numbers. It is recommended to not use words within the pattern string. | ||
### License | ||
MIT |
22
test.js
'use strict'; | ||
var easydate = require('./easydate'); | ||
var assert = require('assert'); | ||
console.log(easydate('d-M-y')); | ||
console.log(easydate('d/M/Y')); | ||
console.log(easydate('Y.M.d')); | ||
console.log(easydate('d-M-Y @ h:m:s.l')); | ||
assert.equal(easydate('d/M/y', '2016-10-01T00:00:00.000Z'), '01/10/16'); | ||
assert.equal(easydate('d/M/Y', '2015-11-03T00:00:00.000Z'), '03/11/2015'); | ||
assert.equal(easydate('d-M-Y @ h:m', '2015-11-03T16:06:00.000Z'), '03-11-2015 @ 16:06'); | ||
assert.equal(easydate('h:m:s', '2015-11-03T16:06:08.000Z'), '16:06:08'); | ||
assert.equal(easydate('h:m:s.l', '2015-11-03T16:06:08.123Z'), '16:06:08.123'); | ||
assert.equal(easydate('d-M-y', '03/01/2017'), '01-03-17'); | ||
assert.equal(easydate('M~d~Y', '03/01/2017'), '03~01~2017'); | ||
assert.equal(easydate('M/d/Y', '03/01/2017'), '03/01/2017'); | ||
assert.equal(easydate('M/d/Y', '01/01/2017'), '01/01/2017'); | ||
assert.equal(easydate('M/d/Y', '12/31/3045'), '12/31/3045'); | ||
assert.equal(easydate('d-M-y', '03-01-2017'), '01-03-17'); | ||
assert.equal(easydate('M~d~Y', '03-01-2017'), '03~01~2017'); | ||
assert.equal(easydate('M/d/Y', '03-01-2017'), '03/01/2017'); | ||
assert.equal(easydate('M/d/Y', '01-01-2017'), '01/01/2017'); | ||
assert.equal(easydate('M/d/Y', '12-31-3045'), '12/31/3045'); |
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
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
5532
5
54
61