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

Better way to reformat a complex textual date string in Javascript

本文关键字:字符串 日期 更好 方法 文本 复杂 Javascript 格式化      更新时间:2023-09-26

我的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
    );
});

这很管用,但感觉很。。。庞大的

有更好的方法或更简单的方法来实现同样的目标吗?

另一个值得考虑的选项是jQuery UI Datepicker的parseDate()方法,在这里进行了说明。

然而,虽然这很容易使用,但显然只有当你已经在网站上使用日期选择器时,它才实用。

如果你不反对引入库,我会看看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'));

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