2014-09-30 70 views
62

我需要計算JS年月= 2014和月= 9(2014年9月)的日期。瞬間JS開始和月底結束

我嘗試這樣做:

var moment = require('moment'); 
var startDate = moment(year+'-'+month+'-'+01 + ' 00:00:00'); 
      var endDate = startDate.endOf('month'); 
      console.log(startDate.toDate()); 
      console.log(endDate.toDate()); 

日誌都顯示:

Tue Sep 30 2014 23:59:59 GMT+0200 (CEST) 
Tue Sep 30 2014 23:59:59 GMT+0200 (CEST) 

結束日期是正確的,但......爲什麼開始日期是不是?

回答

114

這是因爲endOf突變了原來的值。

相關報價:

器變化了原有的時刻將其設置爲時間單位的結束。

下面是一個例子功能,讓你的輸出你想要的:

function getMonthDateRange(year, month) { 
    var moment = require('moment'); 

    // month in moment is 0 based, so 9 is actually october, subtract 1 to compensate 
    // array is 'year', 'month', 'day', etc 
    var startDate = moment([year, month - 1]); 

    // Clone the value before .endOf() 
    var endDate = moment(startDate).endOf('month'); 

    // just for demonstration: 
    console.log(startDate.toDate()); 
    console.log(endDate.toDate()); 

    // make sure to call toDate() for plain JavaScript date type 
    return { start: startDate, end: endDate }; 
} 

參考文獻:

+2

'moment'是冪等的,所以你也可以使用'endDate = moment(starDate).endOf(「month」)'** ^。^ ** – naomik 2014-09-30 22:33:27

+1

絕對地,實際上在文檔中克隆對象的默認方式是「moment()'。更新了示例以使用一些較短的函數。 – klyd 2014-09-30 22:39:10

+0

謝謝。我花了兩個小時試圖弄清楚爲什麼它不工作。 – Leon 2015-09-30 00:44:14

12

當您使用.endOf()你是變異的,它被稱爲上的對象,所以startDate成爲09月30日

您應該使用.clone()做它的一個副本,而不是改變它

var startDate = moment(year + '-' + month + '-' + 01 + ' 00:00:00'); 
      var endDate = startDate.clone().endOf('month'); 
      console.log(startDate.toDate()); 
      console.log(endDate.toDate()); 

Mon Sep 01 2014 00:00:00 GMT+0700 (ICT) 
Tue Sep 30 2014 23:59:59 GMT+0700 (ICT) 
2

唐真的認爲有一些直接的方法可以得到最後一天,但你可以做這樣的事情:

var dateInst = new moment(); 
/** 
* adding 1 month from the present month and then subtracting 1 day, 
* So you would get the last day of this month 
*/ 
dateInst.add(1, 'months').date(1).subtract(1, 'days'); 
/* printing the last day of this month's date */ 
console.log(dateInst.format('YYYY MM DD')); 
0

你的startDate是第一天的一個月,在這種情況下,我們可以使用

var endDate = moment(startDate).add(1, 'months').subtract(1, 'days'); 

希望這有助於!

3

可以直接爲最終使用或啓動一個月的日期

new moment().startOf('month').format("YYYY-DD-MM"); 
new moment().endOf("month").format("YYYY-DD-MM"); 

您可以通過定義一個新的格式

+0

moment()。endOf(「month」).format(「YYYY-DD-MM」); – Scholle 2017-12-11 23:59:50

0
var d = new moment(); 
var startMonth = d.clone().startOf('month'); 
var endMonth = d.clone().endOf('month'); 
console.log(startMonth, endMonth); 

doc

0

const year = 2014; 
 
const month = 09; 
 

 
// months start at index 0 in momentjs, so we subtract 1 
 
const startDate = moment([year, month - 1, 01]).format("YYYY-MM-DD"); 
 

 
// get the number of days for this month 
 
const daysInMonth = moment(startDate).daysInMonth(); 
 

 
// we are adding the days in this month to the start date (minus the first day) 
 
const endDate = moment(startDate).add(daysInMonth - 1, 'days').format("YYYY-MM-DD"); 
 

 
console.log(`start date: ${startDate}`); 
 
console.log(`end date: ${endDate}`);
<script 
 
src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"> 
 
</script>
更改格式