Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #22358

Re: Date Conversion ?

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Date Conversion ?
Date 2013-12-17 17:02 +0000
Organization A noiseless patient Spider
Message-ID <l8q03k$fid$1@dont-email.me> (permalink)
References <bh9en6Foh76U1@mid.individual.net> <1l7578dngtzld$.19qzw1e0gy93t$.dlg@40tude.net> <l8o67q$tli$1@dont-email.me> <XnsA299674D3AA55eejj99@194.109.133.133>

Show all headers | View raw


On Tue, 17 Dec 2013 10:09:17 +0100, Evertjan. wrote:

> Denis McMahon wrote on 17 dec 2013 in comp.lang.javascript:
> 
>>> function parseDate(dateStr) {
>>>   //for "Mmm dd/yy" format, year 20nn only.
>>>   //converts to "Mmm dd yyyy" (good for FF, IE8, Chrome, Opera).
>>>   //returns NaN if dateStr is in invalid format.
>>>   var date=dateStr.replace(/([a-z]+ \d+)\/(\d+)/,"$1 20$2"); return
>>>   (new Date(date)).valueOf();
>>> }
>> 
>> To walk an array of numbers once to find the min and max values:
>> 
>> 
> 1 There is no need for conversion into a date object value,
> [ and fearing a DST-change during the code's execution perhaps ;-) ]
> Putting these numbers in the below array will do fine as well:
> 
> (year * 10000) + (month * 100) + (day * 1)
> 
> ===================================
> 
>> var low, high, i;
>> low = high = arr[0];
>> for( i = 1; i < arr.length; i++ ) {
>> 	if ( arr[i] < low ) low = arr[i]; if ( arr[i] > high ) high = arr
[i];
>> }
> 
> 2 You can just store the pointers,
> supposedly those results are what you need in the html.
> 
> Try [not tested]:
> 
> var arr = [ -1, .... ];
> var low = 0, high = 0, arrlen = arr.length, i;
> 
> for( i = 1; i < arrlen; i++ ) {
>    if ( arr[i] < arr[low] ) low = i; if ( arr[i] > arr[high] ) high = i;
> };

I think there's a flaw in the logic. If arr[0] is -1 and all the numbers 
in the array are +ve, you'll never change the low index, because no 
numbers in the array are less than arr[0] and low starts as 0. If you 
don't stick the -1 value in as the zeroth element, however, it should 
work fine.

My assumption is that either the first number in the array is the 
lowest / highest number, or another number in the array is lower / higher 
than the first number.

Taking arr.length will probably see a performance improvement over my 
code, I assume that's why you did it.

Looking at performance though, my variant does one array lookup unless it 
needs to change the low or high value, yours does a lookup for every 
comparison. Probably not going to be noticeable on arrays of a few 
thousand members or less though.

Finally, Having set both low and high to the same values initially, it 
might be slightly quicker if we assume that for any existing pair of high 
and low values, where low <= high, no new value can be both < low and > 
high, so that if we find a new low value, we can skip the "is this a new 
high value" test:

If you want the values:

var low, high, i;
var low = high = arr[0], arrlen = arr.length;
for( i = 1; i < arrlen; i++ ) {
	if ( arr[i] < low ) low = arr[i];
	else if ( arr[i] > high ) high = arr[i];
}

Or if we're looking for the indexes (possibly slower) a slight variant of 
your code:

var low, high, i;
var low = high = 0, arrlen = arr.length;
for( i = 1; i < arrlen; i++ ) {
	if ( arr[i] < arr[low] ) low = i;
	else if ( arr[i] > arr[high] ) high = i;
}

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Date Conversion ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-12-16 15:55 -0700
  Re: Date Conversion ? Bjoern Hoehrmann <bjoern@hoehrmann.de> - 2013-12-17 01:06 +0100
    Re: Date Conversion ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-19 12:34 +0100
      Re: Date Conversion ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-20 22:29 +0100
        Re: Date Conversion ? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2013-12-23 10:00 -0200
  Re: Date Conversion ? JJ <duh@nah.meh> - 2013-12-17 07:06 +0700
    Re: Date Conversion ? Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-17 00:35 +0000
      Re: Date Conversion ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-12-16 18:16 -0700
      Re: Date Conversion ? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-12-17 10:09 +0100
        Re: Date Conversion ? Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-17 17:02 +0000
        Re: Date Conversion ? Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-12-18 19:14 +0000
          Re: Date Conversion ? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-12-19 15:42 +0100
            Re: Date Conversion ? "Michael F. Stemper" <michael.stemper@gmail.com> - 2013-12-19 09:20 -0600
              Re: Date Conversion ? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-12-20 10:04 +0100
      Re: Date Conversion ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-17 15:12 +0100
        Re: Date Conversion ? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2013-12-17 18:10 -0200
          Re: Date Conversion ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-17 21:42 +0100
    Re: Date Conversion ? Bjoern Hoehrmann <bjoern@hoehrmann.de> - 2013-12-18 13:36 +0100
  Re: Date Conversion ? Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-12-17 19:02 +0000
    Re: Date Conversion ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-12-17 20:51 -0700
      Re: Date Conversion ? Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-12-19 20:21 +0000
        Re: Date Conversion ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-12-20 09:52 -0700
          Re: Date Conversion ? Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-12-21 20:41 +0000
            Re: Date Conversion ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-12-22 09:52 -0700

csiph-web