需要比较的两个时刻是:
日历中的选定日期(例如2016-10-11)
以下是我的工作方式:
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;
问题似乎是因为我在巴黎时间(+02:00)我在巴黎时间(+02:00)以下选择的任何偏移,例如GMT(+00:00)然后设置选择日期过去1点,晚上10点。
所以我的问题是如何正确检查日期(2016-10-12 GMT)是否已过去?
更新
我现在通过使用格式函数提取日期而不考虑时区来解决它。然后我创建了全新的时刻,将日期字符串明确地传递到这样的时刻:
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;
答案 0 :(得分:1)
如何设置selectedDate小时和分钟00:00或为其指定偏移?
var date = new Date();
var offset = date.getTimezoneOffset() * (-1)
selectedDate.add(offset, "hours");