我正在尝试根据抽奖时间创建一个倒计时。
如果当前日期时间大于当前星期三,那么我们会显示当前星期六的日期时间。
如果当前日期时间大于当前星期六,那么我们将显示下周三的日期时间。
我不希望它显示当天的最后一小时。
我正在使用时刻和时刻。
var current = new Date(moment().tz('America/New_York').format('YYYY-MM-DDTHH:mm:ss'));
var wednesday = new Date().wednesday(); //if we are past the current Wednesday, it returns next weeks Wednesday
var wednesdayLimit = new Date(wednesday.getFullYear(), wednesday.getMonth(), wednesday.getDate(), 22, 59, 00);
var saturday = new Date().saturday();
var saturdayLimit = new Date(saturday.getFullYear(), saturday.getMonth(), saturday.getDate(), 22, 59, 00);
if (current > wednesdayLimit && current < saturdayLimit)
{
var temp = moment(saturdayLimit).format('YYYY-MM-DD HH:mm');
$('.countdown').data('date', temp);
$(".countdown").TimeCircles();
}
else if (current > saturdayLimit && current < wenesdayLimit)
{
var temp = moment(wednesdayLimit).format('YYYY-MM-DD HH:mm');
$('.countdown').data('date', temp);
$(".countdown").TimeCircles();
}
也许我过度复杂了。
https://jsfiddle.net/1bLj4m2t/11/
更新
对不起,让我试着澄清一下。倒计时不应在周三或周六的22:59:01 - 23:59:59之间显示。一天的最后1小时1分钟。这就是为什么我创建了* Limit vars并将它们调整为22:59:00。
因此要显示wednesdayLimit,当前日期时间需要介于两者之间
Thursdays 00:00:00 and Saturday 22:59:00
现在显示saturdayLimit当前日期时间需要介于两者之间
Sunday 00:00:00 and Wednesday 22:59:00
更新2 https://jsfiddle.net/1bLj4m2t/11/
目前工作但可能是更清洁/更好的方法吗?
答案 0 :(得分:0)
我想你只需要检查今天哪个日期?是周三还是周六?换句话说,检查星期六是否大于星期三,反之亦然。 (或者找出两者中的最小值)
这是更新的小提琴:https://jsfiddle.net/vnathalye/1bLj4m2t/6/
var current = new Date(moment().tz('America/New_York').format('YYYY-MM-DDTHH:mm:ss'));
var wednesday = new Date().wednesday();
var saturday = new Date().saturday();
$('.current').html(current);
$('.wednesday').html(wednesday);
$('.saturday').html(saturday);
var currentWeekday = moment(current).weekday();
var wednesdayWeekday = moment(wednesday).weekday();
var saturdayWeekday = moment(saturday).weekday();
var temp = (currentWeekday == wednesdayWeekday || currentWeekday == saturdayWeekday)? current : Math.min(wednesday, saturday);
$('.countdown').html(moment(temp).fromNow()); // use appropriate time of temp date
更新:添加逻辑以检查当天是星期三/星期六。
更新2 :我已检查过您的更新小提琴并发现您正在使用momentjs&amp; datejs。你真的需要2个图书馆吗?我会坚持一下。
查看current
的不同值以及您在小提琴v11中提到的预期结果我已更新了我的代码@ https://jsfiddle.net/vnathalye/1bLj4m2t/14/
//var current = moment('2016/01/17 08:00:00', 'YYYY/MM/DD HH:mm:ss'); //display Wed
//var current = moment('2016/01/20 00:00:00', 'YYYY/MM/DD HH:mm:ss'); //display Wed
//var current = moment('2016/01/20 22:59:00', 'YYYY/MM/DD HH:mm:ss'); //display Wed last hour
//var current = moment('2016/01/20 23:59:59', 'YYYY/MM/DD HH:mm:ss'); //display Wed last hour
//var current = moment('2016/01/21 00:00:00', 'YYYY/MM/DD HH:mm:ss'); //display Sat
//var current = moment('2016/01/23 00:00:00', 'YYYY/MM/DD HH:mm:ss'); //display Sat
var current = moment('2016/01/23 22:59:01', 'YYYY/MM/DD HH:mm:ss'); //display Sat last hour
var wednesday = moment(current).day(3); // sun=0 ... sat=6
var saturday = moment(current).day(6);
var currentWeekday = current.weekday();
var wednesdayWeekday = wednesday.weekday();
//var saturdayWeekday = moment(saturday).weekday();
var showWednesday = currentWeekday <= wednesdayWeekday;
var temp = showWednesday? wednesday : saturday;
temp = temp.hours(23).minutes(59).seconds(59).milliseconds(999);
$('.current').html(current.toDate());
$('.wednesday').html(wednesday.toDate());
$('.saturday').html(saturday.toDate());
$('.countdown').html((showWednesday? "Wed" : "Sat") +
(temp.diff(current, "minutes") <= 60 ? " last hour" : ""));
注意:
.day()
函数返回当前周的日期(与您正在使用的wednesday()
/ saturday()
不同)。这样,我就可以简单地将current
的工作日与wednesday
的工作日进行比较,以决定是使用wednesday
还是saturday
进行进一步的计算.toDate()
的日期,但您可以使用.format()