admin管理员组文章数量:1414604
I'm modifying Angular's UI Bootstrap Datepicker to allow for a week selection mode. In order for this to work effectively, I need to set the activeDate
to the first Sunday of a given week, so if you select a date, say a Monday like 8/29/2016, it converts your selection into the earlier Sunday, 8/28/16.
This is what the selection function looks like. If interested in the conversion to Sunday logic in the else if
block where the alert matches.
$scope.select = function(date) {
if ($scope.datepickerMode === self.minMode) {
var dt = ngModelCtrl.$viewValue ? dateParser.fromTimezone(new Date(ngModelCtrl.$viewValue), ngModelOptions.timezone) : new Date(0, 0, 0, 0, 0, 0, 0);
dt.setFullYear(date.getFullYear(), date.getMonth(), date.getDate());
dt = dateParser.toTimezone(dt, ngModelOptions.timezone);
ngModelCtrl.$setViewValue(dt);
ngModelCtrl.$render();
}
else if($scope.datepickerMode =="week")
{
alert("Match!");
self.activeDate = date.firstSundayofthatweek() //what do I actually put here using Moment.js?
setMode(self.modes[self.modes.indexOf($scope.datepickerMode) - 1]);
$scope.$emit('uib:datepicker.mode');
}
else {
self.activeDate = date;
setMode(self.modes[self.modes.indexOf($scope.datepickerMode) - 1]);
$scope.$emit('uib:datepicker.mode');
}
$scope.$broadcast('uib:datepicker.focus');
};
I'm using moment.js but can't figure out in their docs how to do it. A vanilla JS answer would also be appreciated. date
that is passed to the function is a date object.
Moment docs suggest either:
moment().day(-7);
or
moment().weekday(-7);
but I am passing a date object, not a moment... so do I convert or chain or something? Sorry a bit confused... I also need to return a date object to self.activeDate
I'm modifying Angular's UI Bootstrap Datepicker to allow for a week selection mode. In order for this to work effectively, I need to set the activeDate
to the first Sunday of a given week, so if you select a date, say a Monday like 8/29/2016, it converts your selection into the earlier Sunday, 8/28/16.
This is what the selection function looks like. If interested in the conversion to Sunday logic in the else if
block where the alert matches.
$scope.select = function(date) {
if ($scope.datepickerMode === self.minMode) {
var dt = ngModelCtrl.$viewValue ? dateParser.fromTimezone(new Date(ngModelCtrl.$viewValue), ngModelOptions.timezone) : new Date(0, 0, 0, 0, 0, 0, 0);
dt.setFullYear(date.getFullYear(), date.getMonth(), date.getDate());
dt = dateParser.toTimezone(dt, ngModelOptions.timezone);
ngModelCtrl.$setViewValue(dt);
ngModelCtrl.$render();
}
else if($scope.datepickerMode =="week")
{
alert("Match!");
self.activeDate = date.firstSundayofthatweek() //what do I actually put here using Moment.js?
setMode(self.modes[self.modes.indexOf($scope.datepickerMode) - 1]);
$scope.$emit('uib:datepicker.mode');
}
else {
self.activeDate = date;
setMode(self.modes[self.modes.indexOf($scope.datepickerMode) - 1]);
$scope.$emit('uib:datepicker.mode');
}
$scope.$broadcast('uib:datepicker.focus');
};
I'm using moment.js but can't figure out in their docs how to do it. A vanilla JS answer would also be appreciated. date
that is passed to the function is a date object.
Moment docs suggest either:
moment().day(-7);
or
moment().weekday(-7);
but I am passing a date object, not a moment... so do I convert or chain or something? Sorry a bit confused... I also need to return a date object to self.activeDate
2 Answers
Reset to default 5To get the last sunday from a given moment, simply use day()
on that moment, together with the days name.
var today = moment("29.08.2016", "DD.MM.YYYY");
var sunday = today.day("Sunday");
console.log(sunday.format("YYYY-MM-DD"));
If you have a Date
object, simply parse it as moment
moment(new Date());
Moment uses sunday as the first day of the week:
const lastSunday = currentDate => {
const date = moment(currentDate).startOf('week');
return date._d;
};
本文标签: javascriptMomentJS Get The Sunday Before A Given DateStack Overflow
版权声明:本文标题:javascript - Moment.JS Get The Sunday Before A Given Date - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745165043a2645632.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论