Dates - am I going mad?

Y

yukabuk

Please look at the following code and run it.

<script type='text/javascript'>
var firstDate=new Date();
var tday=31;
var tmonth=4;
var tyear=2010;

firstDate.setDate(tday);
firstDate.setMonth(tmonth);
firstDate.setFullYear(tyear);

alert("First "+ tday + "/"+tmonth + "/"+tyear + " - " +
firstDate); //should this not say 31st and not the 1st?

var secondDate=new Date();
var tday=30;
var tmonth=4;
var tyear=2010;

secondDate.setDate(tday);
secondDate.setMonth(tmonth);
secondDate.setFullYear(tyear);

alert("Second "+ tday + "/"+tmonth + "/"+tyear + " - " +
secondDate);

</script>

Should the First date say 31st or May and not the 1st?
Am I missing something obvious?

Thanks
Graham Vincent
 
R

Richard Cornford

Please look at the following code and run it.

<script type='text/javascript'>
var firstDate=new Date();

The date object that results from this has today's date, which is 23rd
June 2010.
var tday=31;
var tmonth=4;
var tyear=2010;

firstDate.setDate(tday);

You have set the day of the month of a date object that already has
the date 23rd June 2010 to 31. June does not have 31 days so the month
is incremented. If you looked at the date at this point it would be
1st July 2010.
firstDate.setMonth(tmonth);

Now you set the month of a date object that has the date of 1st July
2010, so its date is now 1st May 2010.
firstDate.setFullYear(tyear);

alert("First "+ tday + "/"+tmonth + "/"+tyear + " - " +
firstDate); //should this not say 31st and not the 1st?

Not according to the specification and its interactions with the code
you have written.
var secondDate=new Date();
var tday=30;
var tmonth=4;
var tyear=2010;

secondDate.setDate(tday);
secondDate.setMonth(tmonth);
secondDate.setFullYear(tyear);

alert("Second "+ tday + "/"+tmonth + "/"+tyear + " - " +
secondDate);

</script>

Should the First date say 31st or May and not the 1st?

31st May.
Am I missing something obvious?

Yes, the order in which you are doing things is significant because
setting a date that is out of the range for the current month will
cause the month (and possibly the year) to be modified. It may be
better to start with a 'zeroed' date (as that is 1st Jan 1970, and
January has 31 days) or assign values in a years, months, date order
(having verified that the date is in the acceptable range for the
month).

Richard.
 
Y

yukabuk

Richard, that was a very complete, concise and speedy reply.

10 points to you, and thank you.

Graham
 
D

Dr J R Stockton

In comp.lang.javascript message <5635483f-d4f2-4378-bb03-3ca8382e0e21@w3
1g2000yqb.googlegroups.com>, Wed, 23 Jun 2010 06:21:32, Richard Cornford
On Jun 23, 2:09 pm, yukabuk wrote:

Reading the standard, or any non-appalling relevant book.
Yes, the order in which you are doing things is significant because
setting a date that is out of the range for the current month will
cause the month (and possibly the year) to be modified. It may be
better to start with a 'zeroed' date (as that is 1st Jan 1970, and
January has 31 days) or assign values in a years, months, date order
(having verified that the date is in the acceptable range for the
month).

You have abstained from saying that an object with today's date (June)
can be set to the end of May as shown by
D = new Date()
D.setFullYear(2010, 4, 31)

( To check whether the date generated is valid, read
back the month. and see if it is 4. That assumes the
date entered to be (roughly) in the range -333 to +333. )

You have abstained from saying that the code would be better written as
shown by
D = new Date(2010, 4, 31)
showing that there is no need expensively to create an Object with
today's value and then throw away that value.

If this is a calendar-type application not needing the real
current time, then it will run faster with UTC functions
rather than local-time ones.

OP : see sig.
And, to get the last day of last month, set the zeroth of this month.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top