0

I have a very simple query which i am not getting how to correct this format structure.

I have a string as follows

var date = "11/21/2016'

I need to change it to this format

var date = '20161122'

Any idea how to achieve this

5
  • 1
    Do you want to do this in pure JS or using library is fine? Commented Nov 23, 2016 at 1:48
  • pure js would be fine... any suggestions ? Commented Nov 23, 2016 at 1:59
  • 1
    Will it always follow that (01/01/2016) or can it be like 1/1/16? Commented Nov 23, 2016 at 1:59
  • 1
    Possible duplicate of How to format a JavaScript date Commented Nov 23, 2016 at 2:03
  • 1
    You should use a date library like Moment.js or Date.js. if you cant then you can do something like this var dt = new Date(date); '' + dt.getFullYear() + (dt.getMonth() + 1) + dt.getDate() Commented Nov 23, 2016 at 2:07

2 Answers 2

2

I'd like to suggest moment.js:

moment('11/21/2016', 'MM/DD/YYYY').format('YYYYMMDD')
Sign up to request clarification or add additional context in comments.

Comments

1

If you do not want to use a library:

var parts = date.split("/");
var finalDate = parts[2] + parts[0] + parts[1];

The finalDate is the answer.

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.