Keywords: JavaScript | Date Handling | Day Names | Month Names | Array Mapping
Abstract: This article provides a comprehensive exploration of various methods to retrieve day and month names in JavaScript, focusing on the core array mapping solution while comparing native Date object methods with third-party libraries. Through complete code examples, it demonstrates implementations ranging from basic to advanced prototype extensions, and introduces internationalization formatting alternatives, offering developers complete date processing solutions.
Fundamentals of JavaScript Date Handling
Date and time processing is a common requirement in web development. JavaScript provides built-in Date objects for handling date and time operations, but the methods directly return numeric values rather than human-readable string names.
Core Problem Analysis
The user needs to format dates into readable formats like "Printed on Thursday, 27 January 2011 at 17:42:21". The key challenge lies in converting the 0-6 number returned by getDay() into day names, and the 0-11 number returned by getMonth() into month names.
Array Mapping Solution
The most direct and effective method is using arrays for mapping conversion. This approach doesn't rely on external libraries and the code is concise and easy to understand:
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var now = new Date();
var day = days[now.getDay()];
var month = months[now.getMonth()];
var h = now.getHours();
var m = now.getMinutes();
var s = now.getSeconds();
function checkTime(i) {
return (i < 10) ? "0" + i : i;
}
h = checkTime(h);
m = checkTime(m);
s = checkTime(s);
var prnDt = "Printed on " + day + ", " + now.getDate() + " " + month + " " + now.getFullYear() + " at " + h + ":" + m + ":" + s;
Prototype Extension Advanced Usage
For projects that require frequent use of date names, extending Date.prototype can improve code readability and reusability:
(function() {
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
Date.prototype.getMonthName = function() {
return months[this.getMonth()];
};
Date.prototype.getDayName = function() {
return days[this.getDay()];
};
})();
var now = new Date();
var day = now.getDayName();
var month = now.getMonthName();
Internationalization Formatting Solution
As an alternative approach, JavaScript's Internationalization API can be used for date formatting, supporting multiple language environments:
var options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
};
var prnDt = 'Printed on ' + new Date().toLocaleDateString('en-us', options);
Comparison of Related Date Functions
In databases and other programming languages, date processing functions are typically more comprehensive. Referring to SQL date functions, we can see similar concepts:
DAYOFWEEK- Returns the numeric representation of the day of week (0-7)MONTH- Returns the numeric representation of the month (1-12)YEAR- Returns the yearDAYOFYEAR- Returns the day of the year
These functions are conceptually similar to JavaScript Date methods, but implementation details and return value ranges may differ. For example, SQL's DAYOFWEEK might return 1-7, while JavaScript's getDay() returns 0-6.
Performance and Compatibility Considerations
The array mapping method has good compatibility across all modern browsers with excellent performance. The prototype extension method improves code readability but should be used cautiously in large projects to avoid conflicts with other libraries. The Internationalization API method provides better localization support but may require polyfills in older browser versions.
Best Practice Recommendations
For most projects, the array mapping method is recommended due to its simplicity, efficiency, and maintainability. For projects requiring multi-language support, the Internationalization API should be considered. The prototype extension method is suitable for use in well-encapsulated libraries.
Regardless of the chosen method, ensure code readability and maintainability, with appropriate comments explaining date processing logic, especially when dealing with time zones and localization.