create JavaScript Date object from date string

后端 未结 2 787
暗喜
暗喜 2021-01-18 08:34

I would like to create a valid JavaScript Date object from the following string \"010-10-25T23:25:55.847Z\".

This string comes out of a PostGIS database \"timestamp

2条回答
  •  孤城傲影
    2021-01-18 09:00

    I'm not sure what that format it, but this will give you each number:

    var results = "010-10-25T23:25:55.847Z".match(/\d+\.{0,1}\d+/g);
    var year = results[0]; // maybe ?
    var month = results[1];
    var day = results[2];
    var etc...;
    new Date(year, --month, day, hour, minutes, seconds);
    

    or if it's kinda like UTC,

    new Date(Date.UTC.apply(this, "010-10-25T23:25:55.847Z".match(/\d+\.{0,1}\d+/g)))
    

提交回复
热议问题