Se você acabou de converter o horário atual em um registro de data e hora, poderá usar diretamente a nova data (). GetTime ()/1000; Mas se você converter uma hora ou data específica em um registro de data e hora do UNIX, o IE não suportar métodos com parâmetros como a nova data ("2013-1-1"), que retornarão a NAN.
Nesse sentido, escrevi as seguintes funções, que suportam IE6+, Google, Firefox e outros navegadores:
A cópia do código é a seguinte:
função gettime (dia) {
re = /(/d {4 })(?
Retornar nova data (re [1], (re [2] || 1) -1, re [3] || 1, re [4] || 0, re [5] || 0, re [6] || 0) .getTime ()/1000;
}
//teste
alerta (gettime ("2013-02-03 10:10:10"));
alerta (gettime ("2013-02-03"));
alerta (gettime ("2013-02"));
alerta (gettime ("2013"));
A seguinte função que converte os registros de data e hora em formato de data suporta formatos de data personalizada, e o efeito é semelhante à função de data do PHP e também suporta navegadores como IE6+, Google, Firefox, etc. Esta função é implementada pelos internautas, e eu o escreverei se tiver tempo no futuro^_^
A cópia do código é a seguinte:
Data da função (formato, registro de data e hora) {
var a, jsdate = ((timestamp)? New Date (Timestamp*1000): new Date ());
var pad = função (n, c) {
if ((n = n + "") .Length <c) {
Retornar nova matriz (++ c - n.Length) .Join ("0")+n;
} outro {
retornar n;
}
};
var txt_weekdays = ["domingo", "segunda -feira", "terça -feira", "quarta -feira", "quinta -feira", "sexta -feira", "sábado"];
var txt_ordin = {1: "st", 2: "nd", 3: "rd", 21: "st", 22: "nd", 23: "rd", 31: "st"};
var txt_months = ["", "janeiro", "fevereiro", "março", "abril", "maio", "junho", "julho", "julho", "agosto", "setembro", "outubro", "novembro", "dezembro"];
var f = {
// Dia
d: function () {return pad (fj (), 2)},
D: function () {return fl (). Substr (0,3)},
j: function () {return jsdate.getdate ()},
L: function () {return txt_weekdays [fw ()]},
N: function () {return fw () + 1},
S: function () {return txt_ordin [fj ()]? txt_ordin [fj ()]: 'th'},
W: function () {return jsdate.getday ()},
z: function () {return (jsdate - nova data (jsdate.getlyear () + "/1/1"))/864e5 >> 0},
// Semana
W: function () {
var a = fz (), b = 364 + fl () - a;
var nd2, nd = (nova data (jsdate.getlyear () + "/1/1"). getDay () || 7) - 1;
if (b <= 2 && ((jsdate.getday () || 7) - 1) <= 2 - b) {
retornar 1;
} outro{
if (a <= 2 && nd> = 4 && a> = (6 - nd)) {
nd2 = nova data (jsdate.getlyear () - 1 + "/12/31");
Data de retorno ("W", Math.Round (nd2.gettime ()/1000));
} outro{
retornar (1 + (nd <= 3? ((A + nd) / 7): (a - (7 - nd)) / 7) >> 0);
}
}
},
// Mês
F: function () {return txt_months [fn ()]},
m: function () {return pad (fn (), 2)},
M: function () {return ff (). Substr (0,3)},
n: function () {return jsdate.getmonth () + 1},
t: function () {
var n;
if ((n = jsdate.getmonth () + 1) == 2) {
retornar 28 + fl ();
} outro{
if (n & 1 && n <8 ||! (n & 1) && n> 7) {
retornar 31;
} outro{
retornar 30;
}
}
},
// Ano
L: function () {var y = fy (); retorna (! (Y & 3) && (y % 1e2 ||! (Y % 4e2)))? 1: 0},
// não ainda não suportado
Y: function () {return jsdate.getlyear ()},
y: function () {return (jsdate.getlyear () + "") .slice (2)},
// Tempo
A: function () {return jsdate.gethours ()> 11? "PM": "Am"},
A: function () {return fa (). Touppercase ()},
B: function () {
// Peter Paul Koch:
var off = (jsdate.getTimeZoneOffSet () + 60)*60;
var theSeconds = (jsdate.gethours () * 3600) + (jsdate.getminutes () * 60) + jsdate.getSeconds () + off;
var beat = Math.Floor (TheSegunds/86.4);
if (batida> 1000) batida -= 1000;
if (bate <0) batida += 1000;
if (((string (beat)). comprimento == 1) beat = "00"+beat;
if (((string (beat)). comprimento == 2) beat = "0"+beat;
batida de retorno;
},
G: function () {return jsdate.gethours () % 12 || 12},
G: function () {return jsdate.gethours ()},
h: function () {return pad (fg (), 2)},
H: function () {return pad (jsdate.gethours (), 2)},
i: function () {return pad (jsdate.getminutes (), 2)},
s: function () {return pad (jsdate.getSeconds (), 2)},
// você ainda não foi suportado
// fuso horário
// e ainda não suportado
// eu ainda não apoiei
O: function () {
var t = pad (math.abs (jsdate.getTimeZoneoffSet ()/60*100), 4);
if (jsdate.getTimeZoneOffSet ()> 0) t = "-" + t; else t = " +" + t;
retornar t;
},
P: function () {var o = fo (); retornar (O.substr (0, 3) + ":" + o.substr (3, 2))},
// ainda não foi suportado
// z ainda não suportado
// data completa/hora
c: function () {return fy () + "-" + fm () + "-" + fd () + "t" + fh () + ":" + fi () + ":" + fs () + fp ()},
// ainda não foi suportado
U: function () {return math.round (jsdate.gettime ()/1000)}
};
retornar format.replace (///]? ([a-za-z])/g, função (t, s) {
if (t! = s) {
// escapou
ret = s;
} else if (f [s]) {
// existe uma função de data
ret = f [s] ();
} outro{
// Nada de especial
ret = s;
}
retornar retorno;
});
}
//teste
alerta (data ('ymd h: i: s', (nova data) .gettime ()/1000));
alerta (data ('ym-d', (nova data) .gettime ()/1000));
alerta (data ('ymd h: i: s', '1355252653'));