Javascript - reformat date string to ISO8601(Javascript - 将日期字符串重新格式化为 ISO8601)
问题描述
我有一个这样的字符串:
I have a string like this:
21.03.2016 23:59
我需要将此字符串转换为 ISO-8601 日期时间字符串:
And I need this string converted into a ISO-8601 date-time string:
YYYY-MM-DDTHH:mm:ss+00:00
有没有简单的方法来转换这个日期?我尝试使用 moment.js,但找不到解析现有日期的函数.
Is there a simple way to convert this date? I try it whit moment.js but i can't find a function to parse an existing date.
推荐答案
你也可以不使用 moment.js 来做到这一点.看代码如下:
You can also do this without using moment.js. Look code as following:
(new Date("03.21.2016 23:59")).toISOString()
只需将字符串 21.03.2016 23:59 (dd-mm-yyyy) 更改为 03.21.2016 23:59 (mm-dd-yyyy).您可以通过拆分日期并更改拆分部分的顺序轻松做到这一点.
just you need to change your string 21.03.2016 23:59 (dd-mm-yyyy) to 03.21.2016 23:59 (mm-dd-yyyy). You can easily do this by split the date and change the order of split part.
如果您不想这样做,那么只需按照 matthias 的回答使用 moment.js.
And if you dont want to do this then simply use moment.js as per matthias's answer.
这篇关于Javascript - 将日期字符串重新格式化为 ISO8601的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Javascript - 将日期字符串重新格式化为 ISO8601


基础教程推荐
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01