Adds Date.toFirstOfWeek() method that sets the date to the first day in the week (Sunday).
Example: 6/28/2011 turns into 6/26/2011
Date.prototype.toFirstOfWeek = function() { | |
// just subtract the day of the week by the date. | |
this.setDate( this.getDate() - this.getDay() ); | |
} |
Date.prototype.toFirstOfWeek=function(){this.setDate(this.getDate()-this.getDay());} |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
{ | |
"name": "toFirstOfWeek", | |
"description": "Sets the date to the first day of the week (Sunday)", | |
"keywords": [ | |
"date", | |
"prototype", | |
"week", | |
"sunday", | |
"first day of week" | |
] | |
} |
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b>undefined</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
Date.prototype.toFirstOfWeek=function(){this.setDate(this.getDate()-this.getDay());} | |
var date = new Date(2011, 5, 29); | |
date.toFirstOfWeek(); | |
// date now equals Sunday, June 26th 2011 | |
</script> |