一开始是想用 spacetime 这个js库, 然后就因为一个提示就要引入接近40k的js文件,怎么想都觉得有些不合适. 所以自己实现了
function pageTsVsServerTs(p_date, s_date_str) {
var sTz = "<?php echo date_default_timezone_get()?>";
var stz = "<?php echo date('O')?>";
var stz_offset = <?php echo -1 * (date('Z') / 60)?>;
var ptz_offset = (new Date).getTimezoneOffset(); // - 480
// UTC as base time point
var p_now = parseInt((p_date).getTime() / 1000);
var p_now_utc = p_now - (ptz_offset * 60); // 网页侧基准原点
var s_now = parseInt((new Date(s_date_str + ' GMT' + stz)).getTime() / 1000);
var s_now_utc = s_now - (stz_offset * 60);
return p_now_utc > s_now_utc;
//console.log([p_now_utc, s_now_utc, p_now_utc > s_now_utc]);
}
上面想复杂了, 写了个错误的函数
function pageTsVsServerTs(p_date, s_date_str) {
var sTz = "<?php echo date_default_timezone_get()?>";
var stz = "<?php echo date('O')?>";
var stz_offset = <?php echo -1 * (date('Z') / 60)?>;
var ptz_offset = (new Date).getTimezoneOffset();// - 480
var interval_second = (stz_offset - ptz_offset) * 60;
var s_ts = parseInt((new Date(s_date_str)).getTime() / 1000);
var p_ts = parseInt((p_date).getTime() / 1000);
return p_ts > (s_ts + interval_second);
}
网友评论