Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  183] [ 5]  / answers: 1 / hits: 51667  / 10 Years ago, wed, october 22, 2014, 12:00:00

I have a string of a date in javascript in format #1. I need to convert it to format #2.



The problem starts when one format is dd/mm/yy and the other is mm/dd/yy.



The formats change dynamically and I have the formats as strings, but I need a function like



   Date newDate = convert(currentDate, currentFormatString, newFormatString).


How can I do it?


More From » date

 Answers
22

You should look into momentjs, which is a javascript date/time library. With that, you can easily convert between dates of different format. In your case, it would be:


string newDate = moment(currentDate, currentFormatString).format(newFormatString)


For example, moment("21/10/14", "DD/MM/YY").format("MM/DD/YY") would return "10/21/14"


[#69049] Monday, October 20, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
minab

Total Points: 701
Total Questions: 104
Total Answers: 91

Location: Saint Pierre and Miquelon
Member since Fri, Jan 28, 2022
2 Years ago
;