javascript - is current date x days since given date?
<p>There are a thousand topics on dates and how annoying they are to use in javascript. Below are a few that I used to get to the point I am at.. </p>

<p><a href="http://stackoverflow.com/questions/5741632/javascript-date-7-days">javascript date + 7 days</a></p>

<p><a href="http://stackoverflow.com/questions/5767704/script-that-adds-x-days-to-a-given-date">add x days to given date</a></p>

<p>but it is either too early and my brain is fogged, or I am just struggling with some of the logic here. I've created 3 methods, or functions rather...</p>

<pre><code>Date.prototype.daysTo = function(target) {
    var divisor = (1000 * 60 * 60 * 24);
    return (((this - target) / divisor));
};
Date.prototype.hoursTo = function(target) {
    var divisor = (1000 * 60 * 60);
    return (((this - target) / divisor));
};
Date.prototype.minutesTo = function(target) {
    var divisor = (1000 * 60);
    return (((this - target) / divisor));
};
</code></pre>

<p>My basic goal is to save a date (using Date().toUTCString()) like this <code>var d = new Date().toUTCString();</code> and then store that somewhere (where it is stored is irrelevent)</p>

<p>now then, that date is saved, everyone has a party, there are chicken wings etc. </p>

<p>I want to fetch the date later, how it is retrieved is also irrelevent. Just assume it is '<code>d</code>'. Now this is the part that should be excessively simple, and I've done it a half billion times in C#, but for heaven only knows what reason I am struggling to repeat it in javascript. </p>

<p>I want to take an integer (<code>7</code>) and then see if another given date (in most situations, 'today') is <code>n days since d</code>. </p>

<p>I tried... </p>

<pre><code>var today = new Date().ToUTCString();

var result = today.daysTo(d);
</code></pre>

<p>and I thought this would give me the appropriate value, but I am just getting strange 'undefined' values. </p>

<p>Can someone help me out? This should be really simple and I honestly don't know why I am struggling so much with it, but for whatever reason I am, and it is driving me batty. I think I am missing something very obvious.</p>

<p>Here we go, a better code snippet.</p>

<pre><code>Date.prototype.daysTo = function(target) {
    var divisor = (1000 * 60 * 60 * 24);
    return (((this - target) / divisor));
};
Date.prototype.hoursTo = function(target) {
    var divisor = (1000 * 60 * 60);
    return (((this - target) / divisor));
};
Date.prototype.minutesTo = function(target) {
    var divisor = (1000 * 60);
    return (((this - target) / divisor));
};

var past = new Date(2011,7,1).toUTCString();
var present = new Date().toUTCString();

alert(present.daysTo(past));
</code></pre>

<p>I get the error</p>

<blockquote>
  <p>present has no function 'daysTo'</p>
</blockquote>
