【问题标题】:Javascript new Date() Concat Date and Time Doesn't Work on Mobile BrowsersJavascript new Date() Concat 日期和时间在移动浏览器上不起作用
【发布时间】:2018-10-15 15:25:08
【问题描述】:

我正在使用open-source script 将事件添加到外部日历。我也在使用时刻来改变时间格式。

  this.addToCalendar = createCalendar({
    options: {
      class: 'addtocal-btn',
      id: 'event-' + this.item.id                                // You need to pass an ID. If you don't, one will be generated for you.
    },
    data: {
      title: this.item.title,     // Event title
      start: new Date(this.calendarDate(this.item.start_date) + ' ' + moment(this.item.start_time, ["h:mm A"]).format("HH:mm")),   // Event start date
      end: new Date(this.calendarDate(this.item.end_date) + ' ' + moment(this.item.end_time, ["h:mm A"]).format("HH:mm")),     // You can also choose to set an end time.
      address: this.item.location,
      description: this.item.description,
      timezone: 'America/Detroit'
    }
  });

不幸的是,数据库中的日期和时间是分开的,所以我必须在 Date() 函数中将两者连接起来。

示例 start_date 将是“2018-10-15 00:00:00”,而示例 start_time 将是“10:00 p.m.”因此,为什么我必须通过函数运行 start_date 并通过时刻运行 start_time 以获得正确的格式。

// If 2018-10-15 00:00:00 is passed, returns 2018-10-15
calendarDate: function (value) {
  var arr = value.split(' ');
  return arr[0]
},

无论如何问题是,虽然这在桌面浏览器上工作得很好,但在移动浏览器上却不行。而且由于我无法直接在移动设备上进行任何测试,因此无法直接诊断问题。

我已经能够将问题缩小到日期和时间的串联,但我不知道为什么桌面浏览器接受它而移动浏览器不接受(仅供参考,我已经在 Safari 中尝试过和 iPhone6s Plus 上的 Chrome)

【问题讨论】:

    标签: javascript datetime mobile


    【解决方案1】:

    所以看起来移动浏览器(无论出于何种原因)不喜欢现在的 .format("HH:mm") 。所以我完全放弃了时刻,手动将 AM/PM 转换为 24 小时格式(使用代码 from here):

    convertTimeformat: function(time) {
        var hours = Number(time.match(/^(\d+)/)[1]);
        var minutes = Number(time.match(/:(\d+)/)[1]);
        var AMPM = time.match(/\s(.*)$/)[1];
        if (AMPM == "p.m." && hours < 12) hours = hours + 12;
        if (AMPM == "a.m." && hours == 12) hours = hours - 12;
        var sHours = hours.toString();
        var sMinutes = minutes.toString();
        if (hours < 10) sHours = "0" + sHours;
        if (minutes < 10) sMinutes = "0" + sMinutes;
        return sHours + ':' + sMinutes;
    }
    

    然后制作新的Date():

    var start_datetime = moment(this.calendarDate(this.item.start_date) + ' ' + this.convertTimeformat(this.item.start_time));
    
    var end_datetime = moment(this.calendarDate(this.item.end_date) + ' ' + this.convertTimeformat(this.item.end_time));
    
      this.addToCalendar = createCalendar({
        options: {
          class: 'addtocal-btn',
          id: 'event-' + this.item.id                                // You need to pass an ID. If you don't, one will be generated for you.
        },
        data: {
          title: this.item.title,
          start: new Date(start_datetime),
          end: new Date(end_datetime),
          address: this.item.location,
          description: this.item.description,
          timezone: 'America/Detroit'
        }
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      相关资源
      最近更新 更多