How to create a new Date() in Javascript from a non-standard date format

前端 未结 6 2097
有刺的猬
有刺的猬 2020-12-24 13:47

I have a date in this format: dd.mm.yyyy

When I instantiate a JavaScript date with it, it gives me a NaN

In c# I can specify a date format, to s

6条回答
  •  Happy的楠姐
    2020-12-24 14:12

    It's easy enough to split the string into an array and pass the parts directly to the Date object:

    var str = "01.01.2010";
    var dmy = str.split(".");
    
    var d = new Date(dmy[2], dmy[1] - 1, dmy[0]);
    

提交回复
热议问题