【问题标题】:moment get time period AM//PM localized时刻获取时间段 AM//PM 本地化
【发布时间】:2019-06-14 05:41:06
【问题描述】:

我们是否有像 moment.weekdays()moment.months() 这样的函数,它会返回 AM PM 本地化? moment.period() 之类的东西?

timeFormatDefaultLocale({
    "dateTime": "%A, der %e. %B %Y, %X",
    "date": "%d.%m.%Y",
    "time": "%H:%M:%S",
    "periods": ["AM", "PM"], <------ I need to pass moment function to get period
    "days":  moment.weekdays() as any,
    "shortDays": moment.weekdaysShort() as any,
    "months": moment.months() as any,
    "shortMonths": moment.monthsShort() as any
});

【问题讨论】:

  • 这会有所帮助-momentjs.com
  • @Ankita Jaiswal 你能解释一下你到底需要什么吗?
  • 此时的代码,我没有日期,我会通过这种格式来本地化日期。
  • @Ajinkya 我想要这样的东西:句点:moment.period()。 moment.period() 这不是即时的函数。我想要一个返回本地化 AM/PM 的函数,就像我们有 moment.weekDays()

标签: javascript momentjs


【解决方案1】:

请试试这个

 moment.format('A') 

这里A 用于 AM PM 格式

请参考以下示例:

  console.log('Current Time is :'+moment().format('hh:mm A')) ;
   
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
 
 

【讨论】:

  • 我想要返回 ["AM","PM"] 的方法。这只会返回 AM 或 PM。
  • @Ankita Jaiswal 对不起,我没有得到你?你能详细说明一下吗?
  • 让 AM = moment("05:30:00","HH:mm:ss").format('hh:mm A').split(' ')[1];让 PM = moment("15:30:00","HH:mm:ss").format('hh:mm A').split('')[1]; "periods": [AM, PM],已经这样做了作为解决方法
  • @Ankita Jaiswal 我也在找它..如果有什么我会在回答中更新它的请告诉我
  • @Ajinkya 我已经发布了一个替代答案,也许你会对它感兴趣。
【解决方案2】:

没有内置的 momentjs 函数来本地化 ['AM', 'PM'],如果你查看文档的 Accessing locale specific functionality 部分,你会看到那一刻有 isPMmeridiem

localeData = moment.localeData()
// ...
localeData.isPM(amPmString);  // returns true iff amPmString represents PM
localeData.meridiem(hours, minutes, isLower);  // returns am/pm string for particular time-of-day in upper/lower case
// ...

查看Customize#AM/PMCustomize#AM/PM Parsing 部分,了解有关时刻如何管理子午线的更多信息。

对于大多数语言环境,您可以编写自己的解决方法 函数,例如(使用moment(Object)format()locale()):

function period(locale) {
  return [
    moment({h: 0}).locale(locale).format('A'),
    moment({h: 23}).locale(locale).format('A')
  ];
}

['en', 'en-us', 'de', 'it', 'fr', 'zh-cn'].forEach(localeName => {
  console.log( localeName, period(localeName) );
});
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"&gt;&lt;/script&gt;

如果您还需要管理具有多个 AM/PM 值的语言环境(例如 zh-cn),您可以使用以下内容:

function period(locale) {
  const result = new Set();
  let m = moment({h: 0}).locale(locale);
  for(let i=0; i<24; i++) {
    result.add( m.format('A') );
    m.add(1, 'h');
  }
  return Array.from(result);
}

['en', 'en-us', 'de', 'it', 'fr', 'zh-cn'].forEach(localeName => {
  console.log( localeName, period(localeName) );
});
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"&gt;&lt;/script&gt;

如果您考虑使用其他库,请注意 Luxon (momentjs "younger brother") 具有可能对您有所帮助的 Info.meridiems() 方法,即使似乎总是处理 AM 的 2 个值/PM(见zh-cn结果):

const Info = luxon.Info;

['en', 'en-us', 'de', 'it', 'fr', 'zh-cn'].forEach(localeName => {
  console.log( localeName, Info.meridiems({ locale: localeName }) );
});
&lt;script src="https://cdn.jsdelivr.net/npm/luxon@1.16.0/build/global/luxon.min.js"&gt;&lt;/script&gt;

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2021-02-17
    • 2013-08-19
    • 2023-03-16
    • 2021-12-29
    相关资源
    最近更新 更多