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


Groups > comp.lang.javascript > #22357

Re: Date Conversion ?

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!217.188.199.168.MISMATCH!takemy.news.telefonica.de!telefonica.de!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <1744237.gKV8rdnPMv@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Organization PointedEars Software (PES)
Date Tue, 17 Dec 2013 15:12:55 +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+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: Date Conversion ?
Newsgroups comp.lang.javascript
References <bh9en6Foh76U1@mid.individual.net> <1l7578dngtzld$.19qzw1e0gy93t$.dlg@40tude.net> <l8o67q$tli$1@dont-email.me>
Followup-To comp.lang.javascript
MIME-Version 1.0
Lines 81
NNTP-Posting-Date 17 Dec 2013 15:12:59 CET
NNTP-Posting-Host 10bdd5a9.newsspool4.arcor-online.net
X-Trace DXC=_2WFMf5<Idb<6cDJZfMd_c4IUK<Cl32<a4Fo<]lROoRa8kF<OcfhCOkN7R:9EjSaToDZm8W4\YJNld0KWJRZReUo6ACEa4WY4?jJYhJ0h`TBhm
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:22357

Followups directed to: comp.lang.javascript

Show key headers only | View raw


Denis McMahon wrote:

> On Tue, 17 Dec 2013 07:06:55 +0700, JJ wrote:
>>> Suggestion or Code very welcome.
>>> 
>>> btw, I can supply *my* code down to producing a correect date value,
>>> but it is clumsy and embarassing. But I will if you insist.
>> 
>> Fix the date string then make them to a Date object so that you can get
>> the timestamp integer value.
>> 
>> 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:
> 
> 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];
> }
> 
> // low and high are now the minimum and maximum values from the array

In implementations of ECMAScript Ed. 3, it is easier and probably more 
efficient to use the built-in Math.min() and Math.max() methods:

  var low = Math.min.apply(Math, arr);
  var high = Math.max.apply(Math, arr);

They also work for Date instances as Array elements because they convert the 
arguments to Number before comparison.  In general, if these methods are 
implemented, it is easiest to overwrite the “valueOf” property of the 
prototype object of the objects for the call – or provide the objects 
themselves with one –, which is triggered first by the internal ToNumber 
algorithm when called with hint Number.

<http://ecma-international.org/ecma-262/5.1/#sec-15.8.2.11>
<http://ecma-international.org/ecma-262/5.1/#sec-9.3>
<http://ecma-international.org/ecma-262/5.1/#sec-9.1>
<http://ecma-international.org/ecma-262/5.1/#sec-8.12.8>
<http://ecma-international.org/ecma-262/5.1/#sec-15.9.5.8>

One can also access the first and last element of a sorted array:

  var sortedCopy = arr.slice().sort(function (a, b) { return a - b; });
  var low = sortedCopy[0];
  var high = sortedCopy[sortedCopy.length - 1];

<http://ecma-international.org/ecma-262/5.1/#sec-15.4.4.11>

Finally, all implementations of ECMAScript Ed. 5 – not just JavaScript 
anymore – provide Array.prototype.reduce(), as mentioned by Björn:

  var low = arr.reduce(
    function (accumulator, e, i, a) {
      if (е < accumulator)
      {
        return e;
      }

      return accumulator;
    },
    arr[0]);

<http://ecma-international.org/ecma-262/5.1/#sec-15.4.4.21>

There appears to be no point to use .map() here because if you have .map() 
you have .reduce() and can thereby shorten the scope chain.
  
-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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