Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  109] [ 2]  / answers: 1 / hits: 23331  / 8 Years ago, mon, march 7, 2016, 12:00:00

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?


More From » jquery

 Answers
12

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

[#63018] Friday, March 4, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zaynerogerb

Total Points: 454
Total Questions: 109
Total Answers: 97

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
zaynerogerb questions
;