2017-02-15 104 views
7

我使用以下代码获取上个月的startDate和endDate。获取上个月使用时刻的月份名称

// Previous month 
var startDateMonthMinusOne = moment().subtract(1, "month").startOf("month").unix(); 
var endDateMonthMinusOne = moment().subtract(1, "month").endOf("month").unix(); 

// Previous month - 1 

var startDateMonthMinusOne = moment().subtract(2, "month").startOf("month").unix(); 
var endDateMonthMinusOne = moment().subtract(2, "month").endOf("month").unix(); 

我该如何获得月份名? (1月,2月......)

+0

检查了这一点http://stackoverflow.com/questions/29303164/get-all-months-name-from-year-in-moment-js – Givelasdougmore

回答

9

代替unix()使用format()功能使用MMMM格式说明当月的名称来格式化日期时间。

var monthMinusOneName = moment().subtract(1, "month").startOf("month").format('MMMM'); 

请参见Display/Format in the documentation

3

您可以简单地使用format('MMMM')

这里工作的例子:

var currMonthName = moment().format('MMMM'); 
 
var prevMonthName = moment().subtract(1, "month").format('MMMM'); 
 

 
console.log(currMonthName); 
 
console.log(prevMonthName);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>

相关问题