2

How to split the following date in javascript,

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

Thanks..

3
  • 2
    jQuery: The future of Javascript. 8-) Commented May 5, 2010 at 8:39
  • 1
    @Matt: lol. This sort of stuff always reminds me of bobince's jQuery rant - doxdesk.com/updates/2009.html#u20091116-jquery Commented May 5, 2010 at 8:41
  • @Matt jQuery: the history of javascript. 8-) Commented Mar 1, 2021 at 20:28

2 Answers 2

7
var date = "2002-01-01";

Either of the following will get you what you want:

  • date = date.replace(/-/g, "");
  • date = date.split("-").join("");
Sign up to request clarification or add additional context in comments.

Comments

4

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.