w3resource

JavaScript: Get a full textual representation of a month

JavaScript Datetime: Exercise-25 with Solution

Write a JavaScript function to get a full text representation of a month, such as January or June.

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

Sample Solution:

JavaScript Code:

// Define a property 'longMonths' on the Date object to store an array of full month names
Date.longMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

// Define a JavaScript function called full_month with parameter dt (date)
function full_month(dt)
{ 
   // Return the full month name corresponding to the month of the provided date
   return Date.longMonths[dt.getMonth()]; 
}

// Create a new Date object representing the current date
dt = new Date(); 
// Output the full month name for the current date
console.log(full_month(dt)); 

// Create a new Date object representing November 1, 2015
dt = new Date(2015, 10, 1); 
// Output the full month name for November 1, 2015
console.log(full_month(dt));

Output:

June
November

Explanation:

In the exercise above,

  • The code defines a property 'longMonths' on the "Date" object, which is an array containing the full names of months ('January', 'February', ..., 'December').
  • It then defines a JavaScript function named "full_month()" with one parameter 'dt', representing a Date object.
  • Inside the full_month function:
    • It retrieves the month index (0 for January, 1 for February, ..., 11 for December) from the provided Date object "dt" using the "getMonth()" method.
    • It uses this month index as an index to access the corresponding full month name from the 'Date.longMonths' array.
    • It returns the full month name.
  • The code then demonstrates the usage of the "full_month()" function:
    • It creates a new Date object "dt" representing the current date using 'new Date()'.
    • It outputs the full month name for the current date by calling the "full_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 full month name for November 1, 2015, by calling the "full_month()" function with 'dt' and logging the result to the console.

Flowchart:

Flowchart: JavaScript- Get a full textual representation of a month

Live Demo:

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


Improve this sample solution and post your code through Disqus.

Previous: Write a JavaScript function to get ISO-8601 week number of year, weeks starting on Monday.
Next: Write a JavaScript function to get a numeric representation of a month, with leading zeros (01 through 12).

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://w3resource.com/javascript-exercises/javascript-date-exercise-25.php