javascript - 不同国家/地区的完整日历日期有所不同

标签 javascript jquery fullcalendar fullcalendar-scheduler

我正在使用 fullcalendar.js,但我遇到时区问题。 它在我住的地方工作得很好,但我住在加拿大伯灵顿的客户没有得到他点击的相同日期。我已将区域设置为 -05:00(他们的区域),但没有帮助,有没有办法将时区设置为本地? 因此,无论您在哪里访问它都会显示完整的日历。

enter image description here

这些突出显示的日期在加拿大显示为 6,与点击的日期相同。

这是我用来在 dayclick 上打开模式的代码

   dayClick: function(date, jsEvent, view) {
    var click = date._d;
    var month = new Array();
    month[0] = "January";
    month[1] = "February";
    month[2] = "March";
    month[3] = "April";
    month[4] = "May";
    month[5] = "June";
    month[6] = "July";
    month[7] = "August";
    month[8] = "September";
    month[9] = "October";
    month[10] = "November";
    month[11] = "December";
    var weekday = new Array(7);
    weekday[0]=  "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";
    $("#myModal").modal("show");

    document.getElementById("date").setAttribute("value", click.getFullYear() +"-"+ ("0"+ (click.getMonth()+1)).slice(-2)+"-"+("0" + click.getDate()).slice(-2));

    document.getElementById("month").setAttribute("value", month[click.getMonth()]);
    document.getElementById("str_day").setAttribute("value", ("0" + click.getDate()).slice(-2));
    document.getElementById("end_day").setAttribute("value", weekday[click.getDay()]);
  },
  eventResize: function(event, delta, revertFunc) {
    console.log(event);
    var title = event.title;
    var end = event.end.format();
    var start = event.start.format();
    $.ajax({
      url: 'fullcalender/process.php',
      data: 'type=resetdate&title='+title+'&start='+start+'&end='+end+'&eventid='+event.id,
      type: 'POST',
      dataType: 'json',
      success: function(response){
        if(response.status != 'success')
          revertFunc();
      },
      error: function(e){
        revertFunc();
        alert('Error processing your request: '+e.responseText);
      }
    });

这是用于添加到数据库中的代码

if($type == 'new')
{
    $startdate = $_POST['startdate'].'+'.$_POST['zone'];
    $title = $_POST['title'];
    $insert = mysqli_query($con,"INSERT INTO calendar(`title`, `startdate`, `enddate`, `allDay`) VALUES('$title','$startdate','$startdate','false')");
    $lastid = mysqli_insert_id($con);
    echo json_encode(array('status'=>'success','eventid'=>$lastid));
}

最佳答案

就存储时间而言,它们应该存储为 UTC 时间戳。为了检索时区,我使用 jstz要获取用户时区,就像这两行一样简单。

var tz = jstz.determine();
var timezone = tz.name();

这将为您提供一个像“美国/芝加哥”这样的时区,无论用户在哪里,这都很棒,因为您不必询问用户他们的时区,并且它可以在任何地方工作。然后传递你的timezone连同您对事件的请求,您可以在检索事件时使用服务器端语言从 UTC 转换为提供的时区。

events: {
    url: "calendarManager.php?request=get&timezone=" + timezone + "&userid=" + userID
},

您还应该使用

timezone: "local",

在您的日历配置中,然后在使用上述方法返回事件时从 UTC 转换为用户本地时区。

关于javascript - 不同国家/地区的完整日历日期有所不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40512643/

相关文章:

jquery - FullCalendar 插槽 5 分钟不起作用

javascript - 脚本似乎在 jQuery 加载之前运行 - 未捕获的 ReferenceError : $ is not defined

javascript - 在业务催化剂结帐中选择 PayPal 时如何隐藏信用卡字段

jquery - Fullcalendar 资源未在版本 4 中显示

javascript - 组件中的两种方式绑定(bind)不起作用

javascript - 如何在第二个 SwitchMap 中返回 2 个响应对象并在 subscribe 方法中使用

javascript - 查找所选元素的所有 CSS 类规则(来自多个样式表)及其事件属性的列表

javascript - 通过赋值嵌套在对象数组解构中

javascript - 在 jquery 弹出窗口中禁用背景

jquery - FullCalendar 和 MySQL——跨多个月的事件