Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
134
rated 0 times [  140] [ 6]  / answers: 1 / hits: 51936  / 12 Years ago, thu, november 1, 2012, 12:00:00


how can I get the date in this format [mm/dd/yy] using javascript. I am struggling to get the 'year' to a 2 digit figure as opposed to the full 4 digits.

Thanks!


More From » date

 Answers
16

Try this:



HTML



<div id=output></div>


JS



(function () {
// Get current date
var date = new Date();

// Format day/month/year to two digits
var formattedDate = ('0' + date.getDate()).slice(-2);
var formattedMonth = ('0' + (date.getMonth() + 1)).slice(-2);
var formattedYear = date.getFullYear().toString().substr(2,2);

// Combine and format date string
var dateString = formattedMonth + '/' + formattedDate + '/' + formattedYear;

// Reference output DIV
var output = document.querySelector('#output');

// Output dateString
output.innerHTML = dateString;
})();


Fiddle: http://jsfiddle.net/kboucher/4mLe1Lrd/


[#82241] Wednesday, October 31, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jamir

Total Points: 736
Total Questions: 97
Total Answers: 101

Location: Cayman Islands
Member since Fri, Mar 4, 2022
2 Years ago
jamir questions
;