Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert mm/dd/yyyy to yyyy-mm-dd

I need to convert a date like this, 03/07/2016 to a date format like, 2016-03-07.

How can I do this using javascript or jquery?

like image 438
Jordan Davis Avatar asked Mar 07 '16 23:03

Jordan Davis


People also ask

How do I change the date format from dd mm yyyy to dd mm yyyy?

Press Ctrl+1 to open the Format Cells dialog. On the Number tab, select Custom from the Category list and type the date format you want in the Type box. Click OK to save the changes.

How do I change date format from mm DD to YYYY?

Press CTRL+1. In the Format Cells box, click the Number tab. In the Category list, click Date, and then choose a date format you want in Type.


1 Answers

Assuming your input is a string, this is easy to do using a regular expression with the String .replace() method:

var input = "03/07/2016";
var output = input.replace(/(\d\d)\/(\d\d)\/(\d{4})/, "$3-$1-$2");

Actually, if the input format is guaranteed, you could just swap the pieces around based on their position without bothering to explicitly match digits and forward slashes:

var output = input.replace(/(..).(..).(....)/, "$3-$1-$2");
like image 62
nnnnnn Avatar answered Oct 04 '22 06:10

nnnnnn