473,382 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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
May 29 '07 #1
10 12409
On May 29, 10:55 am, d...@nospam.com wrote:
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?

May 29 '07 #2
Lee
da*@nospam.com 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);
--

May 29 '07 #3
dan
no*************@gmail.com wrote:
On May 29, 10:55 am, d...@nospam.com wrote:
>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?
Yes.
May 29 '07 #4
dan
Lee wrote:
da*@nospam.com 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);

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
May 29 '07 #5
Lee
da*@nospam.com said:
>
Lee wrote:
>da*@nospam.com 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);


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

May 29 '07 #6
dan
Lee wrote:
da*@nospam.com said:
>Lee wrote:
>>da*@nospam.com 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);

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.
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?
May 29 '07 #7
Lee
da*@nospam.com said:
>
Lee wrote:
>da*@nospam.com said:
>>Lee wrote:
da*@nospam.com 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);
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.

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

May 29 '07 #8
In comp.lang.javascript message <46*********************@news.qwest.net>
, Tue, 29 May 2007 12:16:57, da*@nospam.com posted:
>Lee wrote:
>da*@nospam.com said:
>>Lee wrote:
da*@nospam.com 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);
Gives June 1 in 2009 & 2015.

>>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));
Gives Saturdays.

>>Plus my code is not nonsense as it correctly calculates the Memorial day.
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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
May 29 '07 #9
dan
Dr J R Stockton wrote:
In comp.lang.javascript message <46*********************@news.qwest.net>
, Tue, 29 May 2007 12:16:57, da*@nospam.com posted:
>Lee wrote:
>>da*@nospam.com said:
Lee wrote:
da*@nospam.com 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);

Gives June 1 in 2009 & 2015.

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

Gives Saturdays.

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

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.
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.
May 30 '07 #10
In comp.lang.javascript message <f3*********@drn.newsguy.com>, Tue, 29
May 2007 12:43:23, Lee <RE**************@cox.netposted:
>
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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
May 30 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ian | last post by:
I would like to have some validation on a date field. The date format is dd/mm which is used for our financial year end. I suppose I need also consider leap years. Please can you shed some light on...
3
by: Andreas Klemt | last post by:
Hello, what is the difference and what is better (performance etc) ? a) If myDate.Equals(Date.MinValue) b) If myDate = Date.MinValue Thanks, Andreas
4
by: David Doing | last post by:
This worked in VB6, but does not in VB.Net Format(MyDate - DateSerial(Year(MyDate) - 1, 12, 31), "000") Where: MyDate is type Date and = #11/16/2003# It should return the Julian Day # -->...
3
by: Yannick | last post by:
Hi, I try to execute request on a ms-access database but I have a problem with date. the "myDate" field's format is "date/time" my request is: SELECT myCode, myStuff, myDATE FROM myTable ...
18
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
13
by: ahjiang | last post by:
Hi all, Need some help here. How do i create a javascript date object with the format (yyyy:mm:dd hh:mm:ss) do i need to parse it to achieve that? appreciate any inputs
2
by: bryan.a.fowler | last post by:
I'm really new working with VB.net and I'm working on a simple program with a date input. I've got a couple of questions; 1) I'm using a DateTimePicker to show both the user inputted date as...
5
by: mabond | last post by:
Hi Having trouble filling a datetime filed in SQL table with a value from an array. The original source is a comma-delimited text file where the date and time values are in two columns. I...
2
by: syntego | last post by:
We commonly use triggers to log changes to our main tables to historical log tables. In the trigger, we create a concatenated string of the old values by casting them as follows: ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.