Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3987250
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:06:41+00:00 2026-05-20T06:06:41+00:00

I’m a total newbie and I’m confused about this tutorial. I know (at least

  • 0

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)));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:06:41+00:00Added an answer on May 20, 2026 at 6:06 am

    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”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.