- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
看起来当我使用 format()
函数时,它会自动将原始 UTC 时间转换为我的时区 (UTC+8)。我已经研究了他们的文档几个小时,但似乎找不到将其默认为 UTC 时间的方法。
import { parseISO, format } from "date-fns";
const time = "2019-10-25T08:10:00Z";
const parsedTime = parseISO(time);
console.log(parsedTime); // 2019-10-25T08:10:00.000Z
const formattedTime = format(parsedTime, "yyyy-MM-dd kk:mm:ss");
console.log(formattedTime); // 2019-10-25 16:10:00 <-- 8 HOURS OFF!!
我尝试使用包 data-fns-tz
并使用类似的东西
format(parsedTime, "yyyy-MM-dd kk:mm:ss", {timeZone: "UTC"});
仍然没有运气。
请帮忙!
2019-10-25 08:10:00
2019-10-25 16:10:00
最佳答案
你快到了。这对我有用:
import { parseISO } from "date-fns";
import { format, utcToZonedTime } from "date-fns-tz";
const time = "2019-10-25T08:10:00Z";
const parsedTime = parseISO(time);
console.log(parsedTime); // 2019-10-25T08:10:00.000Z
const formatInTimeZone = (date, fmt, tz) =>
format(utcToZonedTime(date, tz),
fmt,
{ timeZone: tz });
const formattedTime = formatInTimeZone(parsedTime, "yyyy-MM-dd kk:mm:ss xxx", "UTC");
console.log(formattedTime); // 2019-10-25 08:10:00 +00:00
date-fns[-tz] 库坚持内置的 Date
数据类型 carries no TZ info .
有些函数将其视为一个时刻,但有些函数(例如 format
)将其更像是日历组件的结构 — 2019 年,...,第 25 天,第 08 小时,...
现在的麻烦是 Date
在内部只是一个时刻。它的方法提供了到/从本地时区中的日历组件的映射。
因此,为了表示不同的时区,date-fns-tz/utcToZonedTime
临时生成 Date
实例,表示错误的时间 — 只是为了让它的本地时间日历组件成为我们想要的!
date-fns-tz/format
函数的时区输入仅影响打印时区的模板字符 (XX..X
, xx..x
, zz..z
, OO..O
).
参见 https://github.com/marnusw/date-fns-tz/issues/36对于这种“转移”技术(以及激发他们的实际用例)的一些讨论......
这有点低级且有风险,但我在上面组合它们的具体方式 — formatInTimeZone()
— 我相信是一个安全的方法。
关于javascript - 日期-fns |如何格式化为 UTC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58561169/
如我的问题所述。我真的不明白 date-fns 的 isValid 和 luxon 的 isValid 之间的区别。我有一个日期字符串变量和一个用 new Date() 调用日期字符串的变量。 我的日
我正在使用 date-fns 并试图弄清楚如何从语言环境中获取日期格式字符串。 例如。当使用 en-US 语言环境时,我想得到 'MM/dd/yyyy' 作为结果。 我找到了这个答案 Get Loca
我遇到了 UI 问题,想在 3 hours ago 中显示时间戳距离和 4 hours ago等等。来自服务器的时间戳,其属性名为 createdAt它具有以下值。 createdAt: "2021-
我有一组日期字符串以这种格式存储在数据库中:2018-06-28T14:06:26.524Z2018-07-02T10:32:18.818Z2018-07-06T15:08:50.233Z我需要在前端
今天我在研究函数特征。尽管我在下面展示的示例实际上可能不是很有用,但我确实想知道为什么它无法编译。 pub fn do_something(o: &(dyn Other + 'static)) { }
我有一个以分钟为单位存储的时间 01:30:00 我想将其显示为人类可读的 1 小时 30 分钟或 90 分钟。 实现他的最佳方法是什么?我正在使用 Date fns 来获取日期,但无法让 getmi
我正在使用 date-fns,我可以使用像 isMonday(day) 这样的助手,它返回 true is if the day is a Monday,或者 isWithinRange(start,
const dt = new Date('2017-12-12'); console.log(format(dt, 'YYYY-MM-DD')); 以上代码在美国记录 2017-12-11,但在印度记
我不知道如何提供 Stream我在 await 异步函数中获取流值所需的数据。 我已尝试实现 Stream trait 直接,但我遇到了问题,因为我想使用像 awaiting 这样的异步东西,编译器不
当我使用导入已安装的节点模块“date-fns”时 从 'date-fns' 导入 { startOfDay}; 然后我得到编译错误: 错误 TS2307:找不到模块“date-fns”。 但是以下不
我目前正在我的 Angular 应用程序中从 Moment.js 切换到 date-fns,但我遇到了障碍。 我还找到了一个由 angular @angular/material-date-fns-a
我有这个代码示例: function compareDate(value: string) { return isBefore( parse(value, 'dd-MM-yyyy', ne
所以基本上我正在尝试使用 addBusinessDays(new Date(), 3)它设置了一个开始日期而不考虑周末。但是,我也需要在其中使用日期列表(假期)。我找不到任何有关如何将这些假期添加到
如何以主格格式获取月份名称? import { format } from 'date-fns'; import { ru } from 'date-fns/locale'; format(new D
我正在我们的应用程序的上下文中评估 DateFns 和 Moment,并发现 DateFns 中似乎有一个重要的遗漏。 在 Moment 中,语言环境支持允许您格式化日期或时间的语言环境正确表示。例如
我只想验证我收到的字符串是否是使用 date-fns 的有效日期。 我会收到这样的日期“29/10/1989”,BR 日期。 我想将其转换为 29-10-1989 并检查其是否有效。 任何人都可以帮助
我一直在尝试使用 格式化日期日期-fns 但我一直失败。基本上我用momentJS让它工作得很好,但不能用date-fns: 这是我的约会: "10-13-20" // month, day, and
我正在使用 Date-FNS 库来获取 difference between two dates in minutes 。如果日期像这样传递: getDateTime(2018, 3, 4, 15,
我正在使用distanceInWord知道两个日期之间有多少时间。但这并不完全是我想要的,因为我需要更具体。 例如,如果我有这个:2019年1月1日15:00:00和2019年1月1日16:30:00
在我的项目中,我使用 date-fns 进行日期操作。需要在某个范围内迭代天数。为此,我使用以下代码: for ( // from and to are always start of s
我是一名优秀的程序员,十分优秀!