1

I have a value returned in a string (numbers separated by commas) and I'd like to make a Date object out of it. It looks like this is not possible, can someone confirm and/or suggest me a solution.
This does not work :

let dateString='2017,3,22,0';
let dateFromString = new Date(dateString);

This works though (when I pass a list of numbers) :

let dateFromString = new Date(2017,3,22,0);

And this works also :

let dateString = '2008/05/10 12:08:20';
let dateFromString = new Date(dateString);

The goal would be to create a Date object from a uniform string. Is that possible ?

Can I create a Date object from a predefined string, which has only one type of separator (comma, colon, slash or whatever) ?

magor
  • 670
  • 10
  • 14
  • what is your actual problem? – Sagar V Mar 28 '17 at 14:11
  • updated question. can i create a date object from a predefined string which uses only one character as separator ? – magor Mar 28 '17 at 14:14
  • And what exactly uniform string do you want to use - `2017,3,22,0` or `2008/05/10 12:08:20`? – hindmost Mar 28 '17 at 14:14
  • I want to avoid using `,` and `:` both as separator. I would like to use only one certain character as separator, either `,`, `:` or `/` or whatever. – magor Mar 28 '17 at 14:17

1 Answers1

1

If your environment is compatible with ES6 (eg. Babel, TypeScript, modern Chrome/Firefox etc), you can use the string's .split(',') and decompose the array into arguments like the following:

const dateString = '2017,3,22,0';
const date = new Date(...dateString.split(','));  // date object for 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 for how the .bind.apply method works with new, you can take a look at Use of .apply() with 'new' operator. Is this possible?

Note: Thanks to the two comments below for spotting my errors

Community
  • 1
  • 1
Liam Gray
  • 1,089
  • 9
  • 16
  • 1
    `const` is not compatible with ES5 – hindmost Mar 28 '17 at 14:17
  • 1
    It should be `new (Function.prototype.bind.apply(Date, [null].concat(dateString.split(','))))` – abhishekkannojia Mar 28 '17 at 14:17
  • 1
    Thanks hindmost and abhishekkannojia – Liam Gray Mar 28 '17 at 14:24
  • I am actually using Typescript + Angular2 and the compiler will compile to ES5...unfortunatelly both of the suggested options are failing. I know I could parse the string and pass to Date what it needs, I was just wondering if there is a simple solution like using `split()`. – magor Mar 28 '17 at 14:41
  • How is it failing? The TypeScript compilation, or type checking? It appears to compile fine on http://www.typescriptlang.org/play/ – Liam Gray Mar 28 '17 at 14:48
  • +1, the answer is correct, it is failing only in my dev environment. I'm getting `error TS2345: Argument of type 'string[]' is not assignable to parameter of type 'null[] | null'.`, i guess i have to figure it out – magor Mar 28 '17 at 15:04
  • Maybe using `.split(',').map(i => +i)` to force the resulting array type to be `number[]` rather than `string[]` could work? – Liam Gray Mar 28 '17 at 15:07