Moment.js .is在比较日期与时区偏移之前

时间:2016-10-12 09:03:58

标签: javascript jquery date momentjs

需要比较的两个时刻是:

  1. 今天在某个时区(2016年10月12日星期三08:42:19 GMT + 0000)
  2. 日历中的选定日期(例如2016-10-11)

    以下是我的工作方式:

  3. 
    
    var date = '2016/10/12'; // october 11                
    var offset = '0'; // time zone offset in minutes, 0 = GMT
    var dateToday = moment().utcOffset(offset); // get a moment now with the time zone offset
    var selectedDate = moment(date, ["YYYY-MM-DD"]).utcOffset(offset); // and a moment that is the date of the day selected
    
    alert(dateToday);
    alert(selectedDate);
    
    if (selectedDate.isBefore(dateToday, 'day')) {                                   
      alert('That date is in the past!');
    } else {
      alert('Great!');
    }
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://momentjs.com/downloads/moment.js"></script>
    &#13;
    &#13;
    &#13;

    问题似乎是因为我在巴黎时间(+02:00)我在巴黎时间(+02:00)以下选择的任何偏移,例如GMT(+00:00)然后设置选择日期过去1点,晚上10点。

    所以我的问题是如何正确检查日期(2016-10-12 GMT)是否已过去?

    更新

    我现在通过使用格式函数提取日期而不考虑时区来解决它。然后我创建了全新的时刻,将日期字符串明确地传递到这样的时刻:

    &#13;
    &#13;
    var dateToday = moment().utcOffset(offset);      // get a moment now with the time zone offset
    var selectedDate = moment(date, ['YYYY-MM-DD']); // and a moment that is the date of the day selected
    
    var dateToday    = dateToday.format('YYYY-MM-DD');
    var selectedDate = selectedDate.format('YYYY-MM-DD');
    
    var dateToday    = moment(dateToday, ['YYYY-MM-DD']);
    var selectedDate = moment(selectedDate, ['YYYY-MM-DD']);
    &#13;
    &#13;
    &#13;

1 个答案:

答案 0 :(得分:1)

如何设置selectedDate小时和分钟00:00或为其指定偏移?

var date = new Date();
var offset = date.getTimezoneOffset() * (-1)
selectedDate.add(offset, "hours");
相关问题