Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support to get the ISO-8601 numeric representation of the day of …

…the week when using "N".
  • Loading branch information...
commit e47717a2273bf5acfa4db3553653eb82b3d40843 1 parent fc4e32a
@lbdremy lbdremy authored
Showing with 31 additions and 2 deletions.
  1. +6 −0 Readme.md
  2. +15 −2 lib/dateformat.js
  3. +10 −0 test/test_dayofweek.js
View
6 Readme.md
@@ -6,6 +6,7 @@ A node.js package for Steven Levithan's excellent [dateFormat()][dateformat] fun
* Removed the `Date.prototype.format` method. Sorry folks, but extending native prototypes is for suckers.
* Added a `module.exports = dateFormat;` statement at the bottom
+* Added the placeholder `N` to get the ISO 8601 numeric representation of the day of the week
## Usage
@@ -59,6 +60,11 @@ As taken from Steven's post, modified to match the Modifications listed above:
// You can also get the ISO 8601 week of the year:
dateFormat(now, "W");
// 42
+
+ // and also get the ISO 8601 numeric representation of the day of the week:
+ dateFormat(now,"N");
+ // 6
+
## License
(c) 2007-2009 Steven Levithan [stevenlevithan.com][stevenlevithan], MIT license.
View
17 lib/dateformat.js
@@ -13,7 +13,7 @@
*/
var dateFormat = function () {
- var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZW]|"[^"]*"|'[^']*'/g,
+ var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZWN]|"[^"]*"|'[^']*'/g,
timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
timezoneClip = /[^-+\dA-Z]/g,
pad = function (val, len) {
@@ -49,6 +49,17 @@ var dateFormat = function () {
return 1 + weekDiff;
};
+ /**
+ * Get ISO-8601 numeric representation of the day of the week
+ * 1 (for Monday) through 7 (for Sunday)
+ */
+
+ getDayOfWeek = function(date){
+ var dow = date.getDay();
+ if(!dow) dow = 7;
+ return dow;
+ };
+
// Regexes and supporting functions are cached through closure
return function (date, mask, utc) {
var dF = dateFormat;
@@ -88,6 +99,7 @@ var dateFormat = function () {
L = date[_ + "Milliseconds"](),
o = utc ? 0 : date.getTimezoneOffset(),
W = getWeek(date),
+ N = getDayOfWeek(date),
flags = {
d: d,
dd: pad(d),
@@ -116,7 +128,8 @@ var dateFormat = function () {
Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10],
- W: W
+ W: W,
+ N: N
};
return mask.replace(token, function ($0) {
View
10 test/test_dayofweek.js
@@ -0,0 +1,10 @@
+var dateFormat = require('./../lib/dateformat'),
+ assert = require('assert');
+
+var start = 10; // the 10 of March 2013 is a Sunday
+for(var dow = 1; dow <= 7; dow++){
+ var date = new Date('2013-03-' + (start + dow));
+ var N = dateFormat(date,'N');
+ assert.equal(N,dow);
+ console.log('The ISO-8601 numeric representation of the day "' + date.toString() + '" is ' + N);
+}
Please sign in to comment.
Something went wrong with that request. Please try again.