Permalink
Browse files

First commit

  • Loading branch information...
eivindfjeldstad committed May 9, 2012
0 parents commit 09263a38fa544381bca30c39e930b677efc857b4
Showing with 196 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +2 −0 .npmignore
  3. +163 −0 index.js
  4. +8 −0 package.json
  5. +21 −0 test.js
@@ -0,0 +1,2 @@
+node_modules
+.DS_STORE
@@ -0,0 +1,2 @@
+node_modules
+.DS_STORE
163 index.js
@@ -0,0 +1,163 @@
+var oneDay = 1000 * 60 * 60 * 24
+ , formats = {};
+
+exports.formats = formats;
+
+/**
+ * Returns a date as a formatted string
+ *
+ * @param {Date} date
+ * @param {String} format
+ * @return {String}
+ */
+
+exports.toFormat = function (date, format) {
+ var tokens = /Y{2,4}|[MDhms]{1,2}|"[^"]*"|'[^']*'/g;
+
+ format = formats[format] || format;
+
+ return format.replace(tokens, function (part) {
+ switch (part) {
+ case 'YYYY':
+ return date.getFullYear();
+ case 'YY':
+ return date.getYear();
+ case 'MM':
+ return padZero(date.getMonth() + 1);
+ case 'M':
+ return date.getMonth() + 1;
+ case 'DD':
+ return padZero(date.getDate());
+ case 'D':
+ return date.getDate();
+ case 'hh':
+ return padZero(date.getHours());
+ case 'h':
+ return date.getHours();
+ case 'mm':
+ return padZero(date.getMinutes());
+ case 'm':
+ return date.getMinutes();
+ case 'ss':
+ return padZero(date.getSeconds());
+ case 's':
+ return date.getSeconds();
+ default:
+ return part.slice(1, part.length - 1);
+ };
+ });
+};
+
+/**
+ * Parses a date based on a format
+ *
+ * @param {String} string
+ * @param {String} format
+ * @return {Date}
+ */
+
+exports.parse = function (string, format) {
+ var tokens
+ , index
+ , parts = {}
+ , offset = 0
+ , stringLength
+ , tokenLength;
+
+ format = formats[format] || format;
+
+ format = format.replace(/"[^"]*"|'[^']*'/g, function (part) {
+ string = string.replace(part.slice(1, part.length - 1), '');
+ return '';
+ });
+
+ tokens = format.match(/Y{2,4}|[MDhms]{1,2}/g);
+ stringLength = string.length
+
+ for (var i = 0; i < tokens.length; i++) {
+ index = format.indexOf(tokens[i]) + offset;
+
+ if (index - offset < 0) continue;
+
+ tokenLength = tokens[i].length;
+ parts[tokens[i][0]] = string.substr(index, tokenLength);
+ index += tokenLength - 1;
+
+ while (++index < stringLength) {
+ if (!(/[0-9]/).test(string[index]))
+ break;
+
+ parts[tokens[i][0]] += string[index];
+ offset++;
+ }
+ }
+
+ return new Date(
+ parts.Y || 0
+ , (parts.M || 1) - 1
+ , parts.D || 0
+ , parts.h || 0
+ , parts.m || 0
+ , parts.s || 0
+ );
+};
+
+/**
+ * Returns the number of days until a date
+ *
+ * @param {Date} date
+ * @return {int}
+ */
+
+exports.daysUntil = function (date) {
+ return Math.round((end.valueOf() - Date.now()) / oneDay);
+};
+
+/**
+ * Returns the number of days since a date
+ *
+ * @param {Date} date
+ * @return {int}
+ */
+
+exports.daysSince = function (date) {
+ return -exports.daysUntil(date);
+};
+
+/**
+ * Returns the number of days between two dates
+ *
+ * @param {Date} date
+ * @return {int}
+ */
+
+exports.daysBetween = function (start, end) {
+ return Math.abs(Math.round((end.valueOf() - start.valueOf()) / oneDay));
+};
+
+/**
+ * Returns the number of days in a month
+ *
+ * @param {Date|int} year
+ * @param {int} month
+ */
+
+exports.daysInMonth = function (year, month) {
+ var date = year;
+
+ if (!(date instanceof Date))
+ date = new Date(year, month);
+
+ return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
+}
+
+/**
+ * Pads out a number to two digits
+ *
+ * @param {int} number
+ * @return {String}
+ */
+
+function padZero (number) {
+ return number < 10 ? '0' + number : '' + number;
+}
@@ -0,0 +1,8 @@
+{
+ "name": "node-hotdate",
+ "description": "A date formatter and parser for node",
+ "version": "1.0.0",
+ "author": "Eivind Fjeldstad",
+ "dependencies": {},
+ "devDependencies": {}
+}
21 test.js
@@ -0,0 +1,21 @@
+var hotdate = require('./')
+ , assert = require('assert')
+ , date = new Date(2012, 04, 17, 21, 10, 35)
+ , formatted
+ , parsed
+
+hotdate.formats.test = '"Testing date" D/M-YYYY, "and time" hh:mm:ss';
+formatted = hotdate.toFormat(date, 'test');
+parsed = hotdate.parse(formatted, 'test');
+
+assert.equal(formatted, 'Testing date 17/5-2012, and time 21:10:35');
+assert.notStrictEqual(parsed, date);
+assert.equal(parsed.getFullYear(), date.getFullYear());
+assert.equal(parsed.getMonth(), date.getMonth());
+assert.equal(parsed.getDate(), date.getDate());
+assert.equal(parsed.getHours(), date.getHours());
+assert.equal(parsed.getMinutes(), date.getMinutes());
+assert.equal(parsed.getSeconds(), date.getSeconds());
+console.log(hotdate.daysBetween(date, parsed));
+console.log(hotdate.daysBetween(date, new Date(2012, 04, 18, 21, 10, 40)));
+console.log('All tests passed');

0 comments on commit 09263a3

Please sign in to comment.