[英]Convert From One date format to other gives invalid date
I want to convert a date from 18/04/2019 13:17:41
to 18-04-2019 13:17:41
ie from DD/MM/YYYY HH:MM:SS
to DD-MM-YYYY HH:MM:SS
. 我想将日期从
18/04/2019 13:17:41
转换为18-04-2019 13:17:41
即从DD/MM/YYYY HH:MM:SS
为DD-MM-YYYY HH:MM:SS
。 I have tried moment js and Date function ()
but they either return null or invalid date ,where I am wrong here ? 我尝试了一下js和
Date function ()
但是它们要么返回null或无效的日期,我在这里错了吗? Any help will be appreciated 任何帮助将不胜感激
let str="18/04/2019 13:17:41"; let strf=str.replace(/[/]/g,"-"); console.log(strf) let date=moment(strf,"DD-MM-YYYY HH:MM:SS").format("DD-MM-YYYY HH:MM:SS"); console.log(date) console.log(new Date("18/04/2019 13:17:41")) // gives null console.log(new Date("18-04-2019 13:17:41")) // gives null
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>
js Date
use a YYYY-MM-DD
format js
Date
使用YYYY-MM-DD
格式
let str="2019/04/18 13:17:41"; console.log(new Date(str)) // works
you can use something like this to convert from DD/MM/YYYY
to YYYY/MM/DD
but the best would be to get the date in the right format from the start 您可以使用类似的方法将
DD/MM/YYYY
转换为YYYY/MM/DD
但最好的方法是从开始就以正确的格式获取日期
let str="18/04/2019 13:17:41"; str = str.split(" ").map(part => part.split("/").reverse().join("/")).join(" ") console.log(str) console.log(new Date(str))
Your trying to convert string of format "DD-MM-YYYY HH:mm:ss" to moment date. 您试图将格式“ DD-MM-YYYY HH:mm:ss”的字符串转换为时刻日期。 Which is not supported by momentjs.
moment.js不支持该功能。 Here are supported string formats which you can convert to moment date.
这里是支持的字符串格式,您可以将其转换为时刻日期。
2013-02-08 # A calendar date part
2013-W06-5 # A week date part
2013-039 # An ordinal date part
20130208 # Basic (short) full date
2013W065 # Basic (short) week, weekday
2013W06 # Basic (short) week only
2013050 # Basic (short) ordinal date
2013-02-08T09 # An hour time part separated by a T
2013-02-08 09 # An hour time part separated by a space
2013-02-08 09:30 # An hour and minute time part
2013-02-08 09:30:26 # An hour, minute, and second time part
2013-02-08 09:30:26.123 # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000 # hour 24, minute, second, millisecond equal 0 means next day at midnight
20130208T080910,123 # Short date and time up to ms, separated by comma
20130208T080910.123 # Short date and time up to ms
20130208T080910 # Short date and time up to seconds
20130208T0809 # Short date and time up to minutes
20130208T08 # Short date and time, hours only
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.