473,414 Members | 1,775 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,414 software developers and data experts.

Wrong date?

Esa
function Validaattori_1viikko(source,args)
{
var star_tdate;
var end_date;
start_date = document.all("Laina_alku").value;
end_date = document.all("Laina_loppu").value;
var start_day;
var end_day;
var start_month;
var end_month;
var start_year;
var end_year;
start_day = start_date.substring(0,2);
end_day = end_date.substring(0,2);
start_month = start_date.substring(3,5);
end_month = end_date.substring(3,5);
start_year = start_date.substring(6,10);
start_year = end_date.substring(6,10);
var startingdate = new Date();
startingdate.setMonth(parseInt(start_month)-1,parseInt(start_day));
startingdate.setYear(parseInt(start_year));

var endingdate = new Date();
date_loppu.setMonth(parseInt(end_month)-1,parseInt(end_day));
date_loppu.setYear(parseInt(end_year));

var dif_indays = Math.round( (endingdate.getTime() -
startingdate.getTime()) / (1000 * 60 * 60 * 24) );

// For debug
document.writeln(start_date + " " + end_date);
document.writeln(startingdate + " " + endingdate);

if ( (erotus_paivia < 7) && (erotus_paivia > 0) )
args.IsValid = true;
else
args.IsValid = false;
}

So Im using this code with ASP.net pages as clientvalidation function.
I don't think that has anything to do with the actual problem though.
So if i pick start_date of lets say "29.04.2004" and end_date
"06.05.2004" I will get this as debug info:
29.04.2004 06.05.2004 Thu Apr 29 12:54:05 UTC+0300 2004 Thu May 6
12:54:05 UTC+0300 2004

Everything seems to be working just fine, but...

If i pick end_date of "08.04.2004" (or "09.05.2004") i get this:
29.04.2004 08.05.2004 Thu Apr 29 12:55:42 UTC+0300 2004 Fri Apr 30
12:55:42 UTC+0300 2004

End date is completely wrong.

Then again if i pick "10.05.2004" i get:
..04.2004 10.05.2004 Thu Apr 29 12:56:36 UTC+0300 2004 Mon May 10
12:56:36 UTC+0300 2004

So those 2 dates 8 and 9.5 are giving all wrong dates and Im all out
of answers. Haven't found that error with any other dates yet.
Jul 23 '05 #1
7 2147
Esa wrote:
function Validaattori_1viikko(source,args)
{
var star_tdate;
var end_date;
start_date = document.all("Laina_alku").value;
end_date = document.all("Laina_loppu").value;
var start_day;
var end_day;
var start_month;
var end_month;
var start_year;
var end_year;
start_day = start_date.substring(0,2);
end_day = end_date.substring(0,2);
start_month = start_date.substring(3,5);
end_month = end_date.substring(3,5);
start_year = start_date.substring(6,10);
start_year = end_date.substring(6,10);
var startingdate = new Date();
startingdate.setMonth(parseInt(start_month)-1,parseInt(start_day));
startingdate.setYear(parseInt(start_year));

var endingdate = new Date();
date_loppu.setMonth(parseInt(end_month)-1,parseInt(end_day));
date_loppu.setYear(parseInt(end_year));

var dif_indays = Math.round( (endingdate.getTime() -
startingdate.getTime()) / (1000 * 60 * 60 * 24) );

// For debug
document.writeln(start_date + " " + end_date);
document.writeln(startingdate + " " + endingdate);

if ( (erotus_paivia < 7) && (erotus_paivia > 0) )
args.IsValid = true;
else
args.IsValid = false;
}

<snip>

I'd check for typo's first, e.g.:

var star_tdate; < t after underscore
start_year = start_date.substring(6,10); <start year
start_year = end_date.substring(6,10); <should be end year

Mike

