How can I convert string to datetime with format specification in JavaScript?

前端 未结 15 1054
终归单人心
终归单人心 2020-11-22 02:35

How can I convert a string to a date time object in javascript by specifying a format string?

I am looking for something like:

var dateTime = convert         


        
15条回答
  •  星月不相逢
    2020-11-22 03:22

    Date.parse() is fairly intelligent but I can't guarantee that format will parse correctly.

    If it doesn't, you'd have to find something to bridge the two. Your example is pretty simple (being purely numbers) so a touch of REGEX (or even string.split() -- might be faster) paired with some parseInt() will allow you to quickly make a date.

提交回复
热议问题