Re: dd/mm/yyyy Date Compare Problem
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: dd/mm/yyyy Date Compare Problem

From: Dr John Stockton <jrs@merlyn.demon.co.uk>
Date: Sat Apr 29 2006 - 23:12:12 CEST

JRS: In article <e2uf60015o6@news3.newsguy.com>, dated Fri, 28 Apr 2006
20:23:07 remote, seen in news:comp.lang.javascript, Matt Kruse
<newsgroups@mattkruse.com> posted :
>Dr John Stockton wrote:

>I've re-written my date code recently to extend Date using prototypes for
>most functions. One thing you might not like is that I don't use regular
>expressions to parse text. Instead, I still use the manual substring
>approach. Someday I'll update it further, but it's solid as-is, and I don't
>have a lot of time, so updating working code doesn't seem like the best use
>of it :)
>
>> it might be
>> quite easy to produce, in javascript, a de-commenter and de-indenter.
>> Your library could be fetched in its full glory, but readily reduced
>> to the working parts without positive obfuscation.
>> ...But perhaps you already have something better.
>
>I do this in real-time in PHP, actually.
> ...

I did mean that the de-*er would be distributed, for example on a web
page; then the material could be sent in full from library author to
each Web programmer, who could retail a full-glory reference copy but
use in pages the reduced version.

>Since you read offline, I'll paste my source below in case you want to look
>at it. I'm always open to suggestions, although I can probably already name
>many of the same ones that you might offer ;) Hope the code isn't too long,
>and of course beware of wrapping.
>
>/*
>Date functions
>
>These functions are used to parse, format, and manipulate Date objects.
>See documentation and examples at http://www.JavascriptToolbox.com/lib/date/
>
>*/
>Date.$VERSION = 1.01;

Quote pruned.

>// Utility function to append a 0 to single-digit numbers
>Date.LZ = function(x) {return(x<0||x>9?"":"0")+x};

x<0|| is probably not needed in this contest; but prudent.

>Date.dayAbbreviations = new
>Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');

Add a Sunday at the end of the week, and you can match ISO 8601 too!

>// If the getFullYear() method is not defined, create it
>if (!Date.prototype.getFullYear) {
> Date.prototype.getFullYear = function() { var yy=this.getYear(); return
>(yy<1900?yy+1900:yy); }
>}

I have heard that there is or has been a browser which, at least in
1900..2099, has getYear() returning Year%100, for which that fails. See
js-date0.htm, #gY, #gFY. OTOH, if your library is in use in conjunction
with such a browser, the problem will have been noticeable for six
years.

> this.isInteger = function(val) {
> for (var i=0; i < val.length; i++) {
> if ("1234567890".indexOf(val.charAt(i))==-1) {
> return false;
> }
> }
> return true;
> };

or this.isInteger = function(val) { return !/\D/.test(val) }
and name could be isNonNegInt. ISTM that both fail on empty strings;
unary + seems to accept an empty string as 0, but parseInt gives NaN.

> // Is date valid for month?
> if (month==2) {
> // Check for leap year
> if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
> if (date > 29){
> return null;
> }
> }
> else {
> if (date > 28) {
> return null;
> }
> }
> }

There's no need to evaluate Leapness unless it's Feb 29. Feb <29 is
always good; Feb >29 always bad.

I'd use Date Object readback, or an array [0,31,28,31,...,31] to look up
the maximum date for the month, then if date > maximum then if date = 29
then OK = Leap.

>// Clear all time information in a date object
>Date.prototype.clearTime = function() {
> this.setHours(0);
> this.setMinutes(0);
> this.setSeconds(0);
> this.setMilliseconds(0);
> return this;
>}

setTing can be combined in JS 1.3+

>// Add an amount of time to a date. Negative numbers can be passed to
>subtract time.

However, the amount of time actually added can depend on the date; Years
and Feb vary in length by a day, and days can vary by an hour.

>Date.prototype.add = function(interval, number) {

> else if (interval=='w') { // Weekday

I think you are adding a number of WORKING days, stepwise. You could do
div and mod 5, then add 7 times the div result and step the mod result.
It would be faster for long intervals.

> var step = (number>0)?1:-1;
> while (number!=0) {
> this.add('d',step);
> while(this.getDay()==0 || this.getDay()==6) {
> this.add('d',step);
> }
> number -= step;
> }
> }

Adding Minutes, Seconds could be appreciably faster with setTime (but
then Minutes sometimes wrong for Lord Howe Island).

You have support for FIPS/ISO elsewhere?
 

-- 
 © John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v4.00   IE 4 ©
 <URL:http://www.jibbering.com/faq/>  JL/RC: FAQ of 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.
Received on Mon May 1 05:27:12 2006