Created
August 9, 2021 21:14
-
-
Save herdianf/61292b70683969bb431854391b97c546 to your computer and use it in GitHub Desktop.
constructor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createObject(constructor, argArray) { | |
var args = [null].concat(argArray); | |
var factoryFunction = constructor.bind.apply(constructor, args); | |
return new factoryFunction(); | |
} | |
function parseDate(dateStr) { | |
var args = dateStr.split(/[^0-9]+/).map((number,index) => parseInt(number) - (index === 1 ? 1 : 0))); | |
var dt = createObject(Date, args); | |
return dt; | |
} | |
var date = parseDate('2021-01-01 01:01:01'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment