w3resource

JavaScript: Get a numeric representation of a month

JavaScript Datetime: Exercise-26 with Solution

Write a JavaScript function to get a numeric representation of a month, with leading zeros (01 through 12).

Test Data:
dt = new Date(2015, 10, 1);
console.log(numeric_month(dt));
"11"

Sample Solution:

JavaScript Code:

// Define a JavaScript function called numeric_month with parameter dt (date)
function numeric_month(dt)
{
  // Return the month in numeric format, ensuring that months below October are prefixed with '0'
  return (dt.getMonth() < 9 ? '0' : '') + (dt.getMonth() + 1); 
}

// Create a new Date object representing the current date
dt = new Date(); 
// Output the numeric representation of the month for the current date
console.log(numeric_month(dt)); 

// Create a new Date object representing November 1, 2015
dt = new Date(2015, 10, 1); 
// Output the numeric representation of the month for November 1, 2015
console.log(numeric_month(dt));

Output:

06
11

Explanation:

In the exercise above,

  • The code defines a JavaScript function named "numeric_month()" with one parameter 'dt', representing a Date object.
  • Inside the numeric_month function:
    • It retrieves the month index from the provided Date object "dt" using the "getMonth()" method. Note that the month index is zero-based (0 for January, 1 for February, ..., 11 for December).
    • It checks if the month index is less than 9 (indicating October or earlier) to determine whether the month needs a leading zero.
    • It returns the numeric representation of the month, ensuring that months below October are prefixed with '0' to maintain a two-digit format.
  • The code then demonstrates the usage of the "numeric_month()" function:
    • It creates a new Date object "dt" representing the current date using 'new Date()'.
    • It outputs the numeric representation of the month for the current date by calling the "numeric_month()" function with 'dt' and logging the result to the console.
    • It creates another new Date object 'dt' representing November 1, 2015, using 'new Date(2015, 10, 1)'.
    • It outputs the numeric representation of the month for November 1, 2015, by calling the "numeric_month()" function with 'dt' and logging the result to the console.

Flowchart:

Flowchart: JavaScript- Get a numeric representation of a month

Live Demo:

See the Pen JavaScript - Get a numeric representation of a month-date-ex-26 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus.

Previous: Write a JavaScript function to get a full textual representation of a month, such as January or June.
Next: Write a JavaScript function to get a short textual representation of a month, three letters (Jan through Dec).

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript-exercises/javascript-date-exercise-26.php