在各个日期段中拆分日期时间范围,例如访问日期时间范围内每一天的分钟数
Split a datetime range in individual day slots e.g. access the number of minutes for each individual day present in a datetime range
我在 javascript 中有一个开始日期对象和一个结束日期对象,例如
var startDate = moment("2019-04-22T09:20:00+05:00");
var endtDate = moment("2019-04-25T11:00:00+05:00");
现在我想要这样的输出
[880,1440,660] //array for the numbers of minutes for each day present in the given range
我尝试过使用力矩范围模块,但还是没用
提前感谢您的帮助
试试这个代码。
var startDate = moment("2019-04-22T09:20:00+05:00");
var endDate = moment("2019-04-25T11:00:00+05:00");
var duration = moment.duration(endDate.diff(startDate));
var days = Math.round(duration.asDays());
var start,end;
var minutes = [];
for(i = 0; i<=days; i++){
if(i == 0){
start = startDate;
end = moment(startDate).endOf("day").utcOffset("+05:00");
}else if(i == days){
start = moment(endDate).startOf('day').utcOffset("+05:00");
end = endDate;
} else {
start = moment(startDate).add('days', i).startOf('day').utcOffset("+05:00");
end = moment(startDate).add('days', i).endOf('day').utcOffset("+05:00");
}
var duration = moment.duration(end.diff(start));
var min = duration.asMinutes();
minutes.push(Math.round(min))
}
console.log(minutes)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
试试这个代码,只需计算开始日期和结束日期的分钟数,并通过计算两个日期之间的天数将日期之间的时间推到 1440 分钟
function getMinutes(dt1, dt2) {
minuteArray = [];
// hours*minutes*seconds*milliseconds
var oneDay = 24 * 60 * 60 * 1000;
startHours = (24 - dt1.getHours())
if (dt1.getMinutes() > 0) {
startHours--;
}
// Calculate Minutes for start date
startDayMin = startHours * 60 + (60 - dt1.getMinutes());
minuteArray.push(startDayMin);
var diffDays = Math.round(Math.abs((dt1.getTime() - dt2.getTime()) / (oneDay))) - 1; //Calculate number of days
for (i = 0; i < diffDays; i++) {
//Push minutes for number of days
minuteArray.push(1440);
}
if (diffDays >= 0){
// Calculate Minutes for end date
endDayMin = (dt2.getHours()* 60) + dt2.getMinutes();
minuteArray.push(endDayMin)
}
return minuteArray;
}
var startDate = new Date("2019-04-22T09:20:00+05:00");
var endtDate = new Date("2019-04-25T11:00:00+05:00");
console.log(getMinutes(startDate, endtDate));
我在 javascript 中有一个开始日期对象和一个结束日期对象,例如
var startDate = moment("2019-04-22T09:20:00+05:00");
var endtDate = moment("2019-04-25T11:00:00+05:00");
现在我想要这样的输出
[880,1440,660] //array for the numbers of minutes for each day present in the given range
我尝试过使用力矩范围模块,但还是没用
提前感谢您的帮助
试试这个代码。
var startDate = moment("2019-04-22T09:20:00+05:00");
var endDate = moment("2019-04-25T11:00:00+05:00");
var duration = moment.duration(endDate.diff(startDate));
var days = Math.round(duration.asDays());
var start,end;
var minutes = [];
for(i = 0; i<=days; i++){
if(i == 0){
start = startDate;
end = moment(startDate).endOf("day").utcOffset("+05:00");
}else if(i == days){
start = moment(endDate).startOf('day').utcOffset("+05:00");
end = endDate;
} else {
start = moment(startDate).add('days', i).startOf('day').utcOffset("+05:00");
end = moment(startDate).add('days', i).endOf('day').utcOffset("+05:00");
}
var duration = moment.duration(end.diff(start));
var min = duration.asMinutes();
minutes.push(Math.round(min))
}
console.log(minutes)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
试试这个代码,只需计算开始日期和结束日期的分钟数,并通过计算两个日期之间的天数将日期之间的时间推到 1440 分钟
function getMinutes(dt1, dt2) {
minuteArray = [];
// hours*minutes*seconds*milliseconds
var oneDay = 24 * 60 * 60 * 1000;
startHours = (24 - dt1.getHours())
if (dt1.getMinutes() > 0) {
startHours--;
}
// Calculate Minutes for start date
startDayMin = startHours * 60 + (60 - dt1.getMinutes());
minuteArray.push(startDayMin);
var diffDays = Math.round(Math.abs((dt1.getTime() - dt2.getTime()) / (oneDay))) - 1; //Calculate number of days
for (i = 0; i < diffDays; i++) {
//Push minutes for number of days
minuteArray.push(1440);
}
if (diffDays >= 0){
// Calculate Minutes for end date
endDayMin = (dt2.getHours()* 60) + dt2.getMinutes();
minuteArray.push(endDayMin)
}
return minuteArray;
}
var startDate = new Date("2019-04-22T09:20:00+05:00");
var endtDate = new Date("2019-04-25T11:00:00+05:00");
console.log(getMinutes(startDate, endtDate));