2012-03-28 11 views
5

Sto cercando di creare un nuovo oggetto data dalla stringa come segue:Javascript nuovo oggetto Date da String, risultati differenti su IE e FF

var myDate= new Date("1985-01-01T00:00:00.000-06:00"); 

su Firefox, avvisa il seguente

mar 1 gennaio 1985 00:00:00 GMT-0600 (centrale Ora solare)

su IE8, avvisa il seguente

NaN

Perché IE si comporta in questo modo?

+0

possibile duplicato del [date JavaScript in IE, NAN - firefox e chrome ok] (http://stackoverflow.com/questions/2182246/javascript-dates-in-ie-nan-firefox-chrome-ok) –

+1

Perché IE8 non supporta questo formato. Semplice come quella. – BalusC

+0

Ciao balusC, Qual è il modo più semplice di creare una data con String for IE? – user1195192

risposta

5

Guardando al documetation il formato giusto è il seguente:

new Date(year, month, day [, hour, minute, second, millisecond ]) 

Quindi, se si esegue il seguente codice sarà bene in tutti i browser:

var myDate= new Date(1985, 01, 01 , 00, 06, 00, 0000000000); 
myDate // you get the right date in all browsers IE8/7 included 
+0

questo non otterrà la data giusta, cos ie8 conteggio mese da '0' non '1', quindi 'nuova data (1985, 01, 01, 00, 06, 00, 0000000000)' non feb giu, è necessario diminuire 1 quando hai passato il parametro del mese –

2

Prova moment.js per tutti i tuoi problemi con la data di JS.

+0

Puoi dare un esempio di codice di come momento.js può essere utilizzato per rispondere alla domanda? – JustinStolle

+0

@JustinStolle var myDate = moment ('1985-01-01T00: 00: 00.000-06: 00'); – dontGoPlastic

0

Il formato non è supportato da IE. Forse si potrebbe provare a utilizzare setUTCHours:

var rawdate = new Date("1985/01/01 00:00:00 GMT"); 
console.log(rawdate); 
    //=> in my timezone: Tue Jan 1 01:00:00 UTC+0100 1985 
console.log(rawdate.setUTCHours(-6)); 
    //=> in my timezone that results in: Mon Dec 31 19:00:00 UTC+0100 1984 

O forse vuoi dire (opere in IE, non in altri browser)?

var rawdate = new Date("1985/01/01 00:00:00 GMT-6"); 
    //=> Tue Jan 1 07:00:00 UTC+0100 1985 
Problemi correlati