如何计算和检查双周日期 [英] How to calculate and check for a bi-weekly date

查看:114
本文介绍了如何计算和检查双周日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的需要您的帮助,

我的分界开始日期为:2016年12月19日,由变量 x定义

Let's say my demarcation start date is: December 19, 2016 as defined by the variable x

我该如何编写JavaScript函数,以便它将根据 x ,并将当前日期与重复日期为 x 的十四天(由变量 y 定义)相比。

How can I write a JavaScript function, such that it will check the present date against x and the present date against what the recurrence date will be (14) days from x as defined by the variable y.

var y = recurrence is every 14 days, thereafter from the date (x) with no end date specified (unlimited)

Ex。

function() {

    if (present date == x) { alert(true) }

    if (present date == y) { alert(true) }

}


推荐答案

您可以获取开始日期和当前日期之间的天数差,然后检查该数字是否为14的倍数。

You could get the number of days difference between your start date and the current date then check if that number is a multiple of 14.

function treatAsUTC(date) {
    var result = new Date(date);
    result.setMinutes(result.getMinutes() - result.getTimezoneOffset());
    return result;
}

function daysBetween(startDate, endDate) {
    var millisecondsPerDay = 24 * 60 * 60 * 1000;
    return Math.floor((treatAsUTC(endDate) - treatAsUTC(startDate)) / millisecondsPerDay);
}

var demarcationdate = new Date("2016-12-19"), 
    today = new Date(),
    days = daysBetween(demarcationdate,today),
    daystill = 14 - days%14,
    rec = days%14==0,
    d = new Date();

d.setDate(today.getDate() + daystill);
var nextDate = (d.getDate() + "/" + (d.getMonth() + 1) + "/" + d.getFullYear());

console.log("Days diff = "+days+". Recurs today = "+rec+". Next in "+daystill+" days ("+nextDate.toString()+").");

jsFiddle

这篇关于如何计算和检查双周日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