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

Adu Rao
  • 101
  • 2
  • 3
  • 10
  • 1
    Do you want to do this in pure JS or using library is fine? – kkkkkkk Nov 23 '16 at 01:48
  • pure js would be fine... any suggestions ? – Adu Rao Nov 23 '16 at 01:59
  • 1
    Will it always follow that (01/01/2016) or can it be like 1/1/16? – DarkHorse Nov 23 '16 at 01:59
  • 1
    Possible duplicate of [How to format a JavaScript date](http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Sachin Nov 23 '16 at 02: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()` – Sachin Nov 23 '16 at 02:07

2 Answers2

2

I'd like to suggest moment.js:

moment('11/21/2016', 'MM/DD/YYYY').format('YYYYMMDD')
Bob Dust
  • 2,092
  • 1
  • 13
  • 11
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.

DarkHorse
  • 963
  • 10
  • 26