1

我的 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

这是我希望它的格式:

  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)

所以这是我想出的功能:

$(".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
    );
});

这行得通,但感觉非常……笨重。

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

4

3 回答 3

1

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

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

入门页面

// 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'));
于 2012-09-10T21:20:20.790 回答
1

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

然而,虽然这很容易使用,但它显然只有在您已经在您的网站上使用 datepicker 时才实用。

于 2012-09-10T21:23:57.157 回答
0

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

于 2012-09-10T21:12:22.390 回答