Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
test | ||
test.js | ||
example | ||
examples |
37
README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
## format-date | ||
|
||
Takes a string and a date object, returns what you expect | ||
|
||
Example: | ||
|
||
```js | ||
var formatDate = require('format-date') | ||
formatDate('{month}/{day}/{year}', new Date); | ||
// => 21/09/2014 | ||
formatDate('{hours}:{minutes}:{seconds} {day-name}', new Date); | ||
// => 13:30:53 Monday | ||
formatDate('{month-name} {year}') | ||
// => January 2014 | ||
``` | ||
|
||
## Install | ||
|
||
```bash | ||
$ npm install format-date | ||
``` | ||
|
||
## Reference | ||
|
||
```js | ||
* day | ||
* day-name | ||
* month | ||
* month-name | ||
* year | ||
* hours | ||
* minutes | ||
* seconds | ||
``` |
21
index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var format = require("format-text"); | ||
var leftpad = require("left-pad"); | ||
var days = require("days"); | ||
var months = require("months"); | ||
|
||
module.exports = formatDate; | ||
|
||
function formatDate (template, date) { | ||
if (!date) return template; | ||
|
||
return format(template, { | ||
day: leftpad(date.getDate(), 2, '0'), | ||
month: leftpad(date.getMonth() + 1, 2, '0'), | ||
year: date.getFullYear(), | ||
hours: leftpad(date.getHours(), 2, '0'), | ||
minutes: leftpad(date.getMinutes(), 2, '0'), | ||
seconds: leftpad(date.getSeconds(), 2, '0'), | ||
'day-name': days[date.getDay()], | ||
'month-name': months[date.getMonth()] | ||
}); | ||
} |
31
package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "format-date", | ||
"version": "0.0.0", | ||
"description": "Takes a string and a date object, returns what you expect", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test" | ||
}, | ||
"keywords": [ | ||
"date", | ||
"datetime", | ||
"strftime", | ||
"format", | ||
"time" | ||
], | ||
"repository": { | ||
"url": "git@github.com:azer/format-date.git", | ||
"type": "git" | ||
}, | ||
"author": "azer", | ||
"license": "BSD", | ||
"dependencies": { | ||
"format-text": "0.0.3", | ||
"left-pad": "0.0.3", | ||
"days": "^0.1.0", | ||
"months": "^0.1.0" | ||
}, | ||
"devDependencies": { | ||
"prova": "^1.14.2" | ||
} | ||
} |
18
test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var format = require("./"); | ||
var test = require("prova"); | ||
var date = new Date(1411358832845); | ||
|
||
test('day month and year', function (t) { | ||
t.plan(1); | ||
t.equal(format('{day} / {month} / {year}', date), '21 / 09 / 2014'); | ||
}); | ||
|
||
test('day-name month-name and short-year', function (t) { | ||
t.plan(1); | ||
t.equal(format('{month-name} {day}, {day-name}', date), 'September 21, Sunday'); | ||
}); | ||
|
||
test('hours, minutes and seconds', function (t) { | ||
t.plan(1); | ||
t.equal(format('{hours}:{minutes}:{seconds}', date), '21:07:12'); | ||
}); |