Split date in javascript

How to split next date in javascript,

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

      

Thanks..

+2


a source to share


2 answers


var date = "2002-01-01";

      

Any of the following will give you what you want:



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

  • date = date.split("-").join("");

+6


a source


If it's 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();

      

+4


a source







All Articles