Javascript 如何使用momentjs创建firstDayOfMonth和lastDayOfMonth函数
在使用javascript时,我遇到了一个非常简单的问题:如何使用javascript和momentjs获取每月的第一天和最后一天。 我知道在vb中应该是这样的:Javascript 如何使用momentjs创建firstDayOfMonth和lastDayOfMonth函数,javascript,momentjs,Javascript,Momentjs,在使用javascript时,我遇到了一个非常简单的问题:如何使用javascript和momentjs获取每月的第一天和最后一天。 我知道在vb中应该是这样的: Public Function LastDayOfMonth(ByVal current As DateTime) As DateTime Dim daysInMonth As Integer = DateTime.DaysInMonth(current.Year, current.Month) Return cur
Public Function LastDayOfMonth(ByVal current As DateTime) As DateTime
Dim daysInMonth As Integer = DateTime.DaysInMonth(current.Year, current.Month)
Return current.FirstDayOfMonth().AddDays(daysInMonth - 1)
End Function
Public Function FirstDayOfMonth(ByVal current As DateTime) As DateTime
Return current.AddDays(1 - current.Day)
End Function
如何将此代码移动到javascript+momentjs?我认为图书馆没有类似的方法
谢谢。我不懂VB,您的问题不清楚您的输入输出要求。据我所知,这里有一个解决办法。它使用的不是moments.js,而是POJS。如果你愿意(不知道为什么),你可以很容易地将它转换为使用瞬间 Javascript
function firstDayOfMonth() {
var d = new Date(Date.apply(null, arguments));
d.setDate(1);
return d.toISOString();
}
function lastDayOfMonth() {
var d = new Date(Date.apply(null, arguments));
d.setMonth(d.getMonth() + 1);
d.setDate(0);
return d.toISOString();
}
var now = Date.now();
console.log(firstDayOfMonth(now));
console.log(lastDayOfMonth(now));
console.log(moment().startOf('month').utc().toString());
console.log(moment().endOf("month").utc().toString());
输出
2013-06-01T21:22:48.000Z
2013-06-30T21:22:48.000Z
2013-06-01T00:00:00+02:00
2013-06-30T23:59:59+02:00
有关格式,请参见
在
利用时间,你可以做到这一点
Javascript
function firstDayOfMonth() {
var d = new Date(Date.apply(null, arguments));
d.setDate(1);
return d.toISOString();
}
function lastDayOfMonth() {
var d = new Date(Date.apply(null, arguments));
d.setMonth(d.getMonth() + 1);
d.setDate(0);
return d.toISOString();
}
var now = Date.now();
console.log(firstDayOfMonth(now));
console.log(lastDayOfMonth(now));
console.log(moment().startOf('month').utc().toString());
console.log(moment().endOf("month").utc().toString());
输出
2013-06-01T21:22:48.000Z
2013-06-30T21:22:48.000Z
2013-06-01T00:00:00+02:00
2013-06-30T23:59:59+02:00
有关格式,请参阅
到目前为止,您尝试了什么?函数属性的格式是什么?您希望函数返回什么,日期(1-31)还是字符串(星期一-星期日)