Skip to content

Commit c813949

Browse files
author
E.Azer Koçulu
committed
first commit
0 parents  commit c813949

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
npm-debug.log

.npmignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test
2+
test.js
3+
example
4+
examples

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## format-date
2+
3+
Takes a string and a date object, returns what you expect
4+
5+
Example:
6+
7+
```js
8+
var formatDate = require('format-date')
9+
10+
formatDate('{month}/{day}/{year}', new Date);
11+
// => 21/09/2014
12+
13+
formatDate('{hours}:{minutes}:{seconds} {day-name}', new Date);
14+
// => 13:30:53 Monday
15+
16+
formatDate('{month-name} {year}')
17+
// => January 2014
18+
```
19+
20+
## Install
21+
22+
```bash
23+
$ npm install format-date
24+
```
25+
26+
## Reference
27+
28+
```js
29+
* day
30+
* day-name
31+
* month
32+
* month-name
33+
* year
34+
* hours
35+
* minutes
36+
* seconds
37+
```

index.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var format = require("format-text");
2+
var leftpad = require("left-pad");
3+
var days = require("days");
4+
var months = require("months");
5+
6+
module.exports = formatDate;
7+
8+
function formatDate (template, date) {
9+
if (!date) return template;
10+
11+
return format(template, {
12+
day: leftpad(date.getDate(), 2, '0'),
13+
month: leftpad(date.getMonth() + 1, 2, '0'),
14+
year: date.getFullYear(),
15+
hours: leftpad(date.getHours(), 2, '0'),
16+
minutes: leftpad(date.getMinutes(), 2, '0'),
17+
seconds: leftpad(date.getSeconds(), 2, '0'),
18+
'day-name': days[date.getDay()],
19+
'month-name': months[date.getMonth()]
20+
});
21+
}

package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "format-date",
3+
"version": "0.0.0",
4+
"description": "Takes a string and a date object, returns what you expect",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "node test"
8+
},
9+
"keywords": [
10+
"date",
11+
"datetime",
12+
"strftime",
13+
"format",
14+
"time"
15+
],
16+
"repository": {
17+
"url": "git@github.com:azer/format-date.git",
18+
"type": "git"
19+
},
20+
"author": "azer",
21+
"license": "BSD",
22+
"dependencies": {
23+
"format-text": "0.0.3",
24+
"left-pad": "0.0.3",
25+
"days": "^0.1.0",
26+
"months": "^0.1.0"
27+
},
28+
"devDependencies": {
29+
"prova": "^1.14.2"
30+
}
31+
}

test.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var format = require("./");
2+
var test = require("prova");
3+
var date = new Date(1411358832845);
4+
5+
test('day month and year', function (t) {
6+
t.plan(1);
7+
t.equal(format('{day} / {month} / {year}', date), '21 / 09 / 2014');
8+
});
9+
10+
test('day-name month-name and short-year', function (t) {
11+
t.plan(1);
12+
t.equal(format('{month-name} {day}, {day-name}', date), 'September 21, Sunday');
13+
});
14+
15+
test('hours, minutes and seconds', function (t) {
16+
t.plan(1);
17+
t.equal(format('{hours}:{minutes}:{seconds}', date), '21:07:12');
18+
});

0 commit comments

Comments
 (0)