Video: Quick overview of AtScript at ng-europe 2014
Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94
In Javascript, I find it strange how `Date.prototype.getDay()` in defined but `Date.prototype.setDay()`.
So here’s the code for `Date.prototype.setDay()`.
// Sets the Date object to the day of the week. // @param {Number} - dayIndex must be from 0 - 6, where 0 is sunday. // @return {Number} returns the primitive value of the Date object. Date.prototype.setDay = Date.prototype.setDay || function (dayIndex) { dayIndex = Math.floor(+dayIndex); if (dayIndex < 0 || 6 < dayIndex) { throw new Error("Must pass integer between 0-6, where sunday is 0."); } this.setDate( this.getDate() + dayIndex - this.getDay() ); return this.valueOf(); }; |
var a = new Date(Date.parse("Feb 1, 2014")); a.setDay(0); console.log(a.toDateString() === "Sun Jan 26 2014" ); a.setDay(6); console.log(a.toDateString() === "Sat Feb 01 2014" ); |
// Prings the Monday - Friday for the current day. var getWeekString = function () { var days = [], date = new Date(); for (var i = 1, len = 6; i < len; i++) { date.setDay(i) days.push("## " + date.toDateString() ); } return days; }; console.log(getWeekString().join("\n\n")); |
If you looking for a good Javascript Date library, checkout moment.js.
More info:
MDN Date Object
Here’s a cool demo that I wanted to share.
Source: http://liveweave.com/TcZrXF
/**
* Returns an array that contains non-array elements.
* If an element is an array, then the element is replaced by the content of the array.
* @param{Array}
* @return{Array} return null if no arguments are passed.
* @example
flatten([[1,2],3,[4]]); // returns [1,2,3,4];
*/
var flatten = function(arr){
if(!Array.isArray(arr)){
return (arr === null || arr === undefined) ? null : [arr];
}
var result = [], obj;
for(var i = 0, len = arr.length; i < len; i++){
obj = arr[i];
if(Array.isArray(arr)){
obj = flatten(obj);
}
result = result.concat( obj );
}
return result;
};