Please explain this Javascript tutorial -
i'm total newbie , i'm confused tutorial. know (at least think know) functions getdate , month , getfullyear pre-set functions determined javascript.
why preset functions necessary tutorial if new date (2000,0,1) being submitted argument formatdate? getdate clash somehow numbers submitted argument?
in function pad, understand "number" checks see whether number less 10 and, if so, adds zero, argument submitted function pad? how numbers check?
can please take me through (using plain language) tutorial step step...thank in advance
function formatdate(date) { function pad(number) { if (number < 10) return "0" + number; else return number; } return pad(date.getdate()) + "/" + pad(date.getmonth() + 1) + "/" + date.getfullyear(); } print(formatdate(new date(2000, 0, 1)));
this function formats , prints date.
the pad function add "0" (not add 10 noted) in front of number less 10.
this allows print date in dd/mm/yyyy format. eg. feb 3, 2011 printed 03/02/2011 instead of 3/2/2011
within formatdate function, last line return pad(.... "pad" function in "formatdate" function takes each date part , sends padding function prepend "0" ensure mm/dd followed instead of possibly sending single digit variable - such 3/2/2011
function formatdate(date) { // date passed here function pad(number) { // note function defined within outer function if (number < 10) return "0" + number; // prepend 0 if number less 10 else return number; // if number greater 10, no prepending necessary } // pad function ends here return pad(date.getdate()) + "/" + pad(date.getmonth() + 1) + "/" + date.getfullyear(); // note how pad used around "date" , "month" } // formatdate function ends here print(formatdate(new date(2000, 0, 1)));
hope helps clarify things.
this line:
return pad(date.getdate()) + "/" + pad(date.getmonth() + 1) + "/" + date.getfullyear();
gets converted (assuming date of 23/2/2011)
return pad(23) + "/" + pad(1 + 1) + "/" + 2011;
this calls pad(23) - , 23 substituted "number" variable in pad function. no change required , 23 returned.
pad(1+1) = pad(2) - , 2 substituted "number" variable in pad function. appends "0" , returns "02"
so, final conversion
return 23 + "/" + 02 + "/" + 2011;
and ends printing "23/02/2011".
Comments
Post a Comment