2017-08-24 80 views

回答

1

下面使用什么工作的例子加减日期,月份和日期的检查:

一日

let tomorrow = new Date(); 
tomorrow = moment(tomorrow).add(1, 'day').format('YYYY-MM-DD'); // for specific format 

let today = moment(new Date()).format('YYYY-MM-DD'); 

let tomorrow = new Date(); 
tomorrow = moment(tomorrow).add(1, 'day').format('YYYY-MM-DD'); 

单月

moment(currentMonth).add(1, 'month'); 
moment(currentMonth).subtract(1, 'month'); 

对于日期

let now = "14/11/2016"; 
let then = "03/12/2017"; 

let diff = moment(now,"DD/MM/YYYY").diff(moment(then,"DD/MM/YYYY")); 
let duration = moment.duration(diff); 
let result = duration.format("hh:mm:ss"); 

请参阅详细的时间差:https://stackoverflow.com/a/18624295/6592263

编辑(未测试)

要获得月

let d = moment(new Date()); 
d.month(); // month number 
d.format('ddd MMM DD YYYY'); 

获取年份

let d = moment(new Date()); 
d.year() // year 
+0

Wowww很酷的男人。感谢很多。它对我很有帮助。再次感谢 –

+0

还有一件事。如果我想减去并添加一年,以便与上述月份相同。 –

+0

,你能告诉我如何减去一个月..实际上我得到了一个错误。 –