Javascript 如何将用户时区转换为UTC?
我在我的网站上使用TimeIt代码,可以在以下位置找到: 这是代码的直接链接: 看起来是这样的:Javascript 如何将用户时区转换为UTC?,javascript,jquery,Javascript,Jquery,我在我的网站上使用TimeIt代码,可以在以下位置找到: 这是代码的直接链接: 看起来是这样的: //version 3. 2017-08-13 function timeit() { var next_run_array = []; //array of dates/time on a page used to rerun function if a change should happen during the session var curDate = new Date();
//version 3. 2017-08-13
function timeit() {
var next_run_array = []; //array of dates/time on a page used to rerun function if a change should happen during the session
var curDate = new Date();
Date.prototype.yyyymmdd = function() {
var mm = this.getMonth() + 1;
var dd = this.getDate();
return [this.getFullYear(),
(mm > 9 ? '' : '0') + mm,
(dd > 9 ? '' : '0') + dd
].join('-');
};
var curDateYMD = curDate.yyyymmdd();
$('.timeit').each(function() {
var end = $(this).data('end'),
start = $(this).data('start');
//check if date or time value has valid format and push it to the list of refresh anchors
var startDate = checkdate(start, this);
var endDate = checkdate(end, this);
nextrun(startDate);
nextrun(endDate);
//add a datetime when the page needs to be refreshed (now+24 hrs time span only)
function nextrun(date) {
var nextruntimeout = date - curDate;
if (nextruntimeout < 1000 * 60 * 60 * 24 && nextruntimeout > 1000) {
next_run_array.push(nextruntimeout);
}
}
// Main Function
//check if the evend outside of a desired time span
if (((startDate < endDate) && (startDate > curDate || endDate < curDate)) ||
((startDate > endDate) && (startDate >= curDate) && (endDate <= curDate))
) {
$(this).addClass('hidden');
} else {
$(this).removeClass('hidden');
}
//Support Functions
//correct data creation from a string. accepted format YYYY-MM-DD HH:MM
function parseISO8601(d) {
var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)?.(\d\d)?.(\d\d)\s*$/,
date = new Date(NaN),
datenew,
month,
dateString=d.substr(0, d.indexOf(' '));
parts = isoExp.exec(d);
if(parts) {
month = +parts[2];
date.setFullYear(parts[1], month - 1, parts[3]);
if(month != date.getMonth() + 1) {
date.setTime(NaN);
}
date = new Date(parts[1], month - 1, parts[3], parts[4], parts[5])
}
return date;
}
//unification of the date string to the format YYYY-MM-DD HH:MM
function checkdate(date, obj) {
if (date) {
//check if only time is set (HH:MM); if so, add today's date
if (String(date).length < 6 && String(date).indexOf(":") > -1) {
date = curDateYMD + ' ' + String(date);
}
//check if only date is set; if so add 00:00 to the end of date
if (String(date).indexOf(":") == -1) {
date = date + ' 00:00';
}
//check if date is valid (avoid valid time)
var res = date.split(":"),
h = String(res.slice(0, 1)),
hours = h.substr(h.length - 2),
minutes = res.slice(1);
var timetest = (hours < 24 && minutes < 60) ? true : false;
//check if date is could be created from a value; if fails try to parse a string to a format
var returndate = new Date(date);
if (returndate == 'Invalid Date') {
var returndate = parseISO8601(date);
};
if (returndate == 'Invalid Date' || !timetest) {
//highlight the element if the is an error. use own \.error class if needed
$(obj).addClass("error").attr('title', '"' + date + '" date is incorrect; please use YYYY-MM-DD HH:MM format');
}
return returndate.getTime();
} else {
//if datetime is not set, just return current date-time
return curDate.getTime();
}
}
});
/* Schedule next runs */
if (next_run_array.length > 0) {
var nextruntime = Math.min.apply(null, next_run_array);
console.log("next run of timeit function is in " + nextruntime / 1000 + "seconds");
setTimeout(function() {
timeit();
}, nextruntime);
}
}
timeit();
(
//版本3。2017-08-13
函数timeit(){
var next_run_array=[];//页面上的日期/时间数组,用于在会话期间发生更改时重新运行函数
var curDate=新日期();
Date.prototype.yyyymmdd=函数(){
var mm=this.getMonth()+1;
var dd=this.getDate();
返回[this.getFullYear(),
(毫米>9?“”:“0”)毫米+毫米,
(dd>9?“”:“0”)+dd
]。加入(“-”);
};
var curDateYMD=curDate.yyyyymmd();
$('.timeit')。每个(函数(){
var end=$(this).data('end'),
start=$(this).data('start');
//检查日期或时间值的格式是否有效,并将其推送到刷新锚点列表中
var startDate=检查日期(开始,此日期);
var endDate=检查日期(结束,本);
nextrun(起始日期);
nextrun(结束日期);
//添加需要刷新页面的日期时间(现在仅限24小时)
函数nextrun(日期){
var nextruntimeout=date-curDate;
如果(下一次超时<1000*60*60*24和下一次超时>1000){
下一步运行数组。推送(nextruntimeout);
}
}
//主要功能
//检查均匀度是否超出所需的时间范围
如果((开始日期<结束日期)和&(开始日期>截止日期| |结束日期<截止日期))||
((startDate>endDate)&&&(startDate>=curDate)&&(endDate由于Weebly的原因,您不能使用服务器端脚本…您将不得不依赖于客户端的时钟,它可以调整。并且隐藏的类可以很容易地删除…但似乎您没有选择
现在,我建议你忘记这个插件
当涉及到JavaScript/jQuery中的日期时,我总是建议使用非常易于使用(您将不再需要执行复杂的计算)且有完整文档记录的日期,这样您就可以随心所欲了
在这里,基于数据属性中的开始/结束日期的内容隐藏如下所示:
//version 3. 2017-08-13
function timeit() {
var next_run_array = []; //array of dates/time on a page used to rerun function if a change should happen during the session
var curDate = new Date();
Date.prototype.yyyymmdd = function() {
var mm = this.getMonth() + 1;
var dd = this.getDate();
return [this.getFullYear(),
(mm > 9 ? '' : '0') + mm,
(dd > 9 ? '' : '0') + dd
].join('-');
};
var curDateYMD = curDate.yyyymmdd();
$('.timeit').each(function() {
var end = $(this).data('end'),
start = $(this).data('start');
//check if date or time value has valid format and push it to the list of refresh anchors
var startDate = checkdate(start, this);
var endDate = checkdate(end, this);
nextrun(startDate);
nextrun(endDate);
//add a datetime when the page needs to be refreshed (now+24 hrs time span only)
function nextrun(date) {
var nextruntimeout = date - curDate;
if (nextruntimeout < 1000 * 60 * 60 * 24 && nextruntimeout > 1000) {
next_run_array.push(nextruntimeout);
}
}
// Main Function
//check if the evend outside of a desired time span
if (((startDate < endDate) && (startDate > curDate || endDate < curDate)) ||
((startDate > endDate) && (startDate >= curDate) && (endDate <= curDate))
) {
$(this).addClass('hidden');
} else {
$(this).removeClass('hidden');
}
//Support Functions
//correct data creation from a string. accepted format YYYY-MM-DD HH:MM
function parseISO8601(d) {
var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)?.(\d\d)?.(\d\d)\s*$/,
date = new Date(NaN),
datenew,
month,
dateString=d.substr(0, d.indexOf(' '));
parts = isoExp.exec(d);
if(parts) {
month = +parts[2];
date.setFullYear(parts[1], month - 1, parts[3]);
if(month != date.getMonth() + 1) {
date.setTime(NaN);
}
date = new Date(parts[1], month - 1, parts[3], parts[4], parts[5])
}
return date;
}
//unification of the date string to the format YYYY-MM-DD HH:MM
function checkdate(date, obj) {
if (date) {
//check if only time is set (HH:MM); if so, add today's date
if (String(date).length < 6 && String(date).indexOf(":") > -1) {
date = curDateYMD + ' ' + String(date);
}
//check if only date is set; if so add 00:00 to the end of date
if (String(date).indexOf(":") == -1) {
date = date + ' 00:00';
}
//check if date is valid (avoid valid time)
var res = date.split(":"),
h = String(res.slice(0, 1)),
hours = h.substr(h.length - 2),
minutes = res.slice(1);
var timetest = (hours < 24 && minutes < 60) ? true : false;
//check if date is could be created from a value; if fails try to parse a string to a format
var returndate = new Date(date);
if (returndate == 'Invalid Date') {
var returndate = parseISO8601(date);
};
if (returndate == 'Invalid Date' || !timetest) {
//highlight the element if the is an error. use own \.error class if needed
$(obj).addClass("error").attr('title', '"' + date + '" date is incorrect; please use YYYY-MM-DD HH:MM format');
}
return returndate.getTime();
} else {
//if datetime is not set, just return current date-time
return curDate.getTime();
}
}
});
/* Schedule next runs */
if (next_run_array.length > 0) {
var nextruntime = Math.min.apply(null, next_run_array);
console.log("next run of timeit function is in " + nextruntime / 1000 + "seconds");
setTimeout(function() {
timeit();
}, nextruntime);
}
}
timeit();
(
$(文档).ready(函数(){
var utc_date=moment().utc().format(“YYYY-MM-DD HH:MM”);//客户的日期/时间(utc)
$(“.timeit”).each(函数(){
var start=力矩($(this).data(“start”).format(“YYYY-MM-DD HH:MM”);
var end=力矩($(此).data(“end”).format(“YYYY-MM-DD HH:MM”);
console.log((utc_date>start&&utc_datestart&&utc_dateThank,我真的很感谢您花时间研究这一点。不幸的是,我运行了代码,但它仍然无法与utc时间配合使用。波兰是12:39,英国是11:39。我将开始日期更改为2019-02-11 11:41,因此在接下来的2分钟内内容不应该可见,但不管怎么说。很抱歉…我更改了比较,所以它比较了日期对象中的字符串。我还颠倒了逻辑。我想我昨天累了!哈哈