javascript - 如何将代码更改为我自己的国家时区?
问题描述
function timeleft(eventTime) {
var now = new Date();
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
var event_time = new Date(eventTime);
var t1 = now_utc.getTime();
//removes one hour from the date - 60 minutes * 60 seconds * 1000 miliseconds - -60*60*1000
var t2 = event_time.getTime();
var time_left = new Date(parseInt(t2-t1));
if(time_left<0) {
return "00:00:00:00";
}
msPerDay = 24 * 60 * 60 * 1000;
days = Math.floor(time_left/msPerDay);
if (days < 10) { days = "0"+days; }
hours = time_left.getUTCHours();
if (hours < 10) { hours = "0"+hours;}
minutes = time_left.getUTCMinutes();
if (minutes < 10) { minutes = "0"+minutes; }
seconds = time_left.getUTCSeconds();
if (seconds < 10) { seconds = "0"+seconds; }
return(days+":"+hours+":"+minutes+":"+seconds);
}
这是下面的完整代码
/*
* jquery-counter plugin
*
* Dual licensed under the MIT and GPL licenses.
*
* http://docs.jquery.com/License
*/
jQuery(function(){
jQuery('.simple_countdown_timer').each(function() {
countdown(jQuery('#'+jQuery(this).attr('id')+' .sct_count'),{
eventTime: jQuery('#'+jQuery(this).attr('id')+' .sct_event_time').text(),
image: jQuery('#'+jQuery(this).attr('id')+' .sct_image').text(),
});
});
});
function countdown(sct_div_id,userOptions)
{
// Default options
var options = {
stepTime: 60,
// startTime and format MUST follow the same format.
// also you cannot specify a format unordered (e.g. hh:ss:mm is wrong)
format: "dd:hh:mm:ss",
eventTime: "Jan, 1, 2013, 00:00",
digitImages: 6,
digitWidth: 13,
digitHeight: 19,
timerEnd: function(){},
image: "http://cdn.live-nhl.com/media-resources/other/countdown-timer/digits-13-19.png"
};
var digits = [], interval;
userOptions.startTime = timeleft(userOptions.eventTime);
// Draw digits in given container
var createDigits = function(where)
{
var c = 0;
var tempStartTime = options.startTime;
// Iterate each startTime digit, if it is not a digit
// we'll asume that it's a separator
for (var i = 0; i < options.startTime.length; i++)
{
if (parseInt(tempStartTime.charAt(i)) >= 0)
{
elem = jQuery('<div id="cnt_' + i + '" class="cntDigit" />').css({
height: options.digitHeight * options.digitImages * 10,
float: 'left', background: 'url(\'' + options.image + '\')',
width: options.digitWidth});
digits.push(elem);
margin(c, -((parseInt(tempStartTime.charAt(i)) * options.digitHeight *
options.digitImages)));
digits[c].__max = 9;
// Add max digits, for example, first digit of minutes (mm) has
// a max of 5. Conditional max is used when the left digit has reach
// the max. For example second "hours" digit has a conditional max of 3
switch (options.format.charAt(i)) {
case 'h':
digits[c].__max = (c % 2 == 0) ? 2: 9;
if (c % 2 != 0)
digits[c].__condmax = 3;
break;
case 'd':
digits[c].__max = 9;
break;
case 'm':
case 's':
digits[c].__max = (c % 2 == 0) ? 5: 9;
}
++c;
}
else
elem = jQuery('<div class="cntDescSeparator"/>').css({float: 'left'})
.text(options.format.charAt(i-1));
where.append('<div>');
where.append(elem);
where.append('</div>');
}
elem = jQuery('<div class="cntDescSeparator"/>').css({float: 'left'})
.text('s');
where.append('<div>');
where.append(elem);
where.append('</div>');
};
// Set or get element margin
var margin = function(elem, val)
{
if (val !== undefined)
return digits[elem].css({'marginTop': val + 'px'});
return parseInt(digits[elem].css('marginTop').replace('px', ''));
};
// Makes the movement. This is done by "digitImages" steps.
var moveStep = function(elem)
{
digits[elem]._digitInitial = -(digits[elem].__max * options.digitHeight * options.digitImages);
return function _move() {
mtop = margin(elem) + options.digitHeight;
if (mtop == options.digitHeight) {
margin(elem, digits[elem]._digitInitial);
if (elem > 0) moveStep(elem - 1)();
else
{
clearInterval(interval);
for (var i=0; i < digits.length; i++) margin(i, 0);
options.timerEnd();
var match_link = digits[elem].parent().parent().parent().children('.match-link').attr('href');
digits[elem].parent().parent().html('<a href="'+match_link+'" class="live-now"><img width="145" height="35" src="http://cdn.live-nhl.com/media-resources/other/WatchNow.png" class="attachment-medium" alt="WatchNow" title="WatchNow"></a>');
return;
}
if ((elem > 0) && (digits[elem].__condmax !== undefined) &&
(digits[elem - 1]._digitInitial == margin(elem - 1)))
margin(elem, -(digits[elem].__condmax * options.digitHeight * options.digitImages));
return;
}
margin(elem, mtop);
if (margin(elem) / options.digitHeight % options.digitImages != 0)
setTimeout(_move, options.stepTime);
if (mtop == 0) digits[elem].__ismax = true;
}
};
jQuery.extend(options, userOptions);
sct_div_id.css({height: options.digitHeight, overflow: 'hidden'});
createDigits(sct_div_id);
interval = setInterval(moveStep(digits.length - 1), 1000);
}
function timeleft(eventTime) {
var now = new Date();
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
var event_time = new Date(eventTime);
var t1 = now_utc.getTime();
//removes one hour from the date - 60 minutes * 60 seconds * 1000 miliseconds - -60*60*1000
var t2 = event_time.getTime();
var time_left = new Date(parseInt(t2-t1));
if(time_left<0) {
return "00:00:00:00";
}
msPerDay = 24 * 60 * 60 * 1000;
days = Math.floor(time_left/msPerDay);
if (days < 10) { days = "0"+days; }
hours = time_left.getUTCHours();
if (hours < 10) { hours = "0"+hours;}
minutes = time_left.getUTCMinutes();
if (minutes < 10) { minutes = "0"+minutes; }
seconds = time_left.getUTCSeconds();
if (seconds < 10) { seconds = "0"+seconds; }
return(days+":"+hours+":"+minutes+":"+seconds);
}
解决方案
您将时间存储在数据库 unix 时间戳中,并通过 java-script 日期函数根据客户端时区将其转换为客户端。 javascript 支持的 Unix 时间戳
推荐阅读
- sql - Group by 和 Pivot 函数给出不同的计数
- typescript - 只允许特定类型的键
- java - 多次访问 SQLite 数据库后,我的应用程序崩溃。唯一约束错误
- java - Java/JVM:使用其地址和签名调用本机 C 方法
- python - 在 python dict 中添加新的键值对
- angular - 当字段值为零时如何使剑道网格单元格为空白
- kotlin - 如果第一次执行引发异常,则不会从指定范围执行第二个异步方法
- php - 如何直接从浏览器 URL 调用自定义插件功能
- windows-installer - Wix 安装包而不是更新
- python - 从不同于 1970-1-1 的年份开始的日期时间索引