我有一个从不同时区使用的应用程序。如果有人在纽约(东部时间)输入数据库,它应该显示在纽约时间输入的人(它在大多数应用程序中都会这样做)。但是,当来自时区以外的人查看它时,时间似乎被转换为该用户的时区。
我正在尝试停止转换或转换到正确的时间。它被正确地传递给 javascript,它是在 javascript 中发现问题的地方。
对象的日期字符串显示(例如)“Fri Aug 21 12:30:00 PDT 2013”。
我已修改此功能以使用美国夏令时并使用中央时区。我还使用此函数来处理从格式到 unix 纪元的日期。
我在想,如果“PDT”或任何其他时区标识符(“CST”、“MST”、“PST”等),那么转换可能不会发生。我会注意到,除了“PDT”之外的时间戳是我需要的正确时间,但是在它提前 2 小时显示的地方会发生一些事情。
我的主要问题是,如果有人在 2013 年 7 月 24 日下午 1 点在纽约输入,它仍然应该说,无论我在哪个时区查看该数据。这是问题所在:
- 用户于 2013 年 7 月 24 日下午 3:00 从某个位置(并不总是纽约,但它很容易用作示例)输入系统。
- 它是从数据库正确地通过应用程序发送到 javascript (2013 年 7 月 24 日下午 3:00)
- 如果我在不同的时区(比方说中央),则日期在 javascript 的对象中显示为(Wed Jul 24 3:00 CDT 2013)。<--如您所见,除了“CDT”的时区标识符之外,技术上仍然是正确的。如果时间大致相同,如果有办法从中删除该标识符,我想我会很高兴。
- 没有调用其他函数,它现在将日期显示为 2013 年 7 月 24 日 2:00。
任何想法都会非常有帮助。这是我的转换函数:
function calcTime(date) {
//This is to pad the date with a zero if the
//number is less than 10. eg 7/1/2013 = 07/01/2013
function pad(num) {
num = num.toString();
if (num.length == 1) return "0" + num;
return num;
}
//I needed to have my date passed in as string
//This will set the date to the string and then get the time from
//that date. It will then set that time.
var date = new Date(date);
var time = date.getTime();
date.setTime(time);
var year = date.getUTCFullYear();
//This nifty little thing will get you the daylight savings time
// and adjust correctly. In the US, prior to 2006 the DST started
//first sunday in April and ended in the last sunday in October.
//After 2007, it was changed to start on the 2nd sunday in March
//and end in the first sunday in November. If you are working
//with a different country, please follow this link for
//the equations in relation to their daylight savings time.
//http://www.webexhibits.org/daylightsaving/i.html
if (year <= 2006) {
var start_day = (2 + ((((6 * year) - year / 4) % 7) + 1));
var DST_start = new Date(Date.UTC(year, 3, start_day, 1, 0, 0));
var end_day = (31 - ((((5 * year) / 4) + 1) % 7));
var DST_end = new Date(Date.UTC(year, 9, end_day, 1, 0, 0));
}
if (year >= 2007) {
start_day = (14 - ((1 + (year * 5) / 4) % 7));
DST_start = new Date(Date.UTC(year, 2, start_day, 1, 0, 0));
end_day = (7 - ((1 + (year * 5) / 4) % 7));
DST_end = new Date(Date.UTC(year, 10, end_day, 1, 0, 0));
}
//This function is supposed to make sure no matter where you are,
//you can see this in US central time. Obviously you will need to
//change this if you need a different time zone.
//All timezones to the west of GMT to the International Date Line will be negative.
//Timezones east of GMT to the International Date Line will be positive.
//The below offset is if it is DST, it adjusts the time accordingly.
var centralOffset = -6 * 60 * 60 * 1000;
if (date > DST_start && date < DST_end) centralOffset = -5 * 60 * 60 * 1000;
date.setTime(time + centralOffset);
//This will call the pad function to return a two digit month/day format.
//Dates are zero based in JS and needed to add 1 to the UTCMonth so we receive the
//current month
var centralTime = pad(date.getUTCMonth() + 1) + "/" +
pad(date.getUTCDate()) + "/" + date.getUTCFullYear() +
" " + pad(date.getUTCHours()) + ":" +
pad(date.getUTCMinutes()) + ":" + pad(date.getUTCSeconds());
return centralTime;
};
//Test Data
var date = "7/25/2013 09:30:00";
calcTime(date);
更新: 我运气不太好。我已经能够成功删除时区标识符,但是当我有这个新的正确日期字符串并尝试将其传递给新的 Date 函数时,它会将标识符重新放入。请参阅下面的更新代码。
function getDateTime() {
function pad(num) {
num = num.toString();
if (num.length == 1) return "0" + num;
return num;
}
var timezones = ["PST", "PDT", "MST", "MDT", "CST", "CDT", "EST", "EDT", "GMT"];
var date = this; //this = "Wed Aug 21 14:38:00 CST 2013" or whatever date is being passed in
//This converts date to string so it can remove the time zone identifier.
date = date.toDateString();
var length = timezones.length;
while (length--) {
if (date.indexOf(timezones[length]) != -1) {
date = date.replace(timezones[length], "");
}
}
date = new Date(date); // This pretty much defeats all that I did above.
//date = Date(date); //This will give the current date and time.
var month = pad(date.getMonth() + 1);
var day = pad(date.getDate());
var year = date.getFullYear();
var hour = pad(date.getHours());
var minute = pad(date.getMinutes());
var second = pad(date.getSeconds());
if (hour > 12) {
var timestring = hour.toString() + ":" + minute.toString() + ":" + second.toString() + " PM";
} else {
timestring = hour.toString() + ":" + minute.toString() + ":" + second.toString() + " AM";
}
var output = month.toString() + "/" + day.toString() + "/" + year.toString() + " " + timestring;
return output;
}
希望这可以消除其他人可能对原始帖子产生的任何困惑。