Permalink
Browse files

first commit

  • Loading branch information...
azer committed Sep 22, 2014
0 parents commit c813949879dcd84768be20dcdc51f122c00ec445
Showing with 113 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +4 −0 .npmignore
  3. +37 −0 README.md
  4. +21 −0 index.js
  5. +31 −0 package.json
  6. +18 −0 test.js
@@ -0,0 +1,2 @@
+node_modules
+npm-debug.log
@@ -0,0 +1,4 @@
+test
+test.js
+example
+examples
@@ -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
+```
@@ -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()]
+ });
+}
@@ -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
@@ -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');
+});

0 comments on commit c813949

Please sign in to comment.