Si solo convierte la hora actual en una marca de tiempo, puede usar directamente la nueva fecha (). GetTime ()/1000; Pero si convierte una hora o fecha específica en una marca de tiempo UNIX, IE no admite métodos con parámetros como New Date ("2013-1-1"), que devolverá NAN.
En este sentido, escribí las siguientes funciones, que admiten IE6+, Google, Firefox y otros navegadores:
La copia del código es la siguiente:
función gettime (día) {
RE = /(/D{4HITA)
return nueva fecha (re [1], (re [2] || 1) -1, re [3] || 1, re [4] || 0, re [5] || 0, re [6] || 0) .gettime ()/1000;
}
//prueba
alerta (GetTime ("2013-02-03 10:10:10");
alerta (GetTime ("2013-02-03"));
alerta (GetTime ("2013-02"));
alerta (getTime ("2013"));
La siguiente función que convierte las marcas de tiempo en formato de fecha admite formatos de fecha personalizados, y el efecto es similar a la función de fecha de PHP, y también admite navegadores como IE6+, Google, Firefox, etc. Esta función es implementada por los internautas, y lo escribiré si tengo tiempo en el futuro^_^^^^^
La copia del código es la siguiente:
Funcion Fecha (Formato, Timestamp) {
var a, jsdate = ((timestamp)? nueva fecha (marca de tiempo*1000): nueva fecha ());
var pad = function (n, c) {
if ((n = n + "") .length <c) {
devolver una nueva matriz (++ c - n.length) .Join ("0")+n;
} demás {
regresar n;
}
};
var txt_weekdays = ["domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado"];
var txt_ordin = {1: "st", 2: "nd", 3: "rd", 21: "st", 22: "nd", 23: "rd", 31: "st"};
var txt_months = ["", "enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"];
var f = {
// Día
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 - nueva fecha (jsdate.getblyear () + "/1/1"))/864e5 >> 0},
// Semana
W: function () {
var a = fz (), b = 364 + fl () - a;
var nd2, nd = (nueva fecha (jsdate.getblyear () + "/1/1"). getday () || 7) - 1;
if (b <= 2 && ((jsdate.getday () || 7) - 1) <= 2 - b) {
regresar 1;
} demás{
if (a <= 2 && nd> = 4 && a> = (6 - nd)) {
nd2 = nueva fecha (jsdate.getblyaryar () - 1 + "/12/31");
fecha de retorno ("w", math.round (nd2.gettime ()/1000));
} demás{
return (1 + (nd <= 3? ((a + nd) / 7): (a - (7 - nd)) / 7) >> 0);
}
}
},
// Mes
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) {
devolver 28 + fl ();
} demás{
if (n & 1 && n <8 ||! (n & 1) && n> 7) {
regreso 31;
} demás{
regresar 30;
}
}
},
// Año
L: function () {var y = fy (); return (! (Y & 3) && (y % 1e2 ||! (Y % 4e2))? 1: 0},
// O no es compatible todavía
Y: function () {return jsdate.getfulfyear ()},
y: function () {return (jsdate.getblyear () + "") .slice (2)},
// Tiempo
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 (thesegundos/86.4);
if (beat> 1000) beat -= 1000;
if (beat <0) beat += 1000;
if ((string (beat)). longitud == 1) beat = "00"+beat;
if ((string (beat)). longitud == 2) beat = "0"+beat;
Beat de regreso;
},
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)},
// aún no lo compatibles
// zona horaria
// no es compatible todavía
// todavía no apoyé
O: function () {
var t = pad (math.abs (jsdate.gettimezoneOffset ()/60*100), 4);
if (jsdate.gettimezoneOffset ()> 0) t = "-" + t; else t = " +" + t;
regresar t;
},
P: function () {var o = fo (); return (O.Substr (0, 3) + ":" + O.Substr (3, 2))},
// T aún no es compatible
// Z aún no es compatible
// Fecha completa/hora
c: function () {return fy () + "-" + fm () + "-" + fd () + "t" + fh () + ":" + fi () + ":" + fs () + fp ()},,
// r no es compatible todavía
U: function () {return math.round (jsdate.gettime ()/1000)}
};
return format.replace (/[//]? ([A-Za-Z])/G, Función (T, S) {
if (t! = s) {
// escapó
ret = s;
} else if (f [s]) {
// existe una función de fecha
ret = f [s] ();
} demás{
// nada especial
ret = s;
}
regreso de regreso;
});
}
//prueba
alerta (fecha ('ymd h: i: s', (nueva fecha) .gettime ()/1000));
alerta (fecha ('ym-d', (nueva fecha) .gettime ()/1000));
alerta (fecha ('ymd h: i: s', '1355252653'));