【问题标题】:Parse Custom Date and Sort解析自定义日期并排序
【发布时间】:2013-10-10 21:01:15
【问题描述】:

我是 javascript/jQuery 的新手,我正在尝试一些雄心勃勃的东西来提升我的技能水平。我发现了一些有帮助但被卡住的 sn-ps。

我有一堆格式如下的日期:dd-month-yyyy(2013 年 10 月 10 日)。据我了解,这在某种程度上是一种非常规的日期格式。所以我要做的是将日期解析为正常格式,然后使用 jQuery tinysort 插件(我认为我使用不正确)排列父 div。

我在这里做了一个 jsfiddle:http://jsfiddle.net/8BYDZ/

或者这是我的代码:

<div id="date-sort">
  <div class="date-content"> 
    <p>Some Content</p>
    <p class="date-sort">10-Oct-2013</p>
    <hr />
  </div>
  <div class="date-content"> 
    <p>Some Content</p>
    <p class="date-sort">12-Oct-2013</p>
    <hr />
  </div>
  <div class="date-content"> 
    <p>Some Content</p>
    <p class="date-sort">2-Sep-2013</p>
    <hr />
  </div>
  <div class="date-content"> 
    <p>Some Content</p>
    <p class="date-sort">22-Jun-2013</p>
    <hr />
  </div>
  <div class="date-content"> 
    <p>Some Content</p>
    <p class="date-sort">1-May-2013</p>
    <hr />
  </div>
</div>
$(document).ready(function(){
    function customParse(str) {
      var months = ['Jan','Feb','Mar','Apr','May','Jun',
                    'Jul','Aug','Sep','Oct','Nov','Dec'],
          n = months.length, re = /(\d{2})-([a-z]{3})-(\d{4})/i, matches;

      while(n--) { months[months[n]]=n; } // map month names to their index :)

      matches = str.match(re); // extract date parts from string

      return new Date(matches[3], months[matches[2]], matches[1]);
    }

    var array = [];

    var elements = $('.date-sort');

    for(var i = 0; i < elements.length; i++) {
       var current = elements[i];
        if(current.children.length === 0 && current.textContent.replace(/ |\n/g,'') !== '') {
           // Check the element has no children && that it is not empty
           customParse(current.textContent);
           array.push(current.textContent);
        }
    } 

    $('div#date-sort>.date-content>.date-sort').tsort();

});

感谢您提供任何帮助、见解或意见。

【问题讨论】:

  • 您需要将您的问题修改为更具体的内容,而不是寻求洞察力等。
  • 除了您可以在下面的 Kavun 的回答中找到已修复的错误之外,您还会遇到 textContent 属性的问题:IE

标签: javascript jquery parsing sorting date


【解决方案1】:

你需要给 tinysort 一个可排序的日期。

new Date(matches[3], months[matches[2]], matches[1]).toJSON();

// ...

current.setAttribute('data-date', customParse(current.textContent));

// ...

$('div#date-sort>.date-content').tsort('.date-sort', {attr: 'data-date' });

您的正则表达式过于严格,因为天数并不总是两个数字。

re = /(\d{1,2})-([a-z]{3})-(\d{4})/i

Here is a working jsiddle

【讨论】:

  • 差不多。在 customParse 之后添加一个 valueOf() 调用。我在第一个版本中犯了同样的错误;)
【解决方案2】:

您只需使用.replace() 消除- 标记,然后使用new Date().getTime() 将1970 年1 月1 日午夜和您的日期之间的毫秒数分配给一个数组,该数组将.sort() 说明如何你需要它。

$(document).ready(function(){
  var ds = $('.date-sort'), dt = [];
  ds.each(function(i){
    dt[i] = new Date($(this).html().replace(/-/g, ' ')).getTime();
  });
  dt.sort();
  ds.each(function(i){
    $(this).html(new Date(dt[i])/*add . whatever here*/);
  });    
});

我更新了你的 JSFiddle

【讨论】:

    【解决方案3】:

    在 JavaScript 中,对象通常被认为是创建关联映射的最佳方式(正如下面评论者所指出的,在数组上设置属性是完全有效的)。

    我会使用一个对象作为月份名称和数字之间的映射。此外,我会使用小写名称,因此我可以支付一次小写的成本,并在我的映射中使用该值。

    所以我会用对象初始化器替换您的 months 数组的定义:

    var months = { 'jan': 1, 'feb': 2, 'mar': 3, /*...*/ };
    

    这样,你就不需要循环了。

    现在您只需在输入时调用.toLowercase(),月份名称就会正确映射。

    【讨论】:

    • 他没有在数组上设置字符串索引。他使用括号表示法来访问数组上的命名属性(归根结底,这只是一个对象)。 var arr = [1]; arr['test'] = 2; console.log(arr); 将输出数组以及添加的属性test
    • 我不同意。那条线,虽然可能很丑,但完全有效且有效。它的作用是将月份数组的 Jan、Feb... 属性设置为其索引。如:months.Jan = 0, months.Feb = 1, ... ['Jan'] 表示法用于创建这些属性。
    • 是的,卡文,你是对的。我会相应地更新我的答案。我不喜欢以这种方式使用数组,但你的断言是正确的。
    • 我的立场是正确的,先生们。我运行代码并打印出months 的值,并暂时忘记了 JavaScript 中数组的真正本质(它们是具有属性的对象,但属性不可枚举,这就是为什么它们没有被打印出来) .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多