如何使用Intl获取工作日名称?

时间:2015-05-25 11:17:43

标签: javascript internationalization ecmascript-intl

我想在我的i18n-ed应用中显示7个工作日的列表:

Sunday, Monday, Tuesday... Saturday

我依靠Intl global object格式化日期/时间,但我找不到工作日名称的简单方法。

我想我可以在EPOCH时间增加一些时间,以达到一周的第一天,但​​是我没有在工作日找到格式化程序打印。

var date = new Date(0);
date.setDate(4 + day);
for (var i = 0; i < 7; i++) {
  var weekday = new Intl.DateTimeFormat(["en"], {
      weekday: "short" // ?? what should I put here
  }).format(date);
  console.log(weekday);
}

输出:

Sunday, January 4, 1970
Monday, January 5, 1970
Tuesday, January 6, 1970
Wednesday, January 7, 1970
Thursday, January 8, 1970
Friday, January 9, 1970
Saturday, January 10, 1970

期望的输出:

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

我还希望有更短的版本,例如Sun, Mon, Tue...

或者,有没有办法从Intl获取工作日字符串?我试图通过控制台探索对象,但我找不到它们。

5 个答案:

答案 0 :(得分:5)

我被误导了,因为我使用的是Intl polyfill,它不支持{ weekday: "short" }作为选项。

使用本机Intl实现按预期工作。

答案 1 :(得分:3)

const weekdayDateMap = {
  Mon: new Date('2020-01-06'),
  Tue: new Date('2020-01-07'),
  Wed: new Date('2020-01-08'),
  Thu: new Date('2020-01-09'),
  Fri: new Date('2020-01-10'),
  Sat: new Date('2020-01-11'),
  Sun: new Date('2020-01-12'),
};
const shortWeekdays = Object.keys(weekdayDateMap);

const getDayOfWeek = (shortName, locale = 'en-US', length = 'short') =>
  new Intl.DateTimeFormat(locale, { weekday: length }).format(weekdayDateMap[shortName]);

const getDaysOfWeek = (locale = 'en-US', length = 'short') =>
  shortWeekdays.map(shortName => getDayOfWeek(shortName, locale, length));

用法:

getDayOfWeek('Mon', 'de-DE') -> "Mo"

getDaysOfWeek('de-DE') -> ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"]

注意:

如果您每秒调用格式化程序很多次,那么一次创建格式化程序并重新使用它会更有效。

答案 2 :(得分:2)

我在 2021 年使用 es6 的解决方案

/**
 * Return list of days
 * ? localeName : name of local, f.e. en-GB, default es-MX
 *  ✅ weekday   : formart of weekday short/long (Default)
 */
function daysForLocale(localeName = 'es-MX', weekday = 'long') {
  const format = new Intl.DateTimeFormat(localeName, { weekday }).format;
  return [...Array(7).keys()]
    .map((day) => format(new Date(Date.UTC(2021, 5, day))));
}

// ##############################################################
// testing daysForLocale function
// ##############################################################
console.log(daysForLocale());
// ['domingo','lunes',...,'viernes','sábado']
console.log(daysForLocale('en-GB', 'short'));
// ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri','Sat']
console.log(daysForLocale('ja-JP', 'short'));
// ['日', '月', '火','水', '木', '金','土']

答案 3 :(得分:0)

Intl(ergo Intl.js)也没有API来获取工作日名称,所以你必须做一些黑客的事情,用正确的方式查询一堆天选项。这可能是add support for doing so in the future,但现在你基本上没有运气了。 (至少,除非您手动使用修补程序增加Intl.js。)

答案 4 :(得分:-1)

试试这个,拆分工作日字符串。

    private void disableNic()
    {
        SelectQuery wmiQuery = new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId != NULL");
        ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(wmiQuery);
        foreach (ManagementObject item in searchProcedure.Get())
        {
            if (((string)item["NetConnectionId"]) == Interface.Name)
            {
                item.InvokeMethod("Disable", null);
            }
        }
    }

    void Disable(string interfaceName)
    {
        System.Diagnostics.ProcessStartInfo psi =
            new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface \"" + interfaceName + "\" disable");
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo = psi;
        p.Start();
    }