0

我有一個從各個時區使用的應用程序。如果有人在紐約(東部時間)進入數據庫,應該顯示在紐約時間輸入的人(它通過大部分應用程序執行)。但是,當來自時區之外的人查看它時,時間似乎轉換爲該用戶的時區。在JavaScript中防止隱式日期轉換

我正在嘗試停止轉換或將轉換轉換爲正確的時間。它被正確地傳遞給javascript,它在問題被發現的javascript中。

該對象的日期字符串顯示(例如)「2014年8月21日星期五12:30:00 PDT」。

我已修改this function以使用美國夏令時並使用中央時區。我也用this function來處理從格式到unix時代的日期。

我在想,如果「PDT」或任何其他時區標識符(「CST」,「MST」,「PST」等),那麼也許轉換不會發生。我會注意到除了「PDT」以外的時間戳是我需要的正確時間,但是在它提前2小時顯示它的地方會發生一些事情。

我的主要問題是,如果有人在紐約時間2013年7月24日下午1點進入,它應該仍然會說,無論我查看該數據時是在哪個時區。這裏的問題是:

  1. 用戶在從一個位置輸入系統(它並不總是 紐約,但它很容易作爲一個例子來使用)在 下午3:00在2013年7月24日。
  2. 它是從數據庫,並通過應用 正確的JavaScript發送(2013年7月24日下午3:00)
  3. 如果我在adifferent時區(比方說中央)的日期是 在theobject所示在javascript中(Wed Jul 24 3:00 CDT 2013)。 < - 正如你所看到的,除了那個 時區標識符「CDT」之外,仍然在技術上是正確的。如果時間通常是相同的,如果 有一些從中刪除該標識符,我認爲我 將是黃金。
  4. 沒有其他函數被調用,它現在顯示日期爲7/24/2013 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); 

UPDATE: 我沒有多少運氣。我已經能夠成功刪除時區標識符,但是當我有這個正確日期的新字符串並嘗試將它傳遞給新的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; 

} 

希望這可以消除任何其他人可能從原始帖子中產生的任何混淆。

回答

1

使用多個字符串替換保留原始字符串:

var foo = "2013-10-01T09:00:00.000-04:00"; 
var bar = foo.substr(-6); 
var baz = foo.substr(11,2) 
var isoDate = new Date(foo).toISOString().replace("Z",bar).replace(/T../,"T"+baz); 
var hours = String(baz).concat(Number(baz) < 12 ? "PM" : "AM").replace(/^0/,"")