See if date is within last three years.

E

Erich93063

What is the proper syntax to see if a given date falls within 3 years
from todays date? I need it to actually do three years not just take
the year of todays date and subtract 3 which may not actually be three
full years ago. In other words if we are mid way through this year. I
need to go back mid way through the year three years ago.

Basically I have some records that have dates attached to them and i
only want to display them if they are between today and exactly three
years ago.

THANKS
 
J

Jeff North

On Tue, 3 May 2011 14:14:28 -0700 (PDT), in comp.lang.javascript
Erich93063 <[email protected]>
| What is the proper syntax to see if a given date falls within 3 years
| from todays date? I need it to actually do three years not just take
| the year of todays date and subtract 3 which may not actually be three
| full years ago. In other words if we are mid way through this year. I
| need to go back mid way through the year three years ago.
|
| Basically I have some records that have dates attached to them and i
| only want to display them if they are between today and exactly three
| years ago.
|
| THANKS

You can use:
today = new Date(); // get todays date
diff = 365.25 * 3; // account for leap years (sort of)
back3 = today.setDate(-diff); // subtract from today
dt = new Date(back3); // make new date

or
today = new Date(); // get todays date
back3 = today.setYear(-3); // sub 3 years
dt = new Date(back3); // make new date

HTH
 
R

RobG

What is the proper syntax to see if a given date falls within 3 years
from todays date? I need it to actually do three years not just take
the year of todays date and subtract 3 which may not actually be three
full years ago. In other words if we are mid way through this year. I
need to go back mid way through the year three years ago.

Basically I have some records that have dates attached to them and i
only want to display them if they are between today and exactly three
years ago.

Convert the date string into a date object, add 3 years to the year.
If the resulting object is equal to or greater than today's date, it's
within 3 years. e.g.


// dateString is suitable format, e.g. 2009/05/28
function isWithinThreeYears(dateString) {
var now = new Date();
now.setHours(0,0,0,0);
var then = new Date(dateString);
then.setFullYear(then.getFullYear() + 3);
return now <= then;
}
 
B

Benjamin

@gmail.com>


You can use:
today = new Date();  // get todays date
diff = 365.25 * 3;       // account for leap years (sort of)
back3 = today.setDate(-diff);   // subtract from today
dt = new Date(back3); // make new date

or
today = new Date();  // get todays date
back3 = today.setYear(-3); // sub 3 years
dt = new Date(back3);   // make new date

HTH

The first method listed is *really* inaccurate. Consider what would
happen when the date is midnight on the first of January.

That second one's also not quite right. Firstly, setYear is
deprecated, and you should favour using setFullYear instead of it.
Secondly, both setYear and setFullYear take an absolute year, not a
relative one. Here's a better version of the second one:

var then;

(then = new Date()).setFullYear(then.getFullYear() - 3);
alert("Three years ago, it was:\n" + then);
 
L

Lasse Reichstein Nielsen

In what format is the old date represented?
Convert the date string into a date object, add 3 years to the year.
If the resulting object is equal to or greater than today's date, it's
within 3 years. e.g.

Or, go in the other direction, so you have the old date in a format
where you don't need to create an old Date object for each test:

var limit = new Date();
limit.setHours(0,0,0,0); // Good idea!
limit.setFullYear(limit.getFullYear() - 3);
var limitTime = limit.getTime();
function isWithinThreeYears(msSinceEpoch) {
return limitTime <= msSinceEpoch;
}


/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
In what format is the old date represented?


Or, go in the other direction, so you have the old date in a format
where you don't need to create an old Date object for each test:

var limit = new Date();
limit.setHours(0,0,0,0); // Good idea!
limit.setFullYear(limit.getFullYear() - 3);
var limitTime = limit.getTime();
function isWithinThreeYears(msSinceEpoch) {
return limitTime <= msSinceEpoch;
}

The quoted solutions all fail to address the issue. The OP has made it
clear from the beginning that it does _not_ mean for them to simply
substract 3 from the year value. Even more wrong then, if there is such a
condition, is resetting the time to zero hours here (_Bad_ idea!).

So, the question is: What do they mean by "(full) year" then? How do *they*
define "one (full) year ago" relative to now? And what do they mean by
"exactly three (full) years ago", given that, except perhaps in bookkeeping,
not all years have the same number of days, months, let alone hours, and in
UTC, seconds?


PointedEars
 
E

Evertjan.

Thomas 'PointedEars' Lahn wrote on 04 mei 2011 in comp.lang.javascript:
The quoted solutions all fail to address the issue. The OP has made
it clear from the beginning that it does _not_ mean for them to simply
substract 3 from the year value. Even more wrong then, if there is
such a condition, is resetting the time to zero hours here (_Bad_
idea!).

This is not such a bad idea, as it adresses the possible defined
difference between summer/wintertime [dst for the transponders] from one
year to the other.

I would do [better be safe that sorry] something like this:

===================================
function testNyears(date1, date2, yDiff) {

day1 = date1.getDate();
month1 = date1.getMonth();
year1 = date1.getFullYear();

day2 = date2.getDate();
month2 = date2.getMonth();
year2 = date2.getFullYear();

return (year1 < year2 - yDiff) ?
false :
(year1 > year2 - yDiff) ?
true : // else same year
(month1 < month2) ?
false :
(month1 > month2) ?
true : // else same month
(day1 >= day2);
};

alert( testNyears(myDate1, myDate2, 3) );

====================================

So, the question is: What do they mean by "(full) year" then? How do
*they* define "one (full) year ago" relative to now? And what do they
mean by "exactly three (full) years ago", given that, except perhaps
in bookkeeping, not all years have the same number of days, months,
let alone hours, and in UTC, seconds?

Indeed.

I read the OQ as date difference,
not defining the year as 365.24... days, but as a calendar year.
and not counting hours.
 
T

Thomas 'PointedEars' Lahn

SteveYoungGoogle said:
I agree that not all years have the same number of days, hours or
seconds but don't all years (on Earth) have 11 months plus whatever
number of days, hours and seconds?

Strike "months" from that list.

One possible definition of a year would be that it has _12_ months, of
course. (Which leads to the question how one would define a month aso.)


PointedEars
 
L

Lasse Reichstein Nielsen

Erich93063 said:
What is the proper syntax to see if a given date falls within 3 years
from todays date? I need it to actually do three years not just take
the year of todays date and subtract 3 which may not actually be three
full years ago.

You need to define what a "full year" is then.
Between the 3rd of April last year and this year, I'd say there is a
full year.

Can you describe a situation where the same date three years
earlier (or the first of March three years ago if it's the 29th
of February today) isn't three full years ago?
In other words if we are mid way through this year. I
need to go back mid way through the year three years ago.

So if you are 45 days into this year (365 days long), you want the
date that's 45/365*366 days into the year three years ago (366 days
long)? Round up or down?
 
D

Dr J R Stockton

In comp.lang.javascript message <cd5edca8-7cd4-422a-9d71-3c6f35f1af24@k3
g2000prl.googlegroups.com>, Tue, 3 May 2011 14:14:28, Erich93063
What is the proper syntax to see if a given date falls within 3 years
from todays date? I need it to actually do three years not just take
the year of todays date and subtract 3 which may not actually be three
full years ago. In other words if we are mid way through this year. I
need to go back mid way through the year three years ago.

Basically I have some records that have dates attached to them and i
only want to display them if they are between today and exactly three
years ago.


Today being 2011-05-04, you mean files from after the end of 2008-05-03,
or files from after the end of 2008-05-04, or files from after the
current time on 2008-05-04. Or possibly something else. You also need
to consider what to do about leap years; when exactly was three years
before 2012-02-29?

You need to be aware of the possibility that one date is in Summer Time
and the other is not (if you are American and wanted five years ago, you
would also have to consider whether the change in DST rules effective
2007-03-01 [at presumably 00:00:00 local time] might matter).

And you should consider what to do if your business operates in more
than one time zone or in places with different Summer Time Rules -
especially Tel Aviv. Perhaps UTC should be used.

