admin管理员组文章数量:1296877
I'm struggling to make a extension on the date object to return the current month in name. It should return (January) if the method d.getMonthText() is called.
I've searched for extension online but I can't seem to figure it out.
I'm struggling to make a extension on the date object to return the current month in name. It should return (January) if the method d.getMonthText() is called.
I've searched for extension online but I can't seem to figure it out.
Share Improve this question asked Jan 11, 2017 at 13:35 MaximMaxim 971 silver badge9 bronze badges6 Answers
Reset to default 8Just map date.getMonth()
method value to month values
Date.prototype.getMonthText = function() {
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
return months[this.getMonth()];
}
var now = new Date();
var month = now.getMonthText();
console.log(month);
Although not remended, you can add methods to almost any javascript built–in object through its prototype property.
Date.prototype.getMonthName = function() {
let months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];
return months[this.getMonth()];
};
Then just use it:
let date = new Date();
let month = date.getMonthName();
Just to add an option, you might consider toLocaleString which should support the ECMA 402 internationalisation API. It's not ubiquitous yet but worth considering for the future as it will return the month name in any language using the ISO 639-1 language codes:
Date.prototype.getMonthName = function(lang) {
// Default language is English
lang = lang || 'en-GB';
return this.toLocaleString(lang, {month:'long'});
}
console.log(new Date().getMonthName()) // English (default)
console.log(new Date().getMonthName('ar-sy')) // Arabic
console.log(new Date().getMonthName('es-py')) // Spanish
there you go in a very basic way.. This function will return you the name of Month
<script>
function getMonthText(){
var month;
switch (new Date().getMonth()) {
case 0:
month= "Jan";
break;
case 1:
month= "Feb";
break;
case 2:
month= "March";
break;
case 3:
month= "April";
break;
case 4:
month= "May";
break;
case 5:
month= "June";
break;
case 6:
month= "July";
break;
case 7:
month= "August";
break;
case 8:
month= "Sep";
break;
case 9:
month= "Oct";
break;
case 10:
month= "Nov";
break;
case 11:
month= "Dec";
break;
}}
</script>
ECMAScript 6 version :
getPreviousMonth(monthBefore) {
let monthList = [];
let date = new Date();
for (let i = 0; i < monthBefore; i++) {
date.setMonth(date.getMonth() - 1);
monthList.push(date.toLocaleString('en-us', {
month: 'long',
}));
}
return monthList;
}
You could monkey-patch the Date prototype object and implement an extension method:
Date.prototype.getMonthName = function() {
return "January,February,March,April,May,June,July,August,September,October,November,December".split(",")[this.getMonth()];
}
本文标签: Date extension javascript get month nameStack Overflow
版权声明:本文标题:Date extension javascript get month name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741640185a2389871.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论