时刻js中的日期时间比较问题

时间:2019-03-07 09:14:59

标签: javascript c# momentjs datetime-format

我正在使用moment.js进行日期时间比较,但到目前为止未成功。我想在特定时间段内启用网站上的功能。我正在接收json格式的datetime,它以utc datetime的形式保存在数据库中。现在,我想检查当前日期时间是否介于开始时间和结束时间之间。

  1. 如果它在开始广告时段之前降落,我会向用户显示一条消息,提示您需要“激活x分钟”
  2. 如果介于两者之间,则“功能已激活”。
  3. 如果它在时隙结束时间之后下降,则无需采取任何措施。

这就是我所做的。它大部分时间都可以工作,但是仍然有一些失败的情况。

function checkEqual(startDateTime, endDateTime) {

var startDateTimeVal = startDateTime.substring(startDateTime.indexOf("(") + 1, startDateTime.indexOf(")"));
var endDateTimeVal = endDateTime.substring(endDateTime.indexOf("(") + 1, endDateTime.indexOf(")"));

const s = new Date(parseInt(startDateTimeVal));
const e = new Date(parseInt(endDateTimeVal));

const d = new Date();
var timeToActivate = 0, timeToActivateHours = 0, timeToActivateMinutes = 0;

if (s.getUTCDate() === d.getDate() && s.getUTCMonth() === d.getMonth() && s.getFullYear() === d.getFullYear()) { // date


    if (s.getUTCHours() <= d.getHours() && e.getUTCHours() >= d.getHours()) { // hours

        if (s.getUTCHours() === d.getHours()) {
           if (s.getUTCMinutes() <= d.getMinutes()) {

                return new Promise(function (resolve, reject) {
                    resolve(0);
                });
            }
            else {

                return new Promise(function (resolve, reject) {

                    timeToActivate = s.getUTCMinutes() - d.getMinutes();

                    toastr.info("chat will become active in next " + parseInt(timeToActivate) + " minutes!");
                    resolve(timeToActivate * 60000);
                });
            }

        }
        else if (e.getUTCHours() === d.getHours()) {

            if (e.getUTCMinutes() <= d.getMinutes()) {

                    return new Promise(function (resolve, reject) {
                        resolve(0);
                    });
                }
                else {

                    return new Promise(function (resolve, reject) {

                        timeToActivate = e.getUTCMinutes() - d.getMinutes();

                        toastr.info(" chat will become active in next " + parseInt(timeToActivate) + " minutes!");
                        resolve(timeToActivate * 60000);
                    });
                }

        }
        else {
                if (s.getUTCMinutes() <= d.getMinutes()) {

                    return new Promise(function (resolve, reject) {
                        resolve(0);
                    });
                }
                else {

                    return new Promise(function (resolve, reject) {

                        timeToActivate = s.getUTCMinutes() - d.getMinutes();
                        toastr.info("chat will become active in next " + parseInt(timeToActivate) + " minutes!");
                        resolve(timeToActivate * 60000);
                    });
                }
        }
    }
    else {

        if (s.getUTCHours() >= d.getHours()) { // logged in before time

            return new Promise(function (resolve, reject) {

                var timeToActivateHours = (s.getUTCHours() - d.getHours()) * 3600000;
                var timeToActivateMinutes;

                if (s.getUTCMinutes() > d.getMinutes()) {
                    timeToActivateMinutes = (d.getMinutes() - s.getUTCMinutes()) * 60000;
                }
                else {
                    timeToActivateMinutes = (s.getUTCMinutes()- d.getMinutes() ) * 60000;
                }


                timeToActivate = timeToActivateHours + timeToActivateMinutes;

                toastr.info("chat will become active in next " + parseInt(timeToActivate / 60000) + " minutes!");
                resolve(timeToActivate);
            });
        }

    }
}
return new Promise(function (resolve, reject) {
           resolve(-1);
});

}

最近我尝试使用diff函数,但仍然存在问题,因为utc和本地时区使我头疼。有开始日期时间和结束日期时间。

开始日期时间:/ Date(1551966300000)/
结束日期时间:/ Date(1551969900000)/

非常感谢任何指导!

2 个答案:

答案 0 :(得分:0)

U可以尝试这种逻辑

var start = 1551966300000 
var end = 1551969900000
var currentTime = moment().valueOf() //this should return a value  of current time utc
if(currentTime >= start && currentTime <= end ) {
//call your function
myFuction()
}
if(currentTime < start){
var current = moment(currentTime)
var diff = current.diff(start,'minutes')}//diff- minutes to activate 

答案 1 :(得分:0)

这是我还没活着的凌晨4点。但似乎您在不需要的地方使用了承诺,使代码复杂化。可能要休息一下。 无论如何。 我看过古怪的格式。 (/ Date(1551966300000)/)我也将其切成薄片。 后来我发现有一种方法可以立即解析它。 http://momentjs.com/docs/#/parsing/asp-net-json-date/ TLDR; “片刻就能识别并照顾好它”

我基本上会做

    const inputDate = Moment(value, "X");
    const referenceDateStart = Moment(startSlot);
    const referenceDateEnd = Moment(endSlot);
    const minutesToGo = referenceDateStart.diff(inputDate, "minutes");

    if (minutesToGo < 0) {
      return;
      // no action needed, it falls after the endTime
    }


    if (minutesToGo > 0) {
      console.log(`${minutesToGo}  minutes to activate`);
      return;
      // before start time
    }

    /*this last scenario es the more complex one to calculate. it's also the logical 
        alternative. sooo you can skip the validate . Booyah!*/

    console.log("feature activated");



/*disclosure. you'll need to test whether those ">" and "<" are correct. you might need to flip them*/