Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
192
rated 0 times [  193] [ 1]  / answers: 1 / hits: 20134  / 8 Years ago, mon, march 7, 2016, 12:00:00

Desired return value should be a string formatted as dd-mm-yyyy.



Im trying to give a format date dd-mm-yyyy to ISOString and adding GMT but the code gives me this format. How can i do?



new Date().toISOString()
.replace(/T/, ' '). // replace T with a space
.replace(/..+/, ''); // delete the dot and everything after



'2012-11-04 14:55:45'



More From » date

 Answers
12

im looking for 04-11-2012 date format




Using today's date (which as an ISO string is currently 2016-03-08T13:51:13.382Z), you can do this:



new Date().toISOString().replace(/T.*/,'').split('-').reverse().join('-')


The output of this is:



-> 08-03-2016


This:




  1. Grabs the date.

  2. Converts it to an ISO string.

  3. Replaces the 'T' and everything after it.

  4. Converts it into an array by splitting on any hyphen ('-') character. ([2016, 03, 08])

  5. Reverses the order of the array. ([08, 03, 2016])

  6. Joins the array back as a string, separating each value with a hyphen character.






Here is a demo using your date (2012-11-04T14:55:45.000Z) as input:





var input = 2012-11-04T14:55:45.000Z,
output;

output = new Date(input).toISOString().replace(/T.*/,'').split('-').reverse().join('-');

document.getElementById('input').innerHTML = input;
document.getElementById('output').innerHTML = output;

<p><strong>Input:</strong> <span id=input></span></p>
<p><strong>Output:</strong> <span id=output></span></p>




[#63029] Thursday, March 3, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kiley

Total Points: 733
Total Questions: 118
Total Answers: 94

Location: Liechtenstein
Member since Wed, Dec 8, 2021
3 Years ago
;