Jul 23 '05 #2
Esa wrote:
function Validaattori_1viikko(source,args)
{
var star_tdate;
var end_date;
start_date = document.all("Laina_alku").value;
end_date = document.all("Laina_loppu").value;
var start_day;
var end_day;
var start_month;
var end_month;
var start_year;
var end_year;
start_day = start_date.substring(0,2);
end_day = end_date.substring(0,2);
start_month = start_date.substring(3,5);
end_month = end_date.substring(3,5);
start_year = start_date.substring(6,10);
start_year = end_date.substring(6,10);
var startingdate = new Date();
startingdate.setMonth(parseInt(start_month)-1,parseInt(start_day));
startingdate.setYear(parseInt(start_year));

var endingdate = new Date();
date_loppu.setMonth(parseInt(end_month)-1,parseInt(end_day));
date_loppu.setYear(parseInt(end_year));

var dif_indays = Math.round( (endingdate.getTime() -
startingdate.getTime()) / (1000 * 60 * 60 * 24) );

// For debug
document.writeln(start_date + " " + end_date);
document.writeln(startingdate + " " + endingdate);

if ( (erotus_paivia < 7) && (erotus_paivia > 0) )
args.IsValid = true;
else
args.IsValid = false;
}
<snip>

if i pick start_date of lets say "29.04.2004" If i pick end_date of "08.04.2004" (or "09.05.2004")
End date is completely wrong.


This looks like it's working, I used a different method of assigning the
dates, found on this page:

http://www.webreference.com/js/column2/instance.html

var startingdate = new Date(start_year,start_month-1,start_day);
var endingdate = new Date(end_year,end_month-1,end_day);

<script type="text/javascript">
function validate() { //Validaattori_1viikko(source,args) {
var
start_date,end_date,start_day,end_day,start_month, end_month,start_year,end_year;
start_date = document.getElementById('startdate').value;
end_date = document.getElementById('enddate').value;
start_day = start_date.substring(0,2);
end_day = end_date.substring(0,2);
start_month = start_date.substring(3,5);
end_month = end_date.substring(3,5);
start_year = start_date.substring(6,10);
end_year = end_date.substring(6,19);
var startingdate = new Date(start_year,start_month-1,start_day);
var endingdate = new Date(end_year,end_month-1,end_day);
var dif_indays = Math.round( (endingdate.getTime() -
startingdate.getTime()) / (1000 * 60 * 60 * 24) );
document.getElementById('output').innerHTML = 'start date
'+start_date+'<br>end date '+ end_date+'<br><br>starting date
'+startingdate+'<br>ending date '+ endingdate;
}
</script>
</head>
<body>
<input type="text" name="startdate" id="startdate" value="30.01.2004">
<input type="text" name="enddate" id="enddate" value="09.05.2004"
size="20">
<input type=button value="doit" onclick="validate()">
<div id="output">
</div>
</body>
</html>

Mike

Jul 23 '05 #3

Ah yes, but those typos are there only because I translated the variable
names from Finnish to English so that everyone can understand them. The
typos do not excist in my own code.

Thx for the reply but thats not the problem.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #4
> var endingdate = new Date();
date_loppu.setMonth(parseInt(end_month)-1,parseInt(end_day));
date_loppu.setYear(parseInt(end_year));


I don't know if this is in the real code but "endingdate" is initialized to
the current system date and never modified. The "date_loppu" object in the
above code should be "endingdate".

Also you may want to try creating the date object by passing in the values
instead of setting them after the fact.

Example:

var endingdate = new Date(parseInt(end_year),
parseInt(end_month)-1,
parseInt(end_day),0,0,0,0)
Jul 23 '05 #5
Esa Itkonen wrote:
Ah yes, but those typos are there only because I translated the variable
names from Finnish to English so that everyone can understand them. The
typos do not excist in my own code.

Thx for the reply but thats not the problem.


You are using parseInt() with only one parameter on values that contain
leading zeros. As a result, values passed to parseInt() with a leading zero
are assumed to be base-8. parseInt("08") == 0, parseInt("09") == 0.

Use the second parameter on parseInt() to specify a base and it will
properly parse those values. parseInt("08", 10) == 8, parseInt("09", 10) ==
9.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #6
JRS: In article <c4**************************@posting.google.com >, seen
in news:comp.lang.javascript, Esa <es*********@turunsanomat.fi> posted
at Thu, 29 Apr 2004 02:59:43 :

A description of intent would have helped.

