【问题标题】:How can I transform a date/time coming from an excel file to momentjs utc date?如何将来自 excel 文件的日期/时间转换为 momentjs utc 日期?
【发布时间】:2020-06-08 21:11:22
【问题描述】:

我在将 Excel 时间转换为 UTC 时遇到了一些问题。

我从 excel 收到的时间是这样的:0.3333333333333333 它是一个介于 01 之间的数字。

我有这个代码:

  const formatTime = (days) => {
    const timeFormated = new Date(days * 864e5).toLocaleString("en-US", {
      timeStyle: "short",
      timeZone: "UTC",
    });

    return new moment.utc(timeFormated, "HH:mm").format();
  };

假设我将其传递给时刻:formatTime(0.3333333333333333)

我得到一个这样的字符串,而不仅仅是时间:

"2020-06-08T20:00:00Z"

我只需要向用户显示时间,例如:6:00 PM 或无论结果如何,它都应该是 UTC 人类可读的。

请注意我正在使用toLocaleString 做一件事,我不知道它是否正确。因此,如果我做错与否,我想得到一些建议,以及如何才能仅向用户显示时间。你知道,小时和分钟。

【问题讨论】:

  • 你有没有试过像这样传递一个格式:format('h:mm:ss a')

标签: javascript reactjs ecmascript-6 momentjs


【解决方案1】:

您需要将格式字符串传递给momentjs格式化程序

return new moment.utc(timeFormated, "HH:mm").format("HH:mm A");

上午下午的格式A

Moment JS 文档https://momentjs.com/docs/#/displaying/format/

看起来您的 timeFormated 具有 @Non 所说的正确格式。您可以只在 formatTime 函数上返回 timeFormated ,除非您想要稍微不同的格式,即“h:mm:ss a”,在这种情况下,您可以使用我的示例中所示的 format 函数。

【讨论】:

  • 变量timeFormated怎么样,可以吗?
  • @Non 只是将 timeFormated 打印到控制台。看起来你可以只返回 timeFormated
猜你喜欢
  • 2019-08-31
  • 1970-01-01
  • 2014-01-24
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 2012-10-07
相关资源
最近更新 更多