start date
和end date
- 完成start date
(week start date)mm/dd - mm/dd(week end date)
在下拉列表中显示周数。下面的代码会告诉我开始和结束日期。
let dates = JSON.parse(localStorage.getItem('dates'));
let startDate = moment((dates[0].value)).format('YYYY-MM-DD'); //"2018-05-01"
let endDate = moment((dates[1].value)).format('YYYY-MM-DD'); //"2018-05-15"
示例 开课日期:2018年5月1日,星期二 结束日:2018年5月15日,星期二
总天数:15天。为2周和1天。
所以我需要显示下面的下拉列表
试图提取周数
Date.prototype.getWeek = function(start)
{
//Calcing the starting point
start = start || 0;
var today = new Date(this.setHours(0, 0, 0, 0));
var day = today.getDay() - start;
var date = today.getDate() - day;
// Grabbing Start/End Dates
var StartDate = new Date(today.setDate(date));
var EndDate = new Date(today.setDate(date + 6));
return [StartDate, EndDate];
}
// test code
var Dates = new Date().getWeek();
但上述代码并不起作用。请帮忙
答案 0 :(得分:1)
您可以执行以下操作:
formatDates() {
let startDate = moment('2018-05-01');
let endDate = moment('2018-05-15');
let weekData = [];
while(startDate.isSameOrBefore(endDate)) {
if(weekData.length > 0) {
// Update end date
let lastObj = weekData[weekData.length - 1];
lastObj['endDate'] = moment(startDate).format('MM/DD');
lastObj['label'] = `${lastObj.startDate} - ${lastObj['endDate']} (week${weekData.length})`
startDate.add(1, 'days');
}
weekData.push({startDate: moment(startDate).format('MM/DD')});
startDate.add(6, 'days');
}
if(startDate.isAfter(endDate)) {
// Update last object
let lastObj = weekData[weekData.length - 1];
lastObj['endDate'] = moment(endDate).format('MM/DD');
lastObj['label'] = `${lastObj.startDate} - ${lastObj['endDate']} (week${weekData.length})`
}
return weekData;
}
formatDates
将返回数周的数组:
[
{startDate: "05/01", endDate: "05/07", label: "05/01 - 05/07 (week1)"},
{startDate: "05/08", endDate: "05/14", label: "05/08 - 05/14 (week2)"},
{startDate: "05/15", endDate: "05/15", label: "05/15 - 05/15 (week3)"}
]
答案 1 :(得分:0)
如果有人仍在寻找简单的解决方案,那就是我的:
shared.service.ts
import * as moment from 'moment'; // Make sure you have installed moment from npm
// Add these methods to your Service
/**
* @description group Array of dates by an entire week
* @param dates Array of dates
*/
public getWeeksMapped(dates: Date[]): Map<string, number[]> {
const weeks = dates.reduce((week, date) => {
const yearWeek = `${moment(date).year()}-${moment(date).week()}`;
if (!week.has(yearWeek)) {
week.set(yearWeek, []);
}
week.get(yearWeek).push(date.getTime()); // timestamp returned
return week;
}, new Map());
return weeks;
}
/**
* @description Returns the dates between 2 dates
* @param startDate Initial date
* @param endDate Final date
* @param dayStep Day step
*/
public getDateRange(startDate: Date, endDate: Date, dayStep = 1): Date[] {
const dateArray = [];
const currentDate = new Date(startDate);
while (currentDate <= new Date(endDate)) {
dateArray.push(new Date(currentDate));
currentDate.setUTCDate(currentDate.getUTCDate() + dayStep);
}
return dateArray;
}
component.ts
const dates = this.sharedS.getDateRange(yourDateStart, yourDateEnd);
const weeksGrouped = this.sharedS.getWeeksMapped(dates);
console.log(weeksGrouped);
您将获得正确映射的周数。 getWeeksMapped()
返回为时间戳,但是,如果要返回为Date,则只需在Map<string, Date[]>
中更改类型并在推入对象时删除getTime()
方法即可。像这样:
public getWeeksMapped(dates: Date[]): Map<string, Date[]> {
const weeks = dates.reduce((week, date) => {
const yearWeek = `${moment(date).year()}-${moment(date).week()}`;
if (!week.has(yearWeek)) {
week.set(yearWeek, []);
}
week.get(yearWeek).push(date); // remove getTime()
return week;
}, new Map());
return weeks;
}