이 날짜 객체에서 월의 이름 (예 : 10 월 / 10 월)을 JavaScript로 생성하려면 어떻게해야합니까?
var objDate = new Date("10/11/2009");
답변
더 짧은 버전 :
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const d = new Date();
document.write("The current month is " + monthNames[d.getMonth()]);
참고 (2019-03-08)-2009 년에 처음 쓴 저의 답변은 구식입니다. 더 나은 솔루션 은 David Storey의 답변 을 참조하십시오 .
답변
이제 ECMAScript Internationalization API를 사용하여이 작업을 수행 할 수 있습니다.
const date = new Date(2009, 10, 10); // 2009-11-10
const month = date.toLocaleString('default', { month: 'long' });
console.log(month);
'long'
알파벳의 첫 글자와 같이 더 'short'
짧은 'narrow'
버전과 더 작은 버전 의 경우 월의 전체 이름을 사용합니다 .
로케일을 브라우저에서 'default'
원하는대로 (예 :로 'en-us'
) 변경할 수 있으며 해당 언어 / 국가에 올바른 이름을 사용합니다.
toLocaleString
API 를 사용하면 매번 로캘과 옵션을 전달해야합니다. 여러 다른 날짜에 동일한 로캘 정보 및 서식 옵션을 사용하려는 경우 Intl.DateTimeFormat
대신 다음을 사용할 수 있습니다 .
const formatter = new Intl.DateTimeFormat('fr', { month: 'short' });
const month1 = formatter.format(new Date());
const month2 = formatter.format(new Date(2003, 5, 12));
console.log(`${month1} and ${month2}`); // current month in French and "juin".
자세한 내용은 Internationalization API 에 대한 내 블로그 게시물을 참조하십시오 .
답변
현지화를 지원하는 또 하나는 다음과 같습니다. 🙂
Date.prototype.getMonthName = function(lang) {
lang = lang && (lang in Date.locale) ? lang : 'en';
return Date.locale[lang].month_names[this.getMonth()];
};
Date.prototype.getMonthNameShort = function(lang) {
lang = lang && (lang in Date.locale) ? lang : 'en';
return Date.locale[lang].month_names_short[this.getMonth()];
};
Date.locale = {
en: {
month_names: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
month_names_short: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
}
};
그런 다음 다른 언어에 대한 지원을 쉽게 추가 할 수 있습니다.
Date.locale.fr = {month_names: [...]};
답변
Date 프로토 타입을 확장하지 않아도되고 (이를 원하지 않는 몇 가지 이유가있는 경우) 실제로 매우 쉬운 방법을 찾을 수 있습니다.
Date.prototype.monthNames = [
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
];
Date.prototype.getMonthName = function() {
return this.monthNames[this.getMonth()];
};
Date.prototype.getShortMonthName = function () {
return this.getMonthName().substr(0, 3);
};
// usage:
var d = new Date();
alert(d.getMonthName()); // "October"
alert(d.getShortMonthName()); // "Oct"
이 함수는 모든 javascript Date 객체에 적용됩니다 .
답변
다음 과 같이 사용할 수 format
있는 moment.js 라이브러리 의 기능을 진심으로 추천합니다 .
moment().format("MMM"); // "Apr" - current date
moment(new Date(2012, 01, 04)).format("MMM"); // "Feb" - from a local date
moment.utc(new Date(2012, 00, 04).format("MMM"); // "Jan" - from a UTC date
월의 전체 이름이 필요한 경우 “MMM”대신 “MMMM”을 사용하십시오.
답변
Date.prototype.getMonthName = function() {
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
return monthNames[this.getMonth()];
}
로 사용할 수 있습니다
var month_Name = new Date().getMonthName();
답변
이것에 의해 날짜 객체로부터의 일반적인 쉬운 프로세스가 수행 될 수있다.
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var monthShortNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
function dateFormat1(d) {
var t = new Date(d);
return t.getDate() + ' ' + monthNames[t.getMonth()] + ', ' + t.getFullYear();
}
function dateFormat2(d) {
var t = new Date(d);
return t.getDate() + ' ' + monthShortNames[t.getMonth()] + ', ' + t.getFullYear();
}
console.log(dateFormat1(new Date()))
console.log(dateFormat2(new Date()))
또는 날짜 프로토 타입을 만들 수 있습니다
Date.prototype.getMonthName = function() {
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
return monthNames[this.getMonth()];
}
Date.prototype.getFormatDate = function() {
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
return this.getDate() + ' ' + monthNames[this.getMonth()] + ', ' + this.getFullYear();
}
console.log(new Date().getMonthName())
console.log(new Date().getFormatDate())
전의:
var dateFormat3 = new Date().getMonthName();
# March
var dateFormat4 = new Date().getFormatDate();
# 16 March, 2017
