我在下面尝试了JS:
var start = new Date("25-05-2016");
var finish = new Date("31-05-2016");
var dayMilliseconds = 1000 * 60 * 60 * 24;
var weekendDays = 0;
while (start <= finish) {
var day = start.getDay()
if (day == 0) {
weekendDays++;
}
start = new Date(+start + dayMilliseconds);
}
alert(weekendDays);
&#13;
然而,它输出错误。
我需要在两个日期之间获得星期日总数。
答案 0 :(得分:3)
您使用了错误的日期格式。如果初始日期如此,它将起作用:
var start = new Date("2016-05-25");
var finish = new Date("2016-05-31");
答案 1 :(得分:1)
您的日期格式和比较条件应如下所示:
var start = new Date("2016-05-11");
var finish = new Date("2016-05-31");
var dayMilliseconds = 1000 * 60 * 60 * 24;
var weekendDays = 0;
while (start.getTime() <= finish.getTime()) {
var day = start.getDay();
if (day == 0) {
weekendDays++;
}
start = new Date(+start + dayMilliseconds);
}
alert(weekendDays);
检查Fiddle
答案 2 :(得分:1)
您使用的日期格式不正确 只需将格式更改为:
var start = new Date(2016, 4, 25);
var finish = new Date(2016, 4, 31);
答案 3 :(得分:1)
您的日期格式错误。日期的字符串格式为func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRectZero
messageLabel.text = "No barcode/QR code is detected"
return
}
self.captureSession?.stopRunning()
。有关详细信息,请参阅here。
此外,循环每一天的间隔效率非常低。您可以尝试以下方式。
"yyyy-mm-dd"
答案 4 :(得分:0)
尝试此功能:
function CalculateWeekendDays(fromDate, toDate){
var weekendDayCount = 0;
while(fromDate < toDate){
fromDate.setDate(fromDate.getDate() + 1);
if(fromDate.getDay() === 0){
++weekendDayCount ;
}
}
return weekendDayCount ;
}
console.log(CalculateWeekendDays(new Date(2011, 6, 2), new Date(2011, 7, 2)));
这将为您提供两个日期之间的星期日数
答案 5 :(得分:0)
更改日期格式。它会正常工作
<tr><form><td>
答案 6 :(得分:0)
JS日期格式没有“dd-MM-yyyy”,因此它将无效日期格式。尝试重新创建日期即可,或者只是更改日期格式Date Format
试试这个:
var start = new Date("25-05-2016");
var end = new Date("31-05-2016");
var startDate = new Date(start);
var endDate = new Date(end);
var totalSundays = 0;
for (var i = startDate; i <= endDate; ){
if (i.getDay() == 0){
totalSundays++;
}
i.setTime(i.getTime() + 1000*60*60*24);
}
console.log(totalSundays);