Can I create a Date object from a predefined string (typescript)? -
i have value returned in string (numbers separated commas) , i'd make date object out of it. looks not possible, can confirm and/or suggest me solution.
not work :
let datestring='2017,3,22,0'; let datefromstring = new date(datestring);
this works though (when pass list of numbers) :
let datefromstring = new date(2017,3,22,0);
and works :
let datestring = '2008/05/10 12:08:20'; let datefromstring = new date(datestring);
the goal create date object uniform string. possible ?
can create date object predefined string, has 1 type of separator (comma, colon, slash or whatever) ?
if environment compatible es6 (eg. babel, typescript, modern chrome/firefox etc), can use string's .split(',')
, decompose array arguments following:
const datestring = '2017,3,22,0'; const date = new date(...datestring.split(',')); // date object 2017/03/22
es5 compatible version:
var datestring = '2017,1,2,0'; var date = new (function.prototype.bind.apply(date, [null].concat(datestring.split(','))));
as how .bind.apply
method works new
, can take @ use of .apply() 'new' operator. possible?
note: 2 comments below spotting errors 👍
Comments
Post a Comment