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


Groups > comp.lang.javascript > #22365

Re: Date Conversion ?

Message-ID <3082498.rgpu3afg7L@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-12-19 12:34 +0100
Subject Re: Date Conversion ?
Newsgroups comp.lang.javascript
References <bh9en6Foh76U1@mid.individual.net> <5f4va916l0r2qoto1r4iq5dh0i0dnqthnv@hive.bjoern.hoehrmann.de>

Show all headers | View raw


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(
             "(?<m>\\S+)\\s+(?<d>\\d+)/(?<y>\\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] <http://pointedears.de/wsvn/JSX/trunk/string.js>
[2] <http://PointedEars.de/scripts/test/regexp>
-- 
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