By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,740 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,740 IT Pros & Developers. It's quick & easy.

changing date format

P: n/a
Hello,

i'm using a form with 2 dates in the format mm/dd/yyyy in it. I wanna
calculate the days between the two dates.
I tried it like this:

<script language="JavaScript">
var date1 = new date("10/22/2003");
var date2 = new date("10/25/2003");
var diff = date2.gettime() - date1.gettime();
</script>

only it seems that the date format like this won't work is there anything i
can do to convert the format or convience it to use the date like that?

Thanks for the help
chris
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Chris wrote:
i'm using a form with 2 dates in the format mm/dd/yyyy in it. I wanna
calculate the days between the two dates. I tried it like this:

<script language="JavaScript">
That's invalid HTML 4, the `type' attribute is missing:

http://validator.w3.org/
var date1 = new date("10/22/2003"); var date2 = new date("10/25/2003");
This won't work anyway or if it does, it works most certainly only for you:
JavaScript is case-sensitive, the constructor function for Date objects is
Date(...), not date(...).
var diff = date2.gettime() - date1.gettime();
</script>

only it seems that the date format like this won't work
Of course not, see
http://devedge.netscape.com/library/...e.html#1193137
is there anything i can do to convert the format or convience it to use
the date like that?


See the discussion with the subject `Date datatype' one week ago.
PointedEars

Jul 20 '05 #2

P: n/a
JRS: In article <vp************@corp.supernews.com>, seen in
news:comp.lang.javascript, Chris <cm***@gmx.de> posted at Wed, 22 Oct
2003 02:35:49 :-
i'm using a form with 2 dates in the format mm/dd/yyyy in it. I wanna
calculate the days between the two dates. var date1 = new date("10/22/2003");
var date2 = new date("10/25/2003");
var diff = date2.gettime() - date1.gettime();

Date differencing is covered in <URL:http://www.merlyn.demon.co.uk/js-
date2.htm>; read the rest of the Javascript material there too.

The 'word' "wanna" is not good English; if you can, please use proper
English, out of courtesy to those who are trying to learn it. Much the
same goes for "i'm"; case is important in proper English, as it is for
Javascript. For example, "new Date(...)".

You seem to be German; you should not therefore want to give dates in
DDF. While (AIUI) currently most browsers interpret 10/22/2002 as Oct
22, 2003, one should not rely on that, since it can readily be avoided
by using the proper form "2003/10/22" with ISO-8601 field order, or by
using "2003 Oct 22". I have not heard of any browser that does not
accept those. Note that while 04/07/2003 will be interpreted as April
7th by present browsers, a European reader may think that it means 4th
July.

Method gettime does not exist; getTime() returns milliseconds. The
number of milliseconds in a Javascript day is not always 864E5; you
should test for, say the cases of 2003/03/31 - 2003/03/28 and 2003/10/27
- 2003/10/24 (still assuming that you are in the EU).

Just date2 - date1 would suffice.

If the input is from a user, then you should consider validating the
dates. Code like you give would accept "13/13/2003" as meaning
2004-01-13.

--
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> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.