javascript - How to add leading "0" to month/day of date string? -
this question has reply here:
how month , date of javascript in 2 digit format? 13 answersso wrote method takes number , subtracts amount of months current date.
i trying figure out how can add together '0' in front end of months less 10. also, how can add together '0' in front end on days less 10 well.
currently, when returns object (2012-6-9). returns 6 , 9 no '0' in front end of it, can show me how it?
here code
lastnmonths = function(n) { var date = new date(); if (n <= 0) homecoming [date.getfullyear(), date.getmonth() + 1 , date.getdate()].join('-'); var years = math.floor(n/12); var months = n % 12; if (years > 0) date.setfullyear(date.getfullyear() - years); if (months > 0) { if (months >= date.getmonth()) { date.setfullyear(date.getfullyear()-1 ); months = 12 - months; date.setmonth(date.getmonth() + months ); } else { date.setmonth(date.getmonth() - months); } } } homecoming [date.getfullyear(), date.getmonth() + 1, date.getdate()].join('-'); };
you can avoid testing if n < 10 using :
("0" + (yourdate.getmonth() + 1)).slice(-2)
javascript jquery date datepicker
No comments:
Post a Comment