I'm having real trouble working out whether JavaScript's getDay()
method (http://www.w3schools.com/jsref/jsref_getday.asp) complies with ISO 8601.
getDay()
returns 0
for Sunday, 1
for Monday etc..
The documentation recommends mapping to day names like this:
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
...
var n = weekday[(new Date()).getDay()];
I can only see that the ISO standard defines Monday as the first day of the week. I would assume this means Monday should be 0, not Sunday.
Does anyone have any experience with this? Can you clarify and would you recommend overriding the method to make Monday the first day of the week?
The date. toISOString() method is used to convert the given date object's contents into a string in ISO format (ISO 8601) i.e, in the form of (YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss.
Date.prototype.toISOString() The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .
All weeks in the ISO-8601 Week-Based calendar have exactly 7 days, start on a Monday, and each week belongs to single year. Unlike the Gregorian calendar, there are no weeks that extend across years. Each ISO-8601 year is either a Long or a Short year, with 52 or 53 weeks depending on when the ISO-8601 year begins.
Converts a date to extended ISO format (ISO 8601), including timezone offset. Use Date. prototype. getTimezoneOffset() to get the timezone offset and reverse it.
var weekday=new Array(7);
weekday[0]="Monday";
weekday[1]="Tuesday";
weekday[2]="Wednesday";
...
var n = weekday[((new Date()).getDay() + 6) % 7 + 1];
or extend the Date prototype:
Date.prototype.getISODay = function(){ return (this.getDay() + 6) % 7 + 1; }
and then use
var n = weekday[(new Date()).getISODay()];
How the weekdays are represented only matters if you want to use a date format where the weekday actually is represented as a number, e.g. the YYYY-Www-D
format, or if you want to get the week number for a specific date.
Otherwise it doesn't matter how the weekdays are represented numerically. A monday is still a monday.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With