Skip to content

Instantly share code, notes, and snippets.

@ripter
Forked from 140bytes/LICENSE.txt
Created June 27, 2011 15:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ripter/1049116 to your computer and use it in GitHub Desktop.
Save ripter/1049116 to your computer and use it in GitHub Desktop.
Set date to the first day of the week

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment