Subtract a Day to Date Object Using 'myDate - 1'

D

dan

Am i breaking any rules when I loop dates like

// Determine Memorial Day
intFlag = 0;
memDayHol = new Date (currentYear, 4, 31);
while (intFlag == 0) {
if (memDayHol.getDay() == 1) {intFlag =1;}
else {memDayHol = memDayHol - 1;}
}

I can find no docs that one can use the '+' operator directly on a date
object, and it assumes adding/subtracting a day(s). Is this deprecated
code?

TIA,
Dan
 
N

noagbodjivictor

Am i breaking any rules when I loop dates like

// Determine Memorial Day
intFlag = 0;
memDayHol = new Date (currentYear, 4, 31);
while (intFlag == 0) {
if (memDayHol.getDay() == 1) {intFlag =1;}
else {memDayHol = memDayHol - 1;}

}

I can find no docs that one can use the '+' operator directly on a date
object, and it assumes adding/subtracting a day(s). Is this deprecated
code?

TIA,
Dan

Is it working?
 
L

Lee

(e-mail address removed) said:
Am i breaking any rules when I loop dates like

// Determine Memorial Day
intFlag = 0;
memDayHol = new Date (currentYear, 4, 31);
while (intFlag == 0) {
if (memDayHol.getDay() == 1) {intFlag =1;}
else {memDayHol = memDayHol - 1;}
}

I can find no docs that one can use the '+' operator directly on a date
object, and it assumes adding/subtracting a day(s). Is this deprecated
code?

It certainly seems safer to use:
memDayHol.setDate(memDayHol.getDate()-1);

but the whole algorithm is nonsense.
If you want the date of the Monday before (or on) May 31, you
can calculate that without looping:

memDayHol = new Date (currentYear, 4, 31);
memDayHol.setDate(memDayHol.getDate()-memDayHol.getDay()+1);


--
 
D

dan

Lee said:
(e-mail address removed) said:

It certainly seems safer to use:
memDayHol.setDate(memDayHol.getDate()-1);

but the whole algorithm is nonsense.
If you want the date of the Monday before (or on) May 31, you
can calculate that without looping:

memDayHol = new Date (currentYear, 4, 31);
memDayHol.setDate(memDayHol.getDate()-memDayHol.getDay()+1);

Assume Memorial Day is 5/31. Then your code will set memorial day to
5/29. I think you want

memDayHol = new Date (currentYear, 4, 31);
memDayHol.setDate(memDayHol.getDate()- (memDayHol.getDay() - 1));

Plus my code is not nonsense as it correctly calculates the Memorial day.

Dan
 
L

Lee

(e-mail address removed) said:
Assume Memorial Day is 5/31. Then your code will set memorial day to
5/29. I think you want

memDayHol = new Date (currentYear, 4, 31);
memDayHol.setDate(memDayHol.getDate()- (memDayHol.getDay() - 1));

Plus my code is not nonsense as it correctly calculates the Memorial day.

You should learn to test. My code returns May 28.
You should also learn some mathematics:

a - b + 1 = a - ( b - 1 )

Looping to search for an answer that can be calculated is
nonsense, whether or not it produces the correct result.


--
 
D

dan

Lee said:
(e-mail address removed) said:

You should learn to test. My code returns May 28.
You should also learn some mathematics:

a - b + 1 = a - ( b - 1 )

Looping to search for an answer that can be calculated is
nonsense, whether or not it produces the correct result.

Oops I thought I saw 'a - (b + 1)'. My mistake. But back to my
original question, is the '+' an undocumented way of doing day
incrementing/decrementing?
 
D

Dr J R Stockton

Gives June 1 in 2009 & 2015.


Gives Saturdays.


The original code, as posted, fails to run.

Oops I thought I saw 'a - (b + 1)'. My mistake. But back to my
original question, is the '+' an undocumented way of doing day
incrementing/decrementing?

It is undocumented because assigning a number to a variable "holding" a
Date Object disconnects the Date Object and makes the variable hold a
Number.


AIUI, Memorial Day is the last Monday in May in America - elsewhere, it
may be different.

Such code should, if it is required to run in all years, be tested in
all types of year. For ordinary dates, that's 14 types - Leap or not,
starting on Mon-Sun. All types necessarily occur in any 28-year stretch
lacking a missing Leap Year.

So test in this sort of manner :-

for (Y=2000 ; Y<2030; Y++) {
memDayHol = new Date (Y, 4, 31);
memDayHol.setDate(memDayHol.getDate()-memDayHol.getDay()+1);
document.writeln(memDayHol, "<br>") }


The easiest way to get the last X-day of a month is to think of it as
the Zeroth X-day of the following month. There is then no need to know
the length of any month.

Reading the FAQ can get you to

function NthXdayOfYM(N, X, Y, M) { var D
// X = Sun=0..Sat=6 Sun=7.. , M=1..12, N=0 for last of prev mth.
with (D = new Date(Y, M-1, 1))
{ setDate(7*N - 6 + (7+X-getDay())%7 ) }
return D }

and test with

for (Y=2000 ; Y<2030; Y++) {
document.writeln(NthXdayOfYM(0, 1, Y, 6), "<br>") }

shows Mondays in May 25-31 as needed.

I rather believe, without remembering proving it, that such jobs always
require a conditional or a %7.

Does any nation have a Special Day on the last, last but one, etc.,
X-day of February each year?

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
L

Lee

(e-mail address removed) said:
Oops I thought I saw 'a - (b + 1)'. My mistake. But back to my
original question, is the '+' an undocumented way of doing day
incrementing/decrementing?

An undocumented method is not likely to be supported by all
browsers, or even in future versions of browsers that currently
support it, so your question becomes "Does this work in some
versions of some browsers?" and that question doesn't seem to
have much value.

In both browsers that I've tried, subtracting 1 from a Date
object works as I would expect, which is not what you're
looking for. The result of subtracting 1 from
"new Date (2007, 4, 31)" is a Number, not a date: 1180594799999

A quick test is to type this as an URL in your browser:

javascript:alert((new Date())-1)

If the result is a Date object, you should see the month day
year and time of day displayed in your Locale format, instead
of the number of msec since the epoch.


--
 
D

dan

Dr said:
Gives June 1 in 2009 & 2015.



Gives Saturdays.



The original code, as posted, fails to run.



It is undocumented because assigning a number to a variable "holding" a
Date Object disconnects the Date Object and makes the variable hold a
Number.


AIUI, Memorial Day is the last Monday in May in America - elsewhere, it
may be different.

Such code should, if it is required to run in all years, be tested in
all types of year. For ordinary dates, that's 14 types - Leap or not,
starting on Mon-Sun. All types necessarily occur in any 28-year stretch
lacking a missing Leap Year.

So test in this sort of manner :-

for (Y=2000 ; Y<2030; Y++) {
memDayHol = new Date (Y, 4, 31);
memDayHol.setDate(memDayHol.getDate()-memDayHol.getDay()+1);
document.writeln(memDayHol, "<br>") }


The easiest way to get the last X-day of a month is to think of it as
the Zeroth X-day of the following month. There is then no need to know
the length of any month.

Reading the FAQ can get you to

function NthXdayOfYM(N, X, Y, M) { var D
// X = Sun=0..Sat=6 Sun=7.. , M=1..12, N=0 for last of prev mth.
with (D = new Date(Y, M-1, 1))
{ setDate(7*N - 6 + (7+X-getDay())%7 ) }
return D }

and test with

for (Y=2000 ; Y<2030; Y++) {
document.writeln(NthXdayOfYM(0, 1, Y, 6), "<br>") }

shows Mondays in May 25-31 as needed.

I rather believe, without remembering proving it, that such jobs always
require a conditional or a %7.

Does any nation have a Special Day on the last, last but one, etc.,
X-day of February each year?

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

I was going to point out that May 31 1998 => June 1st, but I had to take
off yesterday.

Just to fill everyone in the javascript code I originally posted is
running within a Hyperion Reporting system in which it works. But
apparently this is undocumented, and so I will change my 'subtract day'
code to the documented way in case the code is moved to another system
in future.

I prefer my looping structure as this not a "PRO" shop, and others need
to be able to easily understand what is happening, and we will be
running this code only in the USA (my famous last words...). And this
isn't controlling rocket ship and so on. But thanks for the interesting
code example.
 
D

Dr J R Stockton

Tue said:
In both browsers that I've tried, subtracting 1 from a Date
object works as I would expect, which is not what you're
looking for. The result of subtracting 1 from
"new Date (2007, 4, 31)" is a Number, not a date: 1180594799999

No, it is 1180565999999 here.

Evidently you are 7 hours away from the Real Time, probably -8+1.
 

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

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top