Creating dynamic date using javascript where the date is in single digit should append a "0" before them -
Creating dynamic date using javascript where the date is in single digit should append a "0" before them -
i creating javascript dynamic date, code below.
html:<span id="spandate"></span>
javascript: var months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']; var tomorrow = new date(); tomorrow.settime(tomorrow.gettime() + (1000 * 3600 * 24)); document.getelementbyid("spandate").innerhtml = tomorrow.getdate() + "/" + months[tomorrow.getmonth()] + "/" + tomorrow.getfullyear();
fiddle i getting date printed, when comes single digit days, need add together '0'
looks symmetrical. instance, getting 4/11/2014
, 04/11/2014
. how can date 2 digits additional 0 in case of single digit?
thanks.
('00' + tomorrow.getdate()).slice(-2)
will want (slice negative index, start end).
also, can have tomorrow
using date constructor directly:
var tomorrow = new date(date.now() + (1000 * 3600 * 24)); // date.now() returns current date in millisenconds
demo: jsfiddle
javascript
Comments
Post a Comment