将日期时间字符串转换为 utc

发布于 2025-01-16 23:57:47 字数 304 浏览 1 评论 0原文

我有一个输入时间设置,如下所示。

{
    "effectiveFrom": "2022-03-24 06:28",
}

我需要将其转换为 UTC 格式,就像这样

{
       "effective_from": "2022-03-24T00:58:00.000Z",
}

我该怎么做,我在这里使用此代码,但这没有按预期工作任何帮助都很棒。

新日期(ctx.params.dateTime);

I have an input time setting which is like this.

{
    "effectiveFrom": "2022-03-24 06:28",
}

I need to convert the same to UTC format which is like this

{
       "effective_from": "2022-03-24T00:58:00.000Z",
}

How can i do that I am using this code over here but this is not working as expected any help is great.

new Date(ctx.params.dateTime);

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

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

发布评论

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

评论(2

筱武穆 2025-01-23 23:57:47

toISOString() 方法返回一个字符串
简化的扩展 ISO 格式 (ISO 8601),始终为 24 或 27
字符长
(YYYY-MM-DDTHH:mm:ss.sssZ
或者
±YYYYYY-MM-DDTHH:mm:ss.sssZ,
分别)。时区始终为零 UTC 偏移量,如后缀所示
“Z”。

来源:MDN 网络文档

console.log(new Date('2022-03-24 06:28').toISOString())

.toISOString() 将为您提供您所请求的格式。但是您无法对其执行任何日期操作,因为您得到的输出是一个字符串。

我刚刚创建了一个简单的节点文件并检查你哪里做错了。

var http = require("http");
const Sequelize = require("sequelize");
const sequelize = new Sequelize("test", "postgres", "postgres", {
  host: "localhost",
  port: 5431,
  dialect: "postgres",
  options: {
    encrypt: false,
  },
});

const UserProfile = sequelize.define("userProfile", {
  name: {
    type: Sequelize.STRING,
  },
  DateTimeCol: {
    type: Sequelize.DATE,
  },
  TimeCol: {
    type: Sequelize.TIME,
  },
});

userProfile.sync().then(() => {
  userProfile.create({
    name: "Test User",
    DateTimeCol: "2022-03-24T08:00:00.000Z",
    TimeCol: "13:00",
  });
  userProfile.findAll().then((data) => console.log("user response", data));
});

var server = http.createServer(function (req, res) {
  res.writeHead(200);
  res.end();
});
server.listen(8085);

顺便说一句,我使用了sequelize和postgres进行了测试。

The toISOString() method returns a string in
simplified extended ISO format (ISO 8601), which is always 24 or 27
characters long
(YYYY-MM-DDTHH:mm:ss.sssZ
or
±YYYYYY-MM-DDTHH:mm:ss.sssZ,
respectively). The timezone is always zero UTC offset, as denoted by the suffix
"Z".

Source: MDN Web Docs

console.log(new Date('2022-03-24 06:28').toISOString())

.toISOString() will give you the format you are requesting. But you can't perform any date operations on that since the output you get is a string.

I just created a simple node file and check where you are doing wrong.

var http = require("http");
const Sequelize = require("sequelize");
const sequelize = new Sequelize("test", "postgres", "postgres", {
  host: "localhost",
  port: 5431,
  dialect: "postgres",
  options: {
    encrypt: false,
  },
});

const UserProfile = sequelize.define("userProfile", {
  name: {
    type: Sequelize.STRING,
  },
  DateTimeCol: {
    type: Sequelize.DATE,
  },
  TimeCol: {
    type: Sequelize.TIME,
  },
});

userProfile.sync().then(() => {
  userProfile.create({
    name: "Test User",
    DateTimeCol: "2022-03-24T08:00:00.000Z",
    TimeCol: "13:00",
  });
  userProfile.findAll().then((data) => console.log("user response", data));
});

var server = http.createServer(function (req, res) {
  res.writeHead(200);
  res.end();
});
server.listen(8085);

By the way, I have used sequelize and postgres for testing.

梦年海沫深 2025-01-23 23:57:47

当您使用 new Date(ctx.params.dateTime) 解析日期字符串时,您将得到一个 返回日期对象

要获取 UTC 字符串作为输出,您可以调用 该对象上的toISOString() 方法。

const dt = new Date('2022-03-24 06:28')

const isoString = dt.toISOString()

console.log(isoString)

When you parse the date string using new Date(ctx.params.dateTime) you will get a Date object in return.

To get the UTC string as output, you can call the toISOString() method on that object.

const dt = new Date('2022-03-24 06:28')

const isoString = dt.toISOString()

console.log(isoString)

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