gpt4 book ai didi

javascript - 日期 - 每天的特定时间,特定时区

转载 作者:行者123 更新时间:2023-12-03 06:45:35 26 4
gpt4 key购买 nike

** 更新**

使用 moment.js 会不会更容易?我想要做的就是显示一个倒计时器,每天倒计时至太平洋标准时间下午 3 点(美国东部标准时间 6 点)。

<小时/>

寻找一种方法让 javascript get new Date() 来使用特定的时区,而不是用户的时间。

我有:

function ShowTime()  {
var now = new Date();
var hrs = 14-now.getHours();
var mins = 59-now.getMinutes();
var secs = 59-now.getSeconds();
timeLeft etc etc...

jsfiddle(有效),但我认为这是我自己机器的时间: http://jsfiddle.net/36sqeq8x/

还是不行...我只需要每天倒计时到太平洋标准时间下午 3 点(美国东部标准时间 6 点)...而不尝试根据用户的时区在用户端计算它,就像如果可以从 GMT 计算一样,这可能会起作用吗?但这有可能吗?

也许是这样的组合? https://jsfiddle.net/salman/PxzJu/

最佳答案

实现此目的的一种方法是为当前日期和 GMT-0800 时区的 15:00 创建一个日期。如果已经过去了,请添加一天。如果距离 future 超过 1 天(不太可能在如此遥远的西方时区),则减去一天。

然后从中减去当前时间即可得到到下一个太平洋标准时间 15:00 的毫秒数。

下面使用了一些辅助函数,其中最大的是 parseISO 来解析 ISO 8601 日期字符串,以避免被内置日期解析器解析。 toISODate 用于构建 15:00 PDT 的字符串,其他仅用于输出。希望文档和评论足够。

// Create a date for the current date at 15:00 PST (UTC-0800)
// Current date and time
var dLocal = new Date();
// Create Date for same date but time 15:00 at UTC-0800
var dPST = parseISO(toISODate(dLocal) + 'T' + '15:00:00-0800');

// If dPST has passed, add a day
if (dPST < dLocal) dPST.setDate(dPST.getDate() + 1);
// If gap is too big, subtract a day
if (dPST - dLocal > 8.64e7) dPST.setDate(dPST.getDate() - 1);

console.log('Current local: ' + toISOString(dLocal) +
'\nCurrent PST: ' + toISOStringOffset(-480, dLocal) +
'\nNext 3pm PST in PST zone: ' + toISOStringOffset(-480, dPST) +
'\nNext 3pm PST in local zone: ' + toISOString(dPST) +
'\nms to 3pm PST: ' + (dPST - dLocal)
);



/* Parse ISO date string in format yyyy-mm-ddThh:mm:ss.sss+hh:mm or Z
** @param (string} s - string to parse in ISO 8601 extended format
** yyyy-mm-ddThh:mm:ss.sss+/-hh:mm or z
** time zone can omit separator, so +05:30 or +0530
** @returns {Date} - returns a Date object. If any value out of range,
** returns an invalid date.
*/
function parseISO(s) {
// Create base Date object
var date = new Date();
var invalidDate = new Date(NaN);
// Set some defaults
var sign = -1, tzMins = 0;
var tzHr, tzMin;
// Trim leading and trailing whitespace
s = s.replace(/^\s*|\s*$/g,'').toUpperCase();
// Get parts of string and split into numbers
var d = (s.match(/^\d+(-\d+){0,2}/) || [''])[0].split(/\D/);
var t = (s.match(/[\sT]\d+(:\d+){0,2}(\.\d+)?/) || [''])[0].split(/\D/);
var tz = (s.match(/Z|[+\-]\d\d:?\d\d$/) || [''])[0];

// Resolve timezone to minutes, may be Z, +hh:mm or +hhmm
// Splitting into parts makes validation easier
if (tz) {
sign = /^-/.test(tz)? 1 : -1;
tzHr = tz == 'Z'? 0 : tz.substr(1,2);
tzMin = tz == 'Z'? 0 : tz.substr(tz.length - 2, 2)*1;
tzMins = sign * (tzHr*60 + tzMin);
}

// Validation
function isLeap(year){return year % 4 != 0 || year % 100 == 0 && year % 400 != 0}
// Check number of date parts and month is valid
if (d.length > 3 || d[1] < 1 || d[1] > 12) return invalidDate;
// Test day is valid
var monthDays = [,31,28,31,30,31,30,31,31,30,31,30,31];
var monthMax = isLeap(d[0]) && d[1] == 2? 29 : monthDays[d[1]];
if (d[2] < 1 || d[1] > monthMax) return invalidDate;
// Test time parts
if (t.length > 5 || t[1] > 23 || t[2] > 59 || t[3] > 59 || t[4] > 999) return invalidDate;
// Test tz within bounds
if (tzHr > 12 || tzMin > 59) return invalidDate;

// If there's a timezone, use UTC methods, otherwise local
var method = tz? 'UTC' : '';

// Set date values
date['set' + method + 'FullYear'](d[0], (d[1]? d[1]-1 : 0), d[2]||1);
// Set time values - first memeber is '' from separator \s or T
date['set' + method + 'Hours'](t[1] || 0, (+t[2]||0) + tzMins, t[3]||0, t[4]||0);

return date;
}

/* Return ISO 8601 formatted string with local offset, e.g. 2016-06-12T12:43:23.432+05:30
** @param {Date} d - date to craete string from
** @returns {string} in ISO 8601 format with offset
*/
function toISOString(d) {
d = d || new Date();
var offset = d.getTimezoneOffset();
function z(n){return (n<10?'0':'') + n}
// Reverse signe of offset to be consistent with ISO 8601
var offSign = offset < 0? '+' : '-';
offset = Math.abs(offset);
var offHr = z(offset/60 | 0);
var offMin = z(offset%60);
return d.getFullYear() + '-' + z(d.getMonth() + 1) + '-' + z(d.getDate()) + 'T' +
z(d.getHours()) + ':' + z(d.getMinutes()) + ':' + z(d.getSeconds()) + '.' +
('00' + d.getMilliseconds()).slice(-3) + offSign + offHr + ':' + offMin;
}

/* Given a Date, return an ISO 8601 formatted date and time string
** for a particular time zone.
** @param {number} offset - offset in minutes +east, -west, default is local
** @param {Date} d - date to use, default is now
** @returns {string} ISO 8601 formatted string for supplied time zone offset
*/
function toISOStringOffset(offset, d) {
// Copy date if supplied or use now
d = d? new Date(+d) : new Date();
// Prepare offset values
offset = offset || -d.getTimezoneOffset();
var offSign = offset < 0? '-' : '+';
offset = Math.abs(offset);
var offHours = ('0' + (offset/60 | 0)).slice(-2);
var offMins = ('0' + (offset % 60)).slice(-2);

// Apply offset to d
d.setUTCMinutes(d.getUTCMinutes() - offset);

// Return formatted string
return d.getUTCFullYear() +
'-' + ('0' + (d.getUTCMonth()+1)).slice(-2) +
'-' + ('0' + d.getUTCDate()).slice(-2) +
'T' + ('0' + d.getUTCHours()).slice(-2) +
':' + ('0' + d.getUTCMinutes()).slice(-2) +
':' + ('0' + d.getUTCSeconds()).slice(-2) +
'.' + ('000' + d.getUTCMilliseconds()).slice(-3) +
offSign + offHours + ':' + offMins;
}

/* Return an ISO 8601 formatted date string based on local time
** Year must be positive (i.e. doesn't do -ve year)
** @param {Date} date - date object to create date string from
** @returns {string} dates string in yyyy-mm-dd format or default from
** Date.prototype.toString (i.e. "Invalid Date")
*/
function toISODate(date) {
return date.getDate()? ('000' + date.getFullYear()).slice(-4) + '-' +
('0' + (date.getMonth() + 1)).slice(-2) + '-' +
('0' + date.getDate()).slice(-2) : date.toString();
}

关于javascript - 日期 - 每天的特定时间,特定时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37751328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com