返回倒数计时器的下一个本地化日期 [英] Return next localized date for countdown timer

查看:178
本文介绍了返回倒数计时器的下一个本地化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码进行计算,以查找本地化的UTC时间。根据UTC时间,还有另一个功能可以为我计算下一个事件日期和时间。如果时间是星期日@ 12和星期三@ 8之间,那么会给我星期五的活动日期,否则会在星期天给我下一个活动日期。然后我拿这个计算的日期,将它分开,并将其作为参数提供给倒数计时器。

I have this code that does a calculation to find the a localized UTC time. Based on that UTC time there's another function that will calculate a next event date and time for me. If the time is between Sunday @ 12 and Wednesday @ 8 it will give me wednesdays event date, otherwise it will give me the next event date on Sunday. I then take that calculated date, split it apart and feed it in as parameter to a countdown timer.

我想知道是否有更优化的方法来做到这一点,特别是在条件语句中,我将时间分解成参数。

I'm wondering if there's a more optimize way to do this, particularly with the conditional statement and where I split apart the time into parameters.

$(function () {
// Calculate the time locally
function calcLocalizedTime() {
    var d = new Date(); // Date for current location
    var utc = d.getTime() + (d.getTimezoneOffset() * 60000); // convert to msec add local time zone offset get UTC time in msec
    var nd = new Date(utc + (3600000 * -5));
    return nd; // return localized datetime
}

// Calculate the next Webcast DateTime
function calcNextWebCast(date) {
    var localizedTime = new Date(date||new Date()); // localized datetime
    var nextDate = new Date(date||new Date()); // Next webcast datetime object
    var today = localizedTime.getDay() + '' + localizedTime.getHours(); // format localized datetime for comparison

    if (today >= 012 && today <=320 ) { // is today between Sun @ 12 or Wednesday @ 8
        nextDate.setDate(nextDate.getDate() + (3 - 1 - nextDate.getDay() + 7) % 7 + 1);
        nextDate.setHours(20,0,0,0);
    } else {
        nextDate.setDate(nextDate.getDate() + (3 - 4 - nextDate.getDay() + 7) % 7 + 1);
        nextDate.setHours(12,0,0,0);
    }

    return nextDate;
}

var localizedTime = calcLocalizedTime();
var nextWebCast = calcNextWebCast(localizedTime);
var m = nextWebCast.getMonth() + 1;
var d = nextWebCast.getDate();
var y = nextWebCast.getFullYear();
var hh = nextWebCast.getHours();
var mm = nextWebCast.getMinutes();

$('#defaultCountdown').countdown({until: $.countdown.UTCDate(-5, y, m-1, d, hh, mm)});

});


推荐答案


我想知道如果有一个更优化的方法来做到这一点,特别是在条件语句和我将时间分解为参数的地方。

I'm wondering if there's a more optimize way to do this, particularly with the conditional statement and where I split apart the time into parameters.

使用dispatch表替换条件语句和正则表达式来替换范围:

Use a dispatch table to replace the conditional statement and a regexp to replace the range:

function foo(nextDate)
 {
 calcNextWebCast.nextDate.setDate(calcNextWebCast.nextDate.getDate() + (3 - 1 - nextDate.getDay() + 7) % 7 + 1);
 calcNextWebCast.nextDate.setHours(20,0,0,0);
 }

function bar(nextDate)
 {
 calcNextWebCast.nextDate.setDate(calcNextWebCast.nextDate.getDate() + (3 - 4 - nextDate.getDay() + 7) % 7 + 1);
 calcNextWebCast.nextDate.setHours(12,0,0,0);
 }


function calcNextWebCast(date) {
 calcNextWebCast.nextDate = new Date(date||new Date()); // Next webcast datetime object

 var localizedTime = new Date(date||new Date()); // localized datetime
 var today = localizedTime.getDay() + '' + localizedTime.getHours(); // format localized datetime for comparison

 var range = "012|[0][1][3-9]|[0][2-9][0-9]|[1-2][0-9][0-9]|[3][0-1][0-9]|320";

 var datemap = { "true":foo, "false":bar }

 var jump = RegExp(range).test(today);

 var go = datemap[jump];

 go();

 return nextDate;
 }

参考

匹配数字范围与正则表达式

这篇关于返回倒数计时器的下一个本地化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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