2011-11-17 64 views
2

我從服務器獲取字符串,我需要隱藏獲取字符串中的新日期對象..爲此,我試過這個功能,但沒有用,任何人都可以幫助我將字符串轉換爲日期對象?如何將文本字符串更改爲日期對象?

我的代碼是:

var nationZone = { 
     getNewYorkLocalTime : 'getTime.php?lat=40.71417&lan=74.00639', 
     getLondonLocalTime : 'getTime.php?lat=51.5&lan=0.1166667', 
     getChennaiLocalTime : 'getTime.php?lat=13.0833333&lan=80.2833333', 
     getBangaloreLocalTime:'getTime.php?lat=12.9833333&lan=77.5833333' 

    } 

    $.each(nationZone , function(key, value){ 
     $.get(value, function(response){ 
      var newdate = $(response).find('localtime').text(); 
      if(key == "getNewYorkLocalTime"){ 
       var newyourktime = new Date(newdate); 
       newyourktime.getTime() 
      } 
     }); 
    }); 

但是,在newyourktime只顯示本地時間..任何幫助嗎?以及我從服務器得到的迴應是:2011年11月17日18時09分47秒 - 像這樣。

+0

你能提供從服務器的響應? – antyrat

+0

來自服務器的回覆:2011年11月17日18時09分47秒 – 3gwebtrain

+0

'日期'應該如何神奇地知道你通過紐約當地時間? –

回答

0

這會嘗試使用客戶端機器自己的本地設置解析日期,這是不好的。

不是將它作爲字符串傳遞,而是將它作爲從午夜1/1/70開始傳遞的總秒數,並在構造JavaScript的新Date對象時使用此數字。

例如通過這個號碼:13216.14億,你會得到2011年11月18日,下午1點

1

使用http://www.datejs.com/ 作爲一個例子:

var newyourktime = Date.parse('2011-11-11, 11:11 AM'); 
alert(newyourktime.toString('dd/mm/yyyy HH:mm:ss EST')); 

退房的Datejs庫文件,以滿足您的要求,在你的日期字符串被解析後,你可以做很多事情。

+0

我相信大多數瀏覽器都有內置的Date.parse函數.. – pradeek

0

您可以使用substr

day = newdate.substr(0,2); 
month = newdate.substr(3,3); 
year = newdate.substr(7,4); 
var newyorktime = new Date(year, month, day); 

Substr

相關問題