Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <3082498.rgpu3afg7L@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Thu, 19 Dec 2013 12:34:10 +0100 User-Agent: KNode/4.11.3 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. <5f4va916l0r2qoto1r4iq5dh0i0dnqthnv@hive.bjoern.hoehrmann.de> MIME-Version: 1.0 Lines: 96 NNTP-Posting-Date: 19 Dec 2013 12:34:14 CET NNTP-Posting-Host: 503d4ccd.newsspool1.arcor-online.net X-Trace: DXC=iF=@b=h]N?\cHPTNZh_e7Qic==]BZ:af^4Fo<]lROoRQnkgeX?EC@@PKV1GJ:PY58QDZm8W4\YJN\YUVHKH;GI^W:cHGUBBDcgYARo8Th2caEY X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:22365 Bjoern Hoehrmann wrote: > * Mel Smith wrote in comp.lang.javascript: >> I have an approx 300-row table with 13 columns. Column 3 of this >> table >> contains abbreviated date strings in the following format: >> >> 'Mmm dd/yy' (examples: May 31/13 Jan 1/11, etc) >> >> (All dates are current or in the past with all dates in the 21st >> Century, and IE8 must be accommodated) >> >> I would like to find the *oldest* date and the *newest* date in this >> column. > > A simple solution would be to use a Regex to split the value into its > components and converting into a form like `yyyymmdd`. You can then use > string comparison to find the minimum and maximum value (you could sort > the values but that has non-linear worst case behavior). In Perl, > > my ($m, $d, $y) = m#(\S+)\s+(\d+)/(\d+)#; > $m = { > Jan => 1, > Feb => 2, > ... > }->{$m}; > > my $comparable = sprintf "%04u%02u%02u", $y, $m, $d; > > In JavaScript you can use the `map` and `reduce` functions to map each > value to the comparable value and then finding the maximum and minimum > values in the mapped array, > > var mapped = mmmddyy_strings.map(function(e) { > ... > return comparable; > }); > var minimum = mapped.reduce(...); > var maximum = mapped.reduce(...); In JavaScript 1.5 and later, as in other implementations of ECMAScript, you can do much the same as in Perl: var matches = input.match(/(\S+)\s+(\d+)\/(\d+)/); var m = ({ "Jan": 1, "Feb": 2, … })[m]; sprintf() is not a built-in, and you would have to implement it yourself (like I do in JSX:string.js [1]), but it is not necessary because Date instances – especially under these conditions – are easily constructed and comparable: var mapped = mmmddyy_strings.map(function (e) { /* * or var matches = new jsx.regexp.String(e) .match(new jsx.regexp.RegExp( "(?\\S+)\\s+(?\\d+)/(?\\d+)")); */ var matches = input.match(/(\S+)\s+(\d+)\/(\d+)/); /* * or var m = ({ "Jan": 1, "Feb": 2, … })[matches.groups["m"]]; * with JSX:regexp.js [2] */ var m = ({ "Jan": 1, "Feb": 2, … })[matches[0]]; return new Date("20" + matches[2], m - 1, matches[1]); }); It is, however, more memory-efficient to create the mapping object only once. _______ [1] [2] -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.