I'm a total newbie and I'm confused about this tutorial. I know (at least I think I know) that the functions getDate and get Month and getFullYear are pre-set functions determined by JavaScript.
Why are those preset functions necessary for this tutorial if a new Date (2000,0,1) is being submitted as an argument to formatDate? Does getDate clash somehow with the numbers submitted as an argument?
In function pad, I understand that "number" checks to see whether a number is less than 10 and, if so, adds a zero, but what is the argument submitted to function pad? How does it get the numbers to check?
Can you please take me through (using plain language) this tutorial step by step...thank you 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 and prints the date.
The pad function is to add a "0" (not to add 10 as you noted) in front of a number that is less than 10.
This allows you to print a date in the dd/mm/yyyy format. Eg. Feb 3, 2011 will be printed as 03/02/2011 instead of 3/2/2011
Within the formatDate function, the last line is return pad(.... the "pad" function in the "formatDate" function takes each date part and sends it to the padding function to prepend a "0" to ensure the mm/dd is followed instead of possibly sending a single digit variable - such as 3/2/2011
function formatDate(date) { // date is passed here
function pad(number) { // note that this function is defined within the outer function
if (number < 10)
return "0" + number; // prepend a 0 if number is less than 10
else
return number; // if number is greater than 10, no prepending necessary
} // the pad function ends here
return pad(date.getDate()) + "/" + pad(date.getMonth() + 1) +
"/" + date.getFullYear(); // note how the pad is used around the "date" and "month" only
} // the formatDate function ends here
print(formatDate(new Date(2000, 0, 1)));
Hope that helps clarify things.
This line:
return pad(date.getDate()) + "/" + pad(date.getMonth() + 1) +
"/" + date.getFullYear();
gets converted to (assuming a date of 23/2/2011)
return pad(23) + "/" + pad(1 + 1) + "/" + 2011;
This calls pad(23) - and the 23 is substituted into the "number" variable in the pad function. No change is required and 23 is returned.
pad(1+1) = pad(2) - and 2 is substituted into the "number" variable in the pad function. It appends a "0" and returns "02"
So, the final conversion is
return 23 + "/" + 02 + "/" + 2011;
and it ends up printing "23/02/2011".
This is a tutorial about how to display a date in DD/MM/YYYY format. It creates a date, then submits it to the function formatDate
which returns it in that format, and then prints it to the screen.
formatDate has an internal function called "pad." This makes sure that there are a sufficient number of zeroes at the beginning of each item. For example, if the day is Jan 1, 2000, then the programmer wants 01/01/2000. pad
adds the zeroes to the month and day because they're both less than 10. It adds 1 to getMonth
because months are zero-indexed in Javascript date objects: 0 is January, 1 is February, etc.
getDate
, getMonth
, and getFullYear
are object methods of the Date
class. If you're new to object-oriented programming, I'd suggest you start by exploring that to understand objects. Roughly, an object is a datatype that also has methods for manipulating that data. Date
objects have getDate
, getMonth
, and getFullYear
object methods for returning those parts of the date.
This code is fine as is, but if you're trying to do more sophisticated date parsing, you might want to check out the date.js library.
The last line says this:
print(formatDate(new Date(2000, 0, 1)));
It has parentheses so you need to read it backwards like so:
(2000, 0, 1) // make a list of 3 numbers to pass to a function
Date(2000, 0, 1) // call the Date function
new Date(2000, 0, 1) // since Date is a builtin object, tell it that is is constructing a new object or you risk hard to find bugs
(new Date(2000, 0, 1)) // pass the new Date object to the formatDate function
print(formatDate(new Date(2000, 0, 1))); // pass the result of formatDate, a String object, to the print function
You will notice that the formatDate function never sees the three numbers therefore it has to use things like getMonth to decode the parts.
精彩评论