ISTM that alku & loppa contain date strings as dd.mm.yyyy, and you want
to find the difference between the dates. Consider

var startingdate =
new Date(start_date.replace(/(\d+)\D(\d+)\D(\d+)/, "$3/$2/$1"))

var ending_date = // similar (use a function !)

Those will accept any all-numeric date in D M Y order with a full year,
provided that the date is later than B.C. 99. For date validation, see
via sig below.

var dif_indays = Math.round( (endingdate.getTime() -
startingdate.getTime()) / (1000 * 60 * 60 * 24) );
Looks OK, but could be Math.round((endingdate-startingdate)/864e5)

if ( (erotus_paivia < 7) && (erotus_paivia > 0) )
args.IsValid = true;
else
args.IsValid = false;


Could be args.IsValid = erotus_paivia < 7 && erotus_paivia > 0
Code that is modified for News *MUST* be tested in its modified state.

Your real problem (which the above would avoid) is almost certainly that
described in FAQ 4.12; you will get it for the 8th & 9th of every month,
and for all of August & September. With parseInt, always use a second
parameter unless you are (a) sure it's not needed, & (b) right. And, if
the second parameter is 10, you need parseInt only if there may be
visible characters after the numerics - see FAQ 4.21, and use unary
plus.

i.e.
startingdate.setYear(parseInt(start_year));
-> startingdate.setYear(parseInt(start_year, 10));
->> startingdate.setYear(+start_year);

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #7
Sorry fellas for being careless with the code I pasted. Should have
tested it after translation.

I got it to work now by adding the parameter 10 to parseInt. Thanks to
all for help!

So this function was used in Asp.Net pages as Clientvalidationfunction
for CustomValidator control.
Don't know if this is the right forum but since its kinda related I'll
shoot.
I'v been trying to make a server side Validation for the same thing. I
have .aspx page and its code is in .aspx.cs-file. Does it matter where
the OnServerValidate-function is? In the .aspx-file within "<script
"runat=server>" or just in .aspx.cs-file. I'v read many articles about
Serverside validation with customvalidator and tried to do as they say
but it never seems to do anything even if I just put "e.IsValid = false"
to the check, it still doesn't cause the validation to fail.
CausesValidation-property is "true" in the button I use for submit and
all the other validators (requiredfield etc...) do work. Does server
side validation require some setting from IIS? Well hope someone knows
whats going on :/
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #8

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

Similar topics

3
by: Andrew DeFaria | last post by:
I have the following php code: function Today2SQLDatetime () { $today = date ("Y-m-d H:i:s"); print "date (\"Y-m-d H:i:s\") returned $today<br>"; return $today; return date ("Y-m-d H:i:s"); }...
2
by: Veerle | last post by:
Hello, I have made a first version of the frontpage of my new homepage. I use the small javascript <script type="text/javascript"> var modDate = new Date(document.lastModified); var day =...
3
by: K R | last post by:
Hi, I have generated this XML from my application. But, when I open this XML, it is throwing error. Please help me to resolve this. <?xml version="1.0" encoding="utf-8" ?> <searchResults>...
2
by: Terry | last post by:
I have used the code provided in the PreciseDateDiff function at the following Access Web link to calculate the time change date (from Daylight to Standard time and vice versa): ...
7
by: MLH | last post by:
Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date '************************************************************************** ' Accepts a date. Determines month & year of the date....
3
by: Soren Jorgensen | last post by:
Hi, Following code should give the number of weeks in years 1998-2010 for a Danish calendar (on a Danish box) GregorianCalendar cal = new GregorianCalendar(); for(int i = 1998; i < 2010; i++)...
3
by: Terry Olsen | last post by:
I've got 2 different web pages, both updating the same SQL database. One is for the Technician and one is for the Manager. The technician's update page works fine but the Manager's update page...
3
by: Peter | last post by:
Hello all, I have the following t-sql batch: create procedure stp_test ( @p_date1 as datetime = null, @p_date2 as datetime = null )
11
by: MNNovice | last post by:
My form has two text boxes, txtDateFrom and txtDateTo. A macro button with the caption Month is to automatically fill in txtDateFrom and txtDateTo with current month date. My code reads as: ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.