Friday, February 23, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  36] [ 4]  / answers: 1 / hits: 5225  / 10 Years ago, thu, july 31, 2014, 12:00:00

Is there a built in way in javascript that formats a date object in a format like “Y-m-d H:i:s”?



I use



var since= new Date(xhr.getResponseHeader(Last-Modified));
alert(modified:: +since.getDate()+'.'+(since.getMonth()+1)+'. '+ since.getHours()+':'+since.getMinutes())


But that cuts off all leading zeros


More From » javascript

 Answers
13

Use this function:



function pad(number) {
if ( number < 10 ) {
return '0' + number;
}
return number;
}

pad(since.getDate())+'.'+pad(since.getMonth()+1)+'. '+ pad(since.getHours())+':'+pad(since.getMinutes())


For Y-m-d H:i:s format use following example:



pad(since.getFullYear()) + '-' + pad(since.getMonth()+1) + '-' + pad(since.getDate()) + ' ' + pad(since.getHours()) + ':' + pad(since.getMinutes()) + ':' + pad(since.getSeconds())

[#43442] Wednesday, July 30, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
giovanny

Total Points: 314
Total Questions: 101
Total Answers: 90

Location: Tajikistan
Member since Thu, Apr 14, 2022
2 Years ago
;