Reputation: 5622
I am using the following to get the current date:-
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1;
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var newdate = day + "/" + month + "/" + year;
If I alert(newdate);
it shows:-
3/06/2013
Is there any way I can display this as:-
03/06/2013
Upvotes: 0
Views: 121
Reputation: 31072
Please use the following:
var dateObj = new Date();
var month = ('0' + (dateObj.getUTCMonth() + 1) ).slice( -2 );;
var day = ('0' + (dateObj.getUTCDate() + 1) ).slice( -2 );
var year = dateObj.getUTCFullYear();
var newdate = day + "/" + month + "/" + year;
Upvotes: 0
Reputation: 253506
If you want to avoid using a library, and don't mind an extra line in your JavaScript:
var day = dateObj.getUTCDate(),
dd = parseInt(day, 10) < 10 ? '0' + day : day;
Upvotes: 2
Reputation: 41605
You can split it and create your own format:
var splitTime = newDate.split("/");
var day = splitTime[0];
var month = splitTime[1];
var year = splitTime[2];
if (day < 10){
day = "0" + day;
}
var myDate = day + '/' + month + '/' + year;
Living demo: http://jsfiddle.net/rtqpp/
Upvotes: 0
Reputation: 16089
function padWithZeroes(number, width)
{
while (number.length < width)
number = '0' + number;
return number;
}
Now call day = padWithZeroes(day, 2)
(and likewise for the month) before you use it.
Upvotes: 0
Reputation: 100381
With plain Javascript, only manually
if (day < 10) day = "0" + day;
if (month < 10) month = "0" + month;
Upvotes: 3
Reputation: 15170
Using JQuery DateFormat:
$.format.date(dateObj.toString(), "dd/MM/yyyy");
Upvotes: 1