473,657 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wrong date?

Esa
function Validaattori_1v iikko(source,ar gs)
{
var star_tdate;
var end_date;
start_date = document.all("L aina_alku").val ue;
end_date = document.all("L aina_loppu").va lue;
var start_day;
var end_day;
var start_month;
var end_month;
var start_year;
var end_year;
start_day = start_date.subs tring(0,2);
end_day = end_date.substr ing(0,2);
start_month = start_date.subs tring(3,5);
end_month = end_date.substr ing(3,5);
start_year = start_date.subs tring(6,10);
start_year = end_date.substr ing(6,10);
var startingdate = new Date();
startingdate.se tMonth(parseInt (start_month)-1,parseInt(star t_day));
startingdate.se tYear(parseInt( start_year));

var endingdate = new Date();
date_loppu.setM onth(parseInt(e nd_month)-1,parseInt(end_ day));
date_loppu.setY ear(parseInt(en d_year));

var dif_indays = Math.round( (endingdate.get Time() -
startingdate.ge tTime()) / (1000 * 60 * 60 * 24) );

// For debug
document.writel n(start_date + " " + end_date);
document.writel n(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 clientvalidatio n 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 2164
Esa wrote:
function Validaattori_1v iikko(source,ar gs)
{
var star_tdate;
var end_date;
start_date = document.all("L aina_alku").val ue;
end_date = document.all("L aina_loppu").va lue;
var start_day;
var end_day;
var start_month;
var end_month;
var start_year;
var end_year;
start_day = start_date.subs tring(0,2);
end_day = end_date.substr ing(0,2);
start_month = start_date.subs tring(3,5);
end_month = end_date.substr ing(3,5);
start_year = start_date.subs tring(6,10);
start_year = end_date.substr ing(6,10);
var startingdate = new Date();
startingdate.se tMonth(parseInt (start_month)-1,parseInt(star t_day));
startingdate.se tYear(parseInt( start_year));

var endingdate = new Date();
date_loppu.setM onth(parseInt(e nd_month)-1,parseInt(end_ day));
date_loppu.setY ear(parseInt(en d_year));

var dif_indays = Math.round( (endingdate.get Time() -
startingdate.ge tTime()) / (1000 * 60 * 60 * 24) );

// For debug
document.writel n(start_date + " " + end_date);
document.writel n(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.subs tring(6,10); <start year
start_year = end_date.substr ing(6,10); <should be end year

Mike

Jul 23 '05 #2
Esa wrote:
function Validaattori_1v iikko(source,ar gs)
{
var star_tdate;
var end_date;
start_date = document.all("L aina_alku").val ue;
end_date = document.all("L aina_loppu").va lue;
var start_day;
var end_day;
var start_month;
var end_month;
var start_year;
var end_year;
start_day = start_date.subs tring(0,2);
end_day = end_date.substr ing(0,2);
start_month = start_date.subs tring(3,5);
end_month = end_date.substr ing(3,5);
start_year = start_date.subs tring(6,10);
start_year = end_date.substr ing(6,10);
var startingdate = new Date();
startingdate.se tMonth(parseInt (start_month)-1,parseInt(star t_day));
startingdate.se tYear(parseInt( start_year));

var endingdate = new Date();
date_loppu.setM onth(parseInt(e nd_month)-1,parseInt(end_ day));
date_loppu.setY ear(parseInt(en d_year));

var dif_indays = Math.round( (endingdate.get Time() -
startingdate.ge tTime()) / (1000 * 60 * 60 * 24) );

// For debug
document.writel n(start_date + " " + end_date);
document.writel n(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,e nd_month-1,end_day);

<script type="text/javascript">
function validate() { //Validaattori_1v iikko(source,ar gs) {
var
start_date,end_ date,start_day, end_day,start_m onth,end_month, start_year,end_ year;
start_date = document.getEle mentById('start date').value;
end_date = document.getEle mentById('endda te').value;
start_day = start_date.subs tring(0,2);
end_day = end_date.substr ing(0,2);
start_month = start_date.subs tring(3,5);
end_month = end_date.substr ing(3,5);
start_year = start_date.subs tring(6,10);
end_year = end_date.substr ing(6,19);
var startingdate = new Date(start_year ,start_month-1,start_day);
var endingdate = new Date(end_year,e nd_month-1,end_day);
var dif_indays = Math.round( (endingdate.get Time() -
startingdate.ge tTime()) / (1000 * 60 * 60 * 24) );
document.getEle mentById('outpu t').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.20 04">
<input type="text" name="enddate" id="enddate" value="09.05.20 04"
size="20">
<input type=button value="doit" onclick="valida te()">
<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.setM onth(parseInt(e nd_month)-1,parseInt(end_ day));
date_loppu.setY ear(parseInt(en d_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(e nd_year),
parseInt(end_mo nth)-1,
parseInt(end_da y),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*****@agrico reunited.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*********@tu runsanomat.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.get Time() -
startingdate.ge tTime()) / (1000 * 60 * 60 * 24) );
Looks OK, but could be Math.round((end ingdate-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.se tYear(parseInt( start_year));
-> startingdate.se tYear(parseInt( start_year, 10));
->> startingdate.se tYear(+start_ye ar);

--
© 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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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 Clientvalidatio nfunction
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 OnServerValidat e-function is? In the .aspx-file within "<script
"runat=serv er>" 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.
CausesValidatio n-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
4146
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"); } // Today2SQLDatetime Most of the time this works just fine. For today, for example, it
2
3486
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 = modDate.getDate(); var month = modDate.getMonth()+1; var year = modDate.getYear();
3
1537
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> <totalhits>16916</totalhits> <searchquery>*</searchquery> <timetaken>TIME TAKEN</timetaken>
2
2523
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): http://www.mvps.org/access/api/api0024.htm But looks like for this week only, there is a small error in the calculation in the 'GetSundate' function included in the code. You can see what I mean by adding the code below (copied directly from the above link)...
7
6030
by: MLH | last post by:
Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date '************************************************************************** ' Accepts a date. Determines month & year of the date. Returns ' the date of the last day of that month (in the same year) '************************************************************************** GetLastDayOfMonth = DateSerial(Year(dtDay), Month(dtDay), 31) End Function If I enter dtDay value of...
3
5475
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++) { DateTime date = new DateTime(i, 12, 31); int week= cal.GetWeekOfYear(date,
3
1532
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 doesn't. They are both the same..as far as I can tell... --------------------- This code works fine, updating the SQL database with the edited data. Code for Tech Page
3
2626
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
2097
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: Private Sub cmdmonth_Click() 'Sets the Date From and Date To text boxes 'to show complete month (from start to end of current month) Me!txtdatefrom = CDate("01/" & Month(Date) & "/" & Year(Date)) Me!txtDateTo = DateAdd("d", -1, DateAdd("m",...
0
8310
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7333
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6167
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.