Adu Rao
Adu Rao

Reputation: 101

change format in date (Javascript)

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

Upvotes: 0

Views: 49

Answers (2)

DarkHorse
DarkHorse

Reputation: 973

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.

Upvotes: 1

Bob Dust
Bob Dust

Reputation: 2460

I'd like to suggest moment.js:

moment('11/21/2016', 'MM/DD/YYYY').format('YYYYMMDD')

Upvotes: 2

Related Questions