To get the date three years ago, letting the effect of those
considerations be what it might, you only need

D = new Date()
D.setFullYear(D.getFullYear()-3)
// D.setUTCFullYear(D.getUTCFullYear()-3)

You can compare date objects such as D with D1>D2 etc.
You can remove the time from a local date with D.setHours(0,0,0,0).

But, if your midway example be taken literally, you need (if either year
is Leap) to take the fraction of the way through the current year,
including the effects of February 29 and Summer Time, and apply that
fraction to the year three before - which will be in general at a
different time of day and often a different day, or date, of the year.

Some of those considerations apply to the upper end of the allowable
range; remember that clocks go BACK in autumn, so an hour is repeated.

See sig below, and its js-index.htm & js-dates.htm.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
The quoted solutions all fail to address the issue. The OP has made it
clear from the beginning that it does _not_ mean for them to simply
substract 3 from the year value. Even more wrong then, if there is such a
condition, is resetting the time to zero hours here (_Bad_ idea!).

The question is about dates, not about instants. Therefore, the answer
should never depend on the current time-of-day. Setting the time-of-day
to zero is an easy way of ensuring that.

For similar reasons, one should never create a Date Object with new
Date() unless at least some part of the current date+time is needed.
Instead, one should use, for example, new Date(0) which is guaranteed
reproducible - and is quicker, too.

Of course, it's commonly rash to assume that the originator has got the
question right!
 
D

Dr J R Stockton

In comp.lang.javascript message <25732f2c-4f94-47a9-8c7f-db132eed6bce@a1
0g2000vbz.googlegroups.com>, Wed, 4 May 2011 00:45:16, SteveYoungGoogle
I agree that not all years have the same number of days, hours or
seconds but don't all years (on Earth) have 11 months plus whatever
number of days, hours and seconds?

Try asking that at your local synagogue; see
<http://www.merlyn.demon.co.uk/heb-date.htm>.
 
D

Dr J R Stockton

In comp.lang.javascript message <cd5edca8-7cd4-422a-9d71-3c6f35f1af24@k3
g2000prl.googlegroups.com>, Tue, 3 May 2011 14:14:28, Erich93063
What is the proper syntax to see if a given date falls within 3 years
from todays date? I need it to actually do three years not just take
the year of todays date and subtract 3 which may not actually be three
full years ago. In other words if we are mid way through this year. I
need to go back mid way through the year three years ago.

Basically I have some records that have dates attached to them and i
only want to display them if they are between today and exactly three
years ago.

The most efficient way depends on the manner in which the dates of the
records are represented.

If the records are dated in a Year Month Day fashion or similar (for
example, DOS datestamps are stored in 32 bits as YYYYYYY MMMM DDDDD
hhhhh mmmmmm sssss), then the efficient way is likely to be to convert
(if necessary) the file dates to an ISO 8601 format, and to compare them
as strings to a single copy of today's date with the year part reduced
by three.

In the specific case of DOS dates, there is no need to add the 1980;
subtract it from today instead. And, for those, in this application,
just compare the upper 16 bits, as unsigned integer, with that for today
minus 3 * 2^9. Of course, accessing DOS datestamps directly from
JavaScript may nor be feasible.

The string comparison may be slightly slower than the comparison of Date
Objects (as IEEE Doubles), but the preparation for comparison should be
usefully faster (untested).

If these are business records, it could be that their dates are stored
not as Y M D but as Y W D or Y D. Just use the same format for today.
 
R

RobG

                                                           ^^^^^^^^^^^^^>>> the year oftodays date and subtract 3 which may not actually be three

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [...]

The quoted solutions all fail to address the issue.  The OP has made it
clear from the beginning that it does _not_ mean for them to simply
substract 3 from the year value.

I understood that requirement as: don't simply compare years, e.g.
2011 - 2008, but include the month and date in the comparison. If that
is correct, then the solutions offered meet the criterion. The OP has
not updated or clarified the requirement, the point is moot.
 
T

Thomas 'PointedEars' Lahn

