在Javascript中重新格式化复杂文本日期字符串的更好方法

时间:2012-09-10 21:09:09

标签: javascript jquery regex

我的CMS不允许我在后端重新格式化日期字符串,所以我用Javascript转换前端的文本。

这是CMS产生的一些例子:

  1. Sunday, November 11, 2012, 4:45 PM - 6:00 PM
  2. Every Sunday , 9:15 AM - 10:30 AM
  3. First Sunday of the month, 9:15 AM - 1:00 PM - Rm 306
  4. Every Wednesday, 5:30 PM - 8:30 PM, from 09/05/2012 to 11/14/2012
  5. 以下是我希望格式化的方式:

    1. Sun, Nov 11 @ 4:45-6p (短日和月份名称,没有年份,没有结尾:00)
    2. Every Sun @ 9:15-10:30a (如果他们同时是AM或PM,则第一次没有AM / PM指示符)
    3. First Sun of the month @ 9:15a-1p (小写A& P)
    4. Every Wed @ 5:30-8:30p, Sep 5-Nov 14 (显示日期跨度,删除年份,删除日期前导0)
    5. 所以这是我提出的功能:

      $(".smart-time").each(function(){
          $(this).html($(this).html()
              .replace(/( *)?-( *)?([0-9]([0-2])?:[0-5][0-9])/gi,"-$3") // Remove space in dashes between time
              .replace(/(:[0-5][0-9])([ ]?AM)/gi,"$1a") // Short AM
              .replace(/(:[0-5][0-9])([ ]?PM)/gi,"$1p") // Short PM
              .replace(/12:00( )?p/gi,"Noon") // 12:00p = Noon
              .replace(/12:00( )?a/gi,"Midnight") // 12:00a = Midnight
              .replace(/(:00| from)/gi,"") // No trailing :00
              .replace(/([0-9:]*)a-([0-9:]*)a/gi,"$1-$2a") // Remove first 'a' in span
              .replace(/([0-9:]*)p-([0-9:]*)p/gi,"$1-$2p") // Remove first 'p' in span
              .replace(/(\, |\/)(20[0-9][0-9])/gi,"") // Remove Year
              .replace(/ to /gi,"-") // to changed to -
              .replace(/\/(0)?([0-9]*)/gi,"/$2") // Remove leading 0 in dates
              .replace(/01\//gi,"Jan ") // Change month number to short name
              .replace(/02\//gi,"Feb ")
              .replace(/03\//gi,"Mar ")
              .replace(/04\//gi,"Apr ")
              .replace(/05\//gi,"May ")
              .replace(/06\//gi,"Jun ")
              .replace(/07\//gi,"Jul ")
              .replace(/08\//gi,"Aug ")
              .replace(/09\//gi,"Sep ")
              .replace(/10\//gi,"Oct ")
              .replace(/11\//gi,"Nov ")
              .replace(/12\//gi,"Dec ")
              .replace(/(Sun|Mon|Tue|Wed|Thu|Fri|Sat)(s|nes|rs|ur)?day/gi,"$1") // Shorten Day names
              .replace(/\, <\/span>/gi," @ </span>") // Change , to @
              .replace(/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)((r)?uary|(t)?(em)?(o)?ber|ch|il|e|y|ust)?/gi,"$1") // Shorten Month names
          );
      });
      

      这很有效,但感觉非常笨重。

      有更好的方法可以做到这一点或更简单的方法来实现相同的目标吗?

3 个答案:

答案 0 :(得分:1)

如果您不反对引入图书馆,我会看一下DateJs

它有许多非常好的日期格式化功能。

来自getting started page

// Lets start simple. "Today"
Date.parse('today');

// How about tomorrow?
Date.parse('tomorrow');

// July 8?
Date.parse('July 8');

// With a year?
Date.parse('July 8th, 2007');

// And time?
Date.parse('July 8th, 2007, 10:30 PM');

// Get the date, move to Monday (if not already Monday),
// then alert the date to the user in a different format.
var d1 = Date.parse('8-Jul-2007');
if (!d1.is().monday()) {
    d1.last().monday();
}
alert(d1.toString('dddd, MMMM d, yyyy'));

答案 1 :(得分:1)

值得考虑的另一个选择是jQuery UI Datepicker的parseDate()方法,记录为here

然而,虽然这很容易使用,但如果你已经在你的网站上使用了datepicker,那么它显然是实用的。

答案 2 :(得分:0)

你应该查看Date.parseDate。这是一篇很好的示例文章: http://www.xaprb.com/articles/javascript-date-parsing-demo.html