Please help me on this script

D

Dr J R Stockton

Sun said:
Dr J R Stockton wrote on 28 okt 2006 in comp.lang.javascript:


function isLeapyear(yr) {
return new Date(yr,1,29).getMonth()==1
}

function isLeapyear(yr) { return new Date(yr, 2, -28).getMonth() }

returns 0 or 1, which can be used directly in a context demanding
Boolean, can be converted to true Boolean with ! or !!, and can be used
to index an array. No comparison operation required. Speed is about
doubled (for me) by using UTC.
 
E

Evertjan.

Dr J R Stockton wrote on 29 okt 2006 in comp.lang.javascript:
function isLeapyear(yr) { return new Date(yr, 2, -28).getMonth() }

returns 0 or 1, which can be used directly in a context demanding
Boolean, can be converted to true Boolean with ! or !!, and can be
used to index an array. No comparison operation required.

function isLeapyear(yr) { return !new Date(yr, 2, -59).getMonth() }

will give return true boolean value.

As will:

function isLeapyear(yr) { return !new Date(yr, 0, 397).getMonth() }

Or if you wish:

function isLeapyear(yr) { return !!new Date(yr, 0, 366).getMonth() }
Speed is about doubled (for me) by using UTC.

How many leapyear tests would you like to do in a real life script?

;-}

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

new Date(yr, 0, d) is a quick way
to get the Date Object date from the year day number.

Is there a similar quick way
to get the year day number from a Date Object date?
 
D

Dr J R Stockton

Mon said:
new Date(yr, 0, d) is a quick way
to get the Date Object date from the year day number.

Is there a similar quick way
to get the year day number from a Date Object date?


function DoY(X) { var D
return Math.round((+(D = new Date(+X)) - D.setMonth(0, 0))/864e5) }

That can be simplified if the object's value need not be preserved ...

return Math.round((+X - X.setMonth(0, 0))/864e5) }

Those functions should give Local Day of Year; for UTC Day of Year, use
UTC setMonth. South of the Equator, one can use |0 instead of
Math.round.

I wrote those while answering; see also
<URL:http://www.merlyn.demon.co.uk/js-date0.htm#DoY> which includes
much the same but less compactly.
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top