Dr said:
Thomas said:
The quoted solutions all fail to address the issue. The OP has made it
clear from the beginning that it does _not_ mean for them to simply
substract 3 from the year value. Even more wrong then, if there is such
a condition, is resetting the time to zero hours here (_Bad_ idea!).

The question is about dates, not about instants. […]

No, it is not. It is about "within (the) last three years" (Subject) or
"within 3 years from todays date". It is entirely possible to have a
definition of "within (the) last three years" that includes the time of day.
Indeed, the emphasis on the words "exactly" and "full" in the OP rather
suggests that the time of day has to be considered here.
Of course, it's commonly rash to assume that the originator has got the
question right!

Granted, the wording in the OP is confusing, if not contradictory:

| I need it to actually do three years not just take the year of todays date
| and subtract 3 which may not actually be three full years ago. In other
| words if we are mid way through this year. I need to go back mid way
| through the year three years ago.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <7bb0de46-0559-4b72-a582-a21584f596e4@j1
3g2000pro.googlegroups.com>, Thu, 5 May 2011 16:05:33, RobG
I understood that requirement as: don't simply compare years, e.g.
2011 - 2008, but include the month and date in the comparison. If that
is correct, then the solutions offered meet the criterion. The OP has
not updated or clarified the requirement, the point is moot.

The OP wrote "date", dates", "today", but not "time"; we are entitled to
suppose that the time of day should not be taken into account.

The ambiguities are whether a date exactly three years ago is to be
treated the same as one more recent or as one older (he did put
"within", but may not mean it); and what should happen with dates at the
ends of some Februarys.

Unless, of course, the OP is Israeli.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
Dr said:
Thomas said:
The quoted solutions all fail to address the issue. The OP has made it
clear from the beginning that it does _not_ mean for them to simply
substract 3 from the year value. Even more wrong then, if there is such
a condition, is resetting the time to zero hours here (_Bad_ idea!).

The question is about dates, not about instants. […]

No, it is not. It is about "within (the) last three years" (Subject) or
"within 3 years from todays date". It is entirely possible to have a ^^^^
!!!!

definition of "within (the) last three years" that includes the time of day.
Indeed, the emphasis on the words "exactly" and "full" in the OP rather
suggests that the time of day has to be considered here.
Of course, it's commonly rash to assume that the originator has got the
question right!

Granted, the wording in the OP is confusing, if not contradictory:

You are just not very good at reading English as it is commonly written
by native speakers.
 
T

Thomas 'PointedEars' Lahn

Dr said:
Thomas 'PointedEars' Lahn posted:
Dr said:
Thomas 'PointedEars' Lahn wrote:
The quoted solutions all fail to address the issue. The OP has made it
clear from the beginning that it does _not_ mean for them to simply
substract 3 from the year value. Even more wrong then, if there is
such a condition, is resetting the time to zero hours here (_Bad_
idea!).

The question is about dates, not about instants. […]

No, it is not. It is about "within (the) last three years" (Subject) or
"within 3 years from todays date". It is entirely possible to have a
^^^^
!!!!

Your Exclamation Mark key is borken.
You are just not very good at reading English as it is commonly written
by native speakers.

Yes, that must be the reason why the ECMAScript Date object includes methods
to retrieve the time of day, and date values include the time of day in
several programming languages :->


PointedEars
 
M

Mike Duffy

Yes, that must be the reason why the ECMAScript Date object
includes methods to retrieve the time of day, and date values
include the time of day in several programming languages :->

Just to be pedantic:

"Year" might be solar, anomalistic, sidereal, tropical, draconic,
julian, calendar, etc.

"Calendar" in the above sentence might be gregorian, chinese, jewish,
islamic, mesoamerican, etc.

And it looks to me like Thomas' modern continental European version
of English actually gives him an advantage sometimes over the insular
dialects spoken in the UK.

Notwithstanding your command of the language, Dr. Stockton, I find
that the average European outside of the UK has a much better command
of English than the average so-called native English speaker, because
of the gross deterioration in our(*) educational systems and the
rampant abbreviation amongst youngsters used to facilitate texting.

(*) I judge the UK and America to be in the same sorry condition.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top