Alex Rivera | Logout

Adding 0 to months in jQuery date

Asked 2011-08-11T00:36:02.773
13

I have a jQuery script which returns the current date into a text box. However, when a month with only one digit is displayed (e.g. 8) I want it to place a 0 before it (e.g. 08) and not place a 0 before it if it has 2 digits (e.g. 11).

How can I do this? I hope you can understand my question. Here is my jQuery code:

var myDate = new Date();
var prettyDate =myDate.getDate() + '' + (myDate.getMonth()+1) + '' + myDate.getFullYear();
$("#date").val(prettyDate);
Edit
Report

1 Answer

0

There are a number of JS libraries that provide good string formatting. This one does it c# style. http://www.masterdata.dyndns.org/r/string_format_for_javascript/

answered 2011-08-11T00:41:28.797

Your Answer