我如何仅从字符串“ 2022-05-25T15:48:50.000Z”获得日期。使用Moment.js?

发布于 2025-02-01 04:40:49 字数 380 浏览 5 评论 0原文

我正在使用Moment.JS库,例如,我有代码 -

从“ Mists timeZone”中导入;

const date =“ 2022-05-25t15:48:50.000 z“

我已经尝试使用moment(date).format(“ yyyy-mm-dd”),但结果我要获得上一个日期-2022-05-24。

我知道这与UTC有关,并且我的项目在设置了“ MONS.TZ.SETDEFAULT(“ etc/utc””)的行中,从我的理解中可以解决它,但事实并非如此。

我只需要它具有2022-05-25的最终结果,我知道我在这里缺少一些很小的东西。.

我感谢任何帮助!

I am using moment.js library and for the example lets say I have the code -

import moment from "moment-timezone";

const date = "2022-05-25T15:48:50.000Z"

I've tried using moment(date).format("YYYY-MM-DD") but I am getting the previous date - 2022-05-24 as a result.

I know it has to do with utc, and my project has in it's setting the line 'moment.tz.setDefault("Etc/UTC")` which from my understand is supposed to fix it, but it doesn't.

I only need it to have the final result of 2022-05-25 and I know I am missing something really small here..

I'd appreciate any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛起拖把扫天下 2025-02-08 04:40:49

尝试一下
var newstringdate = yourdate.toisstring()。切片(0,10);
或者

var newStringDate = yourDate.toISOString().substring(0, 10);

try this
var newStringDate = yourDate.toISOString().slice(0, 10);
or

var newStringDate = yourDate.toISOString().substring(0, 10);
痞味浪人 2025-02-08 04:40:49

您的格式为ISO8601格式,您可以从此处检查: https://en.wikipedia.org/wiki/ ISO_8601

您可以尝试将完整格式发送到功能

const date = "2022-05-25T15:48:50.000Z"
var now = moment.utc(date,"YYYY-MM-DD\THH:mm:ss\Z").format("YYYY-MM-DD");
document.getElementById('now').innerHTML = now;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>
<strong>Should Show: 2022-05-25</strong>
<div id="now"></div>

Your format is ISO8601 format, you could check from here: https://en.wikipedia.org/wiki/ISO_8601

You can try to send the full format to the function

const date = "2022-05-25T15:48:50.000Z"
var now = moment.utc(date,"YYYY-MM-DD\THH:mm:ss\Z").format("YYYY-MM-DD");
document.getElementById('now').innerHTML = now;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>
<strong>Should Show: 2022-05-25</strong>
<div id="now"></div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文