如何将用户日期转换为毫秒

发布于 2025-01-22 00:52:24 字数 519 浏览 2 评论 0原文

目前,我正在使用这种较长的方法将用户输入格式转换为:dd.mm.yy hh:mm将其转换为毫秒。

let day = date.substring(0, 2);
let mth = date.substring(3, 5);
let yr = date.substring(6, 10);
let hr = date.substring(11, 13);
let min = date.substring(14, 16);

let d = yr + "-" + mth + "-" + day + "T" + hr + ":" + min + ":00.000+02:00";
d = new Date(d).getTime();

是否有更轻松,更可取的方法来执行此操作?我已经读过有关ISO日期和其他规范的信息,但是我还没有找到任何包括日/月/年/小时/分钟的东西,而没有不必要的毫秒和时区。

我不是JS日期的专家,所以请理解。先感谢您。

For now I'm using this long method to convert user input in format: DD.MM.YY HH:mm to milliseconds.

let day = date.substring(0, 2);
let mth = date.substring(3, 5);
let yr = date.substring(6, 10);
let hr = date.substring(11, 13);
let min = date.substring(14, 16);

let d = yr + "-" + mth + "-" + day + "T" + hr + ":" + min + ":00.000+02:00";
d = new Date(d).getTime();

Is there any easier, preferable way to do this? I've read about ISO dates and other norms but I haven't found anything that includes both day/month/year and hours/minutes without unnecessary milliseconds and time zones.

I'm not expert in js dates so be understanding. Thank you in advance.

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

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

发布评论

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

评论(1

醉生梦死 2025-01-29 00:52:24

您可以尝试date.parse解析日期字符串。 MDN网站上有一个完整的文章,网址为: https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/date/date/parse/parse

You can try Date.parse which parses a date string. The MDN website has a full write-up on it at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

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