开发者

javascript - is current date x days since given date?

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..

javascript date + 7 days

add x days to given date

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...

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));
};

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

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

I want to fetch the date later, how it is retrieved is also irrelevent. Just assume it is 'd'. 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.

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

I tried...

var today = new Date().ToUTCString();

var result = today.daysTo(d);

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

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 obviou开发者_如何学Pythons.

Here we go, a better code snippet.

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));

I get the error

present has no function 'daysTo'


I think the problem is that you have defined the daysTo() function as a method of the Date object, but you are trying to call it on a string.

That is, var present = new Date().toUTCString(); results in present being a string, since toUTCString() returns a string.

I would do all of the Date arithmetic before calling toUTCString(), like:

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

alert(present.daysTo(past));

Only call toUTCString() when storing (and possibly displaying) the date.

Also, don't know if this is an option, depending on how you are storing it, but you may want to store it as a Date object, rather than a string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