Hulk
Hulk

Reputation: 34160

Split date in javascript

How to split the following date in javascript,

 var date='2002-01-01';
   The result should be as 20020101

Thanks..

Upvotes: 2

Views: 7353

Answers (2)

Matt
Matt

Reputation: 75307

If it is just a string, you can do:

var date = ("2002-01-02").replace(/-/g, "");

Otherwise:

var now = new Date();
var date = now.getFullYear() + "" + (now.getMonth() + 1) + "" + now.getDate();

Upvotes: 4

Andy E
Andy E

Reputation: 344517

var date = "2002-01-01";

Either of the following will get you what you want:

  • date = date.replace(/-/g, "");
  • date = date.split("-").join("");

Upvotes: 7

Related Questions