我以月数的形式从Ajax接收数据
0: 2018 - year
1: 6 - month
2: 7 - day
如何将其转换为月份名称? 示例:
0: 2018 - year
1: "Jun" - month
2: 7 - day
答案 0 :(得分:2)
使用数组:
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()]);
或尝试
var date = new Date("10/11/2018"),
locale = "en-us",
month = date.toLocaleString(locale, { month: "long" });
答案 1 :(得分:0)
此处:
function monthName(mon) {
return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'November', 'December'][mon - 1];
}
console.log(monthName(7));
当您准备使用更专业的解决方案时,请使用moment.js
答案 2 :(得分:0)
let data = {
0: 2018,
1: 6,
2: 7
}
let months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'November', 'December']
如果您始终在响应的数据对象的索引1处拥有月份,则可以直接将其设置为定义的月份名称。
data[1] = months[data[1]-1];
这将是输出
{
0: 2018,
1: 'June',
2: 7
}