473,503 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help me on this script


Hi,

I need to write a Javascript which calculates datedifference as well
as date comparision.When i try to embed the below script,it is working
well,but the page is submitting even when the condition evaluates to
false .Please let me know the reason what is the problem in the below
script.
function ValidateDate()
{

var fromDate=new
Date(document.getElementById("txtFromFiledDate").v alue);
var toDate=new
Date(document.getElementById("txtToFiledDate").val ue);
var diffDate=new Date();
diffDate = (toDate - fromDate);
diffDate = ((((diffDate / 1000) / 60) / 60) / 24);
alert("Difference is: " + diffDate);
alert(diffDate>30);
if(diffDate<0){alert("Start Date cannot be after End Date!")
return false;}
if(diffDate>30 && diffDate<0) {alert("Please enter the date in
Range")
return false;}
return true;
}
Thanks,
Vishnu

Oct 26 '06 #1
22 2233
se****@gmail.com wrote:
>
Hi,

I need to write a Javascript which calculates datedifference as well
as date comparision.When i try to embed the below script,it is working
well,but the page is submitting even when the condition evaluates to
false .Please let me know the reason what is the problem in the below
script.
function ValidateDate()
{

var fromDate=new
Date(document.getElementById("txtFromFiledDate").v alue);
var toDate=new
Date(document.getElementById("txtToFiledDate").val ue);
var diffDate=new Date();
diffDate = (toDate - fromDate);
diffDate = ((((diffDate / 1000) / 60) / 60) / 24);
alert("Difference is: " + diffDate);
alert(diffDate>30);
if(diffDate<0){alert("Start Date cannot be after End Date!")
return false;}
if(diffDate>30 && diffDate<0) {alert("Please enter the date in
Range")
return false;}
return true;
}
Thanks,
Vishnu
Hi,

How does your formtag look?
It should look like this:
<form action="bla" onSubmit="return ValidateDate();">

mind the return before the functioncall.
If onSubmit returns false, the form is not submitted.
A common mistake is to forget the return (speaking from own experience.)

Regards,
Erwin Moller
Oct 26 '06 #2
Hi Erwin,

Thanks for your lightening response.As you said,i forgot to specify
return statement while calling the function.

Thanks,
Vishnu

Erwin Moller wrote:
se****@gmail.com wrote:

Hi,

I need to write a Javascript which calculates datedifference as well
as date comparision.When i try to embed the below script,it is working
well,but the page is submitting even when the condition evaluates to
false .Please let me know the reason what is the problem in the below
script.
function ValidateDate()
{

var fromDate=new
Date(document.getElementById("txtFromFiledDate").v alue);
var toDate=new
Date(document.getElementById("txtToFiledDate").val ue);
var diffDate=new Date();
diffDate = (toDate - fromDate);
diffDate = ((((diffDate / 1000) / 60) / 60) / 24);
alert("Difference is: " + diffDate);
alert(diffDate>30);
if(diffDate<0){alert("Start Date cannot be after End Date!")
return false;}
if(diffDate>30 && diffDate<0) {alert("Please enter the date in
Range")
return false;}
return true;
}
Thanks,
Vishnu

Hi,

How does your formtag look?
It should look like this:
<form action="bla" onSubmit="return ValidateDate();">

mind the return before the functioncall.
If onSubmit returns false, the form is not submitted.
A common mistake is to forget the return (speaking from own experience.)

Regards,
Erwin Moller
Oct 26 '06 #3
Hi,

Now am facing another issue.It is telling script error "return
statement outside the function"
Please let me know why am getting this error.

Thanks,
Vishnu
se****@gmail.com wrote:
Hi Erwin,

Thanks for your lightening response.As you said,i forgot to specify
return statement while calling the function.

Thanks,
Vishnu

Erwin Moller wrote:
se****@gmail.com wrote:
>
Hi,
>
I need to write a Javascript which calculates datedifference as well
as date comparision.When i try to embed the below script,it is working
well,but the page is submitting even when the condition evaluates to
false .Please let me know the reason what is the problem in the below
script.
>
>
function ValidateDate()
{
>
var fromDate=new
Date(document.getElementById("txtFromFiledDate").v alue);
var toDate=new
Date(document.getElementById("txtToFiledDate").val ue);
var diffDate=new Date();
diffDate = (toDate - fromDate);
diffDate = ((((diffDate / 1000) / 60) / 60) / 24);
alert("Difference is: " + diffDate);
alert(diffDate>30);
if(diffDate<0){alert("Start Date cannot be after End Date!")
return false;}
if(diffDate>30 && diffDate<0) {alert("Please enter the date in
Range")
return false;}
return true;
}
>
>
Thanks,
Vishnu
Hi,

How does your formtag look?
It should look like this:
<form action="bla" onSubmit="return ValidateDate();">

mind the return before the functioncall.
If onSubmit returns false, the form is not submitted.
A common mistake is to forget the return (speaking from own experience.)

Regards,
Erwin Moller
Oct 26 '06 #4
wrote on 26 okt 2006 in comp.lang.javascript:
I need to write a Javascript
"need" = school work? Let's hope not!
which calculates datedifference as well
as date comparision.When i try to embed the below script,it is working
well,but the page is submitting
submitting?

You do not show the code.
even when the condition evaluates to
false .Please let me know the reason what is the problem in the below
script.
function ValidateDate()
{

[..]
if(diffDate>30 && diffDate<0)
A value can NEVER be both more than 30 and less than zero ;-{
[..]
Do not read on, if this is a school assignment,
you will never learn, if you do not try to program for yourself.

================================================
<script type='text/javascript'>

function ValidateDate( f ) {

var d1 = new Date( f.elements['fromDate'].value );
var d2 = new Date( f.elements['toDate'].value );

// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

// alert( diffDate ); // debugger

if ( diffDate < 0 ) {
alert( 'The start date cannot be after the end date!")
return false;
}

if ( diffDate 30 ) {
alert( 'Please enter dates less than 31 days apart' )
return false;
}

return true; // allow submit
}

</script>
<form onsubmit='return ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'start<br>
<input name='toDate' value='2006/12/1'end<br>
<input type='submit'>
</form>
=================================================
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 26 '06 #5
se****@gmail.com wrote:
I need to write a Javascript which calculates datedifference as well
as date comparision.
Is this for a homework assignment or something?

If not, why not just use a lib which already does a lot of the work for you:
http://www.javascripttoolbox.com/lib/date/

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Oct 26 '06 #6

Hi,

function ValidateDate()
{

var fromDate=new
Date(document.getElementById("txtFromFiledDate").v alue);
var toDate=new
Date(document.getElementById("txtToFiledDate").val ue);
var diffDate = Math.round( ( toDate-fromDate ) / 864e5 );

if ( diffDate < 0 ) {
alert( "The start date cannot be after the end date!")
return false;
}
if ( diffDate 30 ) {
alert( "Please enter dates less than 31 days apart" )
return false;
}
return true;

}

I am trying to use this javascript for client-side validation in my
ASPX page using customvalidator control as below:

<asp:CustomValidator id="CustomValidator1" runat="server"
ClientValidationFunction="return
ValidateDate();"></asp:CustomValidator>

Now the problem with the function is that the page is submitting when
datediff<0 .it should not submit.Also alert msg is coming twice and
submitting the page.

Please let me know what is the reason for this.

Thanks,
Vishnu

i have a search button on my screen.When i enter the dates and click it
has to validate.

Matt Kruse wrote:
se****@gmail.com wrote:
I need to write a Javascript which calculates datedifference as well
as date comparision.

Is this for a homework assignment or something?

If not, why not just use a lib which already does a lot of the work for you:
http://www.javascripttoolbox.com/lib/date/

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Oct 26 '06 #7
Hi,

How do i write the same function with parameters? Please help me.

Thanks,
Vishnu

Evertjan. wrote:
wrote on 26 okt 2006 in comp.lang.javascript:
I need to write a Javascript

"need" = school work? Let's hope not!
which calculates datedifference as well
as date comparision.When i try to embed the below script,it is working
well,but the page is submitting

submitting?

You do not show the code.
even when the condition evaluates to
false .Please let me know the reason what is the problem in the below
script.
function ValidateDate()
{

[..]
if(diffDate>30 && diffDate<0)

A value can NEVER be both more than 30 and less than zero ;-{
[..]

Do not read on, if this is a school assignment,
you will never learn, if you do not try to program for yourself.

================================================
<script type='text/javascript'>

function ValidateDate( f ) {

var d1 = new Date( f.elements['fromDate'].value );
var d2 = new Date( f.elements['toDate'].value );

// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

// alert( diffDate ); // debugger

if ( diffDate < 0 ) {
alert( 'The start date cannot be after the end date!")
return false;
}

if ( diffDate 30 ) {
alert( 'Please enter dates less than 31 days apart' )
return false;
}

return true; // allow submit
}

</script>
<form onsubmit='return ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'start<br>
<input name='toDate' value='2006/12/1'end<br>
<input type='submit'>
</form>
=================================================
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 26 '06 #8
Hi,

How do i write the same function with parameters? Please help me.

Thanks,
Vishnu

Evertjan. wrote:
wrote on 26 okt 2006 in comp.lang.javascript:
I need to write a Javascript

"need" = school work? Let's hope not!
which calculates datedifference as well
as date comparision.When i try to embed the below script,it is working
well,but the page is submitting

submitting?

You do not show the code.
even when the condition evaluates to
false .Please let me know the reason what is the problem in the below
script.
function ValidateDate()
{

[..]
if(diffDate>30 && diffDate<0)

A value can NEVER be both more than 30 and less than zero ;-{
[..]

Do not read on, if this is a school assignment,
you will never learn, if you do not try to program for yourself.

================================================
<script type='text/javascript'>

function ValidateDate( f ) {

var d1 = new Date( f.elements['fromDate'].value );
var d2 = new Date( f.elements['toDate'].value );

// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

// alert( diffDate ); // debugger

if ( diffDate < 0 ) {
alert( 'The start date cannot be after the end date!")
return false;
}

if ( diffDate 30 ) {
alert( 'Please enter dates less than 31 days apart' )
return false;
}

return true; // allow submit
}

</script>
<form onsubmit='return ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'start<br>
<input name='toDate' value='2006/12/1'end<br>
<input type='submit'>
</form>
=================================================
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 26 '06 #9
wrote on 26 okt 2006 in comp.lang.javascript:
>>
=============================================== =
<script type='text/javascript'>

function ValidateDate( f ) {

var d1 = new Date( f.elements['fromDate'].value );
var d2 = new Date( f.elements['toDate'].value );

// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

// alert( diffDate ); // debugger

if ( diffDate < 0 ) {
alert( 'The start date cannot be after the end date!")
return false;
}

if ( diffDate 30 ) {
alert( 'Please enter dates less than 31 days apart' )
return false;
}

return true; // allow submit
}

</script>
<form onsubmit='return ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'start<br>
<input name='toDate' value='2006/12/1'end<br>
<input type='submit'>
</form>
=============================================== ==
[Please do not toppost on usenet, use sparse interquote]
How do i write the same function with parameters? Please help me.
Is the (f) not parameter enough?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 26 '06 #10
I want to pass fromdate and toDate as parameters in Javascript function
from my aspx page.

Evertjan. wrote:
wrote on 26 okt 2006 in comp.lang.javascript:
>
================================================
<script type='text/javascript'>

function ValidateDate( f ) {

var d1 = new Date( f.elements['fromDate'].value );
var d2 = new Date( f.elements['toDate'].value );

// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

// alert( diffDate ); // debugger

if ( diffDate < 0 ) {
alert( 'The start date cannot be after the end date!")
return false;
}

if ( diffDate 30 ) {
alert( 'Please enter dates less than 31 days apart' )
return false;
}

return true; // allow submit
}

</script>
<form onsubmit='return ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'start<br>
<input name='toDate' value='2006/12/1'end<br>
<input type='submit'>
</form>
=================================================

[Please do not toppost on usenet, use sparse interquote]
How do i write the same function with parameters? Please help me.

Is the (f) not parameter enough?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 26 '06 #11
I want to pass fromdate and toDate as parameters in Javascript function
from my aspx page.

Evertjan. wrote:
wrote on 26 okt 2006 in comp.lang.javascript:
>
================================================
<script type='text/javascript'>

function ValidateDate( f ) {

var d1 = new Date( f.elements['fromDate'].value );
var d2 = new Date( f.elements['toDate'].value );

// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

// alert( diffDate ); // debugger

if ( diffDate < 0 ) {
alert( 'The start date cannot be after the end date!")
return false;
}

if ( diffDate 30 ) {
alert( 'Please enter dates less than 31 days apart' )
return false;
}

return true; // allow submit
}

</script>
<form onsubmit='return ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'start<br>
<input name='toDate' value='2006/12/1'end<br>
<input type='submit'>
</form>
=================================================

[Please do not toppost on usenet, use sparse interquote]
How do i write the same function with parameters? Please help me.

Is the (f) not parameter enough?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 26 '06 #12
I want to pass fromdate and toDate as parameters in Javascript function
from my aspx page.

Evertjan. wrote:
wrote on 26 okt 2006 in comp.lang.javascript:
>
================================================
<script type='text/javascript'>

function ValidateDate( f ) {

var d1 = new Date( f.elements['fromDate'].value );
var d2 = new Date( f.elements['toDate'].value );

// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

// alert( diffDate ); // debugger

if ( diffDate < 0 ) {
alert( 'The start date cannot be after the end date!")
return false;
}

if ( diffDate 30 ) {
alert( 'Please enter dates less than 31 days apart' )
return false;
}

return true; // allow submit
}

</script>
<form onsubmit='return ValidateDate( this );'>
<input name='fromDate' value='2006/1/1'start<br>
<input name='toDate' value='2006/12/1'end<br>
<input type='submit'>
</form>
=================================================

[Please do not toppost on usenet, use sparse interquote]
How do i write the same function with parameters? Please help me.

Is the (f) not parameter enough?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 26 '06 #13
wrote on 26 okt 2006 in comp.lang.javascript:
I want to pass fromdate and toDate as parameters in Javascript
function from my aspx page.
Evertjan. wrote:
>[Please do not toppost on usenet, use sparse interquote]
1 Why do you think I wrote that??

2 So, please do not toppost

3 please do not post three times the same posting.

btw: Asp.net has it's own Newsgroups.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 26 '06 #14
In message <Xn********************@194.109.133.242>, Thu, 26 Oct 2006
15:33:26, Evertjan. <ex**************@interxnl.netwrites
>
// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );
Being named Vishnu, he may be in India where IIRC they need no Summer
Time. But, contrary to what your comment suggests, that method deals
with changes in season between d1 & d2.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Oct 26 '06 #15
Dr J R Stockton wrote on 26 okt 2006 in comp.lang.javascript:
In message <Xn********************@194.109.133.242>, Thu, 26 Oct 2006
15:33:26, Evertjan. <ex**************@interxnl.netwrites
>>
// below formula from John Stockton at
// <http://www.merlyn.demon.co.uk/js-date1.htm#DDf>
// no account is taken of summer/winter and other time differences.

var diffDate = Math.round( ( d2-d1 ) / 864e5 );

Being named Vishnu, he may be in India where IIRC they need no Summer
Time.
Vishnu is the Ultimate Reality and is responsible for the maintainance and
preservation of the universe, so he is surely would not be interested in
futilities like local time differences of one hour?
But, contrary to what your comment suggests, that method deals
with changes in season between d1 & d2.
Ah, yes,
because the rounding takes care of time zone differences
that are less than half a day.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 27 '06 #16
In message <Xn********************@194.109.133.242>, Fri, 27 Oct 2006
07:17:38, Evertjan. <ex**************@interxnl.netwrites
>Dr J R Stockton wrote on 26 okt 2006 in comp.lang.javascript:
>But, contrary to what your comment suggests, that method deals
with changes in season between d1 & d2.

Ah, yes,
because the rounding takes care of time zone differences
that are less than half a day.
No; the coding expects both dates to be in the same time zone. The
rounding allows for the summer/winter change of offset from UTC by
thirty minutes or by one hour (and, merely adventitiously, for larger
differences).

Time zones are geographically stable, changing only by specific
political action. Offsets from UTC often change twice a year.

Around 1970, the UK remained in the GMT zone but had Summer Time all
year round. In principle, that's quite different from moving to your
(NL) time zone and having Winter Time all year round. But they have the
same effect on the clocks.
--
(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.
Oct 27 '06 #17
Dr J R Stockton wrote on 27 okt 2006 in comp.lang.javascript:
Around 1970, the UK remained in the GMT zone but had Summer Time all
year round. In principle, that's quite different from moving to your
(NL) time zone and having Winter Time all year round. But they have the
same effect on the clocks.
And 25 years earlier Britain toggled between British Summer Time (BST) in
winter and British Double Summer Time (BDST) in summer for 4 years, as I
faguely remember.

At present the Netherlands, using CET, linked to the meridian of Berlin,
enjoys a winter time advance of about 20 minutes and a summer time advance
of 80 minutes, depending on location.

A same effect is enjoyed in Wales etc with WET.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 28 '06 #18
In message <Xn********************@194.109.133.242>, Sat, 28 Oct 2006
09:40:30, Evertjan. <ex**************@interxnl.netwrites
>
And 25 years earlier Britain toggled between British
Summer Time (BST) in winter and British Double Summer Time
(BDST) in summer for 4 years, as I faguely remember.
Double Summer Time (BDST/DBST) has been used in the UK, during 1941-45
and 1947; and between Summer 1940 and Summer 1945, civil time in the
Winter was an hour ahead of GMT. BDST is no longer allowed. See Myers
and NPL's archive, via
<URL:http://www.merlyn.demon.co.uk/uksumtim.htm#Linx>.
Returning towards topic a little : there was a pleasant discussion here
a while back about the fastest way to determine Leap Year - the result
did not use a Date Object. But what's the best way *with* a Date Object
?
--
(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.
Oct 28 '06 #19
Dr J R Stockton wrote on 28 okt 2006 in comp.lang.javascript:
Returning towards topic a little : there was a pleasant discussion here
a while back about the fastest way to determine Leap Year - the result
did not use a Date Object. But what's the best way *with* a Date Object
?
function isLeapyear(yr) {
return new Date(yr,1,29).getMonth()==1
}

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 29 '06 #20
In message <Xn********************@194.109.133.242>, Sun, 29 Oct 2006
09:06:55, Evertjan. <ex**************@interxnl.netwrites
>Dr J R Stockton wrote on 28 okt 2006 in comp.lang.javascript:
>Returning towards topic a little : there was a pleasant discussion here
a while back about the fastest way to determine Leap Year - the result
did not use a Date Object. But what's the best way *with* a Date Object
?

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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<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.
Oct 29 '06 #21
Dr J R Stockton wrote on 29 okt 2006 in comp.lang.javascript:
In message <Xn********************@194.109.133.242>, Sun, 29 Oct 2006
09:06:55, Evertjan. <ex**************@interxnl.netwrites
>>Dr J R Stockton wrote on 28 okt 2006 in comp.lang.javascript:
>>Returning towards topic a little : there was a pleasant discussion
here a while back about the fastest way to determine Leap Year - the
result did not use a Date Object. But what's the best way *with* a
Date Object ?

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 30 '06 #22
In message <Xn********************@194.109.133.242>, Mon, 30 Oct 2006
16:17:34, Evertjan. <ex**************@interxnl.netwrites
>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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<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.
Oct 30 '06 #23

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

Similar topics

3
2307
by: Lodewijk van Haringhal | last post by:
I'am new with javascritping not with programming. Is there nobody who can help me with ths simple promblem? :) Please, please give me a hint. Please help me with this script. I have two lists in...
4
2569
by: dave | last post by:
Hi guys I display one page in popup window...that fetches some data from sql and perfom some calculation (tht approx 10 secs) and display result.... I am trying to display "Please wait ..."message...
3
4611
by: John Dalberg | last post by:
Hi I have a form that opens a new window for the results. Because the results might take a few seconds due to server processing, I would like to display a message "please wait" in the new...
2
2343
by: rked | last post by:
I get nameSPAN1 is undefined when I place cursor in comments box.. <%@ LANGUAGE="VBScript" %> <% DIM ipAddress ipAddress=Request.Servervariables("REMOTE_HOST") %> <html> <head> <meta...
11
1894
by: milkyway | last post by:
Hello, I have an HTML page that I am trying to import 2 .js file (I created) into. These files are: row_functions.js and data_check_functions.js. Whenever I bring the contents of the files into...
1
1817
by: joe kenney | last post by:
I have a question on designing a please wait aspx page. Currently I have a test.aspx page with codebehind page and page_onload contains my stored procedure calls. I've create something similar in...
0
2269
by: Aws | last post by:
My crazy GridView !! I am using Visual Studio 2005, I have a problem with my GridView. I have one access .mdb table and when I update a record on the table EVERYTHING is perfect. I made a Web...
2
6012
by: bigpoppa | last post by:
Hey I need help (doesn't everyone?) on a script. The script functions like this: Please wait X seconds for download (for example x=15 seconds) Please wait 14 seconds for download (and then a second...
7
7551
by: adamalton | last post by:
Hi, I'm building a site where the user enters some info into a form which submits to a php script. The php script takes a long time to execute (anywhere from 10 seconds to a minute) and when it...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.