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?
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.
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With