使用 jQuery datepicker 计算时区 [英] Accounting for Timezone with jQuery datepicker

查看:13
本文介绍了使用 jQuery datepicker 计算时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery 日期选择器,其 minDate 为 4,不包括周末和节假日,并调整 minDate 以排除当天下午 2 点之后.现在我需要以某种方式考虑时区,因为我希望这是基于我的时区,PT,而不是用户的时区.我知道我 getTimeZoneOffset 可能是一个可行的选择,但我正在努力解决如何&在哪里将它添加到我当前的设置中.

I'm using jQuery datepicker with a minDate of 4 excluding weekends and holidays and adjusting the minDate to exclude the current day after 2PM. Now I need to somehow factor in the timezone because I want this to be based on my timezone, PT, not the user's timezone. I know I getTimeZoneOffset might be a viable option but I'm struggling with how & where to add this in to my current setup.

<script type="text/javascript">
$(document).ready(function() {

    //holidays
    var natDays = [
      [1, 1, 'New Year'], //2014
      [1, 20, 'Martin Luther King'], //2014
      [2, 17, 'Washingtons Birthday'], //2014       
      [5, 26, 'Memorial Day'], //2014
      [7, 4, 'Independence Day'], //2014
      [9, 1, 'Labour Day'], //2014
      [10, 14, 'Columbus Day'], //2013
      [11, 11, 'Veterans Day'], //2013
      [11, 28, 'Thanks Giving Day'], //2013 
      [12, 25, 'Christmas'] //2013     
 ];

    var dateMin = new Date();
    dateMin.setDate(dateMin.getDate() + (dateMin.getHours() >= 14 ? 1 : 0));
    AddBusinessDays(dateMin, 4);

    function AddBusinessDays(curdate, weekDaysToAdd) {
        while (weekDaysToAdd > 0) {
            curdate.setDate(curdate.getDate() + 1);
            //check if current day is business day
            if (noWeekendsOrHolidays(curdate)[0]) {
                weekDaysToAdd--;
            }
        }
    }    

    function noWeekendsOrHolidays(date) {
        var noWeekend = $.datepicker.noWeekends(date);
        if (noWeekend[0]) {
            return nationalDays(date);
        } else {
            return noWeekend;
        }
    }
    function nationalDays(date) {
        for (i = 0; i < natDays.length; i++) {
            if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) {
                 return [false, natDays[i][2] + '_day'];
            }
        }
        return [true, ''];
    }
      $('#datepicker').datepicker({ 
            inline: true,
            beforeShowDay: noWeekendsOrHolidays,           
            showOn: "both",            
            firstDay: 0,
            dateformat: "dd/mm/yy",
            changeFirstDay: false,
            showButtonPanel: true,       
            minDate: dateMin            
    });
  });
  </script>

<p>
<label for="datepicker">Desired Delivery Date: </label>
  <input class="input-medium" type="text" id="datepicker" placeholder="ex. 01/01/2013" name="properties[Delivery Date]" readonly />
  <label><font size=1>Need it faster? Please call us! (800) 880-0307</font>
  </label></p>
<style>
  #datepicker { height: 20px; }
  #datepicker {-webkit-border-radius: 0 3px 3px 0; -moz-border-radius: 0 3px 3px 0; border-radius: 0 3px 3px 0;}
</style>

推荐答案

试试这个:

var localTime = new Date();
var ptTime = new Date();
ptTime.setMinutes(ptTime.getMinutes() + localTime.getTimezoneOffset() - 420);
alert(localTime + ' ' + localTime.getTimezoneOffset() + ' ' + ptTime);

这篇关于使用 jQuery datepicker 计算时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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