如何将用户日期转换为毫秒
目前,我正在使用这种较长的方法将用户输入格式转换为: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试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