【问题标题】:Compare dates with loop and javascript使用循环和 javascript 比较日期
【发布时间】:2016-02-10 20:08:04
【问题描述】:

我需要用 javascript 比较日期,所以我写了一个函数:

function fulljson (){
    var db_data;
    $.ajax({
                url: "http://localhost:8888/auction/offers/5", 
                type: "GET",
                async: true, 
                dataType: "html",
                success: function(data) {
                var db_data = $.parseJSON(data);
                console.log(db_data);

    // declare variables
    var period_start = new Date('2016-02-24'),
        period_now = new Date();
        period_end = new Date('2016-11-01'),
        //current_date = period_start,
        array_of_all_dates = [];
console.log(period_start);
        var dodaj = parseInt('3');
        period_now = period_now.setDate(period_now.getDate() + dodaj);

        console.log(new Date(period_start).getTime() + ' i ' + period_now);

        if (new Date(period_start).getTime() > period_now){
            current_date = new Date(period_start);

        } else {
            current_date = new Date(period_now);
            current_date.setHours(1
                ,0,0,0);
        }

        //current_date = moment(current_date).format('YYYY-MM-DD');
        console.log(current_date);
    // Create a populated array of dates
   // Create a populated array of dates
    while (current_date.getTime() <= period_end.getTime()) {
      array_of_all_dates.push(current_date);
      current_date = new Date(+current_date);
      current_date.setDate(current_date.getDate() + 1);
    }

    // Now loop over the array of populated dates and mutate, so something like
    array_of_all_dates = array_of_all_dates.map(function (date) {
      var found_in_db = db_data.filter(function (db_data) {
        return new Date(db_data.start).getTime() === date.getTime(); // You need to do this comparison better!
      });
      if (found_in_db.length > 0) {
        return found_in_db[0];
      }
      var new_object = {
        title: '',
        start: date,
        price: '60'
      };
      console.log(new_object);
      return new_object;

    });



    console.log('result'+array_of_all_dates);
    drawCalendar(array_of_all_dates);
                }, 
                error: function (data) {
                console.log(data);
                console.log('GRESKA NEKA');
                }      
    });
        //end OF AJAX

};

我的 ajax 函数得到这个数据:

[{"id":82,"price":61,"start":"Mon, 28 Mar 2016 00:00:00 +0000"},{"id":81,"price":61.5,"start":"Sun, 27 Mar 2016 00:00:00 +0000"},{"id":79,"price":61,"start":"Sun, 13 Mar 2016 00:00:00 +0000"},{"id":72,"price":61,"start":"Tue, 29 Mar 2016 00:00:00 +0000"},{"id":66,"price":61,"start":"Sat, 12 Mar 2016 00:00:00 +0000"},{"id":64,"price":60.5,"start":"Fri, 11 Mar 2016 00:00:00 +0000"}]

现在我需要比较日期的函数仅在这种情况下有效,即 2016 年 3 月 27 日。

还有我的period_start is 24. Feb.period_end is 1.Nov.2016

为什么我的函数只在 27.March 有效?

【问题讨论】:

  • 这是一些 PHP 模板系统吗? new Date('{{ date('Y-m-d', strtotime($article-&gt;from)) }}')? JS没有strtotime()date()函数
  • 是的,它的 Blade - Laravel...
  • 显示实际生成的JS,然后。不是这个 js/php mishmash。
  • 抱歉,好的 - 我更新了我的问题...
  • 您真的应该再次评估该代码。你为什么连续做 *THREE new Date(period_start)?为什么要创建三次相同的对象,尤其是因为您不更改日期值...

标签: javascript jquery json date compare


【解决方案1】:

这是一条评论,但空间不足,我想格式化它。

在代码中:

var period_start = new Date('2016-02-24'),
        period_now = new Date();  // <=== terminates statement
        period_end = new Date('2016-11-01'),
        //current_date = period_start,
        array_of_all_dates = [];

请注意,第二行以分号结束,因此 period_endarray_of_all_dates 变为全局。这可能不是问题,但谁知道呢……

另外,目前对 ECMAScript 2015 的解释是:

new Date('2016-02-24')

应该为 2016-02-24T00:00:00Z 创建一个新日期,即它被视为 UTC(这与 ISO 8601 和标准本身不一致,因为日期和时间字符串没有时区,如 2016-02- 24T00:00:00 被视为本地,但删除时间部分,它是 UTC)。

接下来,

new Date()

创建一个本地*日期,所以如果今天是 2016-02-12 那么:

new Date() != new Date('2016-02-12')

对于偏移量不是 00:00 的所有时区,因为时间值会因时区偏移量而不同。

我不清楚上述内容是否会对您的代码产生严重影响,但随意混合 UTC 和本地日期并不是一个好主意。

接下来看不懂的用法:

var dodaj = parseInt('3');

而不是:

var dodaj = 3;

还有:

if (new Date(period_start).getTime() > period_now)

无缘无故地创建和丢弃一个Date对象,下面是完全等价的:

if (period_start > period_now)

这样做的好处是它避免了 IE 中关于 new Date(date) 和两位数年份的模糊错误。

哦,最后,2016 年 3 月 27 日是一个星期日,daylight saving starts 在许多地方(例如意大利,虽然不是美国),所以也许夏令时会影响您的日期?

* 其中“本地日期”是指一个日期,其时间值已针对主机的当前时区偏移设置进行了调整。

【讨论】:

  • 我解决了我的问题:return new Date(db_data.start).toDateString() === date.toDateString();
  • @MonkeyBusiness — 使用return +new Date(db_data.start) == +date 更有意义,这样您可以直接比较时间值。
  • 你能写出完整的例子,我们可以测试吗?
  • @MonkeyBusiness——一旦有足够的信息可以这样做。将日期视为本地日期还是 UTC?
猜你喜欢
  • 2021-12-13
  • 2023-03-14
  • 2016-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 1970-01-01
相关资源
最近更新 更多