現在の時間をタイムスタンプに変換するだけの場合、new date()。getTime()/1000を直接使用できます。ただし、特定の時間または日付をUNIXタイムスタンプに変換する場合、IEはNANを返す新しい日付( "2013-1-1")などのパラメーターを使用してメソッドをサポートしていません。
この点で、IE6+、Google、Firefox、その他のブラウザをサポートする次の機能を書きました。
コードコピーは次のとおりです。
関数gettime(day){
re = /(/d {4})(?:-(/d {1,2})(/d {1,2})))ージャン)))))(?:/s+(/d {1,2}):(/d {1,2}):(/d {1,2})(
新しい日付(re [1]、(re [2] || 1)-1、re [3] || 1、re [4] || 0、re [5] || 0、re [6] || 0).gettime()/1000;
}
//テスト
Alert(getTime( "2013-02-03 10:10:10"));
アラート(gettime( "2013-02-03"));
アラート(getTime( "2013-02"));
アラート(getTime( "2013"));
タイムスタンプを日付形式に変換する次の関数は、カスタム日付形式をサポートし、効果はPHPの日付関数に似ており、IE6+、Google、Firefoxなどのブラウザをサポートします。この関数はNetizensによって実装されています。
コードコピーは次のとおりです。
関数日(フォーマット、タイムスタンプ){
var a、jsdate =((timestamp)?new Date(Timestamp*1000):new date());
var pad = function(n、c){
if((n = n + "").length <c){
新しい配列(++ c -n.length).join( "0")+n;
} それ以外 {
n;
}
};
var txt_weekdays = ["Sunday"、 "Monday"、 "火曜日"、「水曜日」、「木曜日」、「金曜日」、「土曜日」];
var txt_ordin = {1: "st"、2: "nd"、3: "rd"、21: "st"、22: "nd"、23: "rd"、31: "st"};
var txt_months = ["" ""、 "1月"、 "2月"、 "3月"、 "4月"、 "5月"、 "6月"、 "7月"、 "8月"、 "9月"、 "10月"、 "12月"]];
var f = {
// 日
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- new date(jsdate.getTullyear() + "/1/1")/864e5 >> 0}、
// 週
W:function(){
var a = fz()、b = 364 + fl()-a;
var nd2、nd =(new date(jsdate.getTullyear() + "/1/1")。getDay()|| 7)-1;
if(b <= 2 &&((jsdate.getday()|| 7)-1)<= 2 -b){
返品1;
} それ以外{
if(a <= 2 && nd> = 4 && a> =(6 -nd)){
nd2 = new date(jsdate.getTullyear()-1 + "/12/31");
return date( "w"、math.round(nd2.gettime()/1000));
} それ以外{
return(1 +(nd <= 3?((a + nd) / 7):( a-(7 -nd)) / 7)>> 0);
}
}
}、
// 月
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){
28 + fl()を返します。
} それ以外{
if(n&1 && n <8 ||!(n&1)&& n> 7){
返品31;
} それ以外{
30を返します。
}
}
}、
// 年
l:function(){var y = fy(); return(!(y&3)&&(y%1e2 ||!(y%4e2))? 1:0}、
// oまだサポートされていません
y:function(){return jsdate.getFullyear()}、
y:function(){return(jsdate.getTullyear() + "").slice(2)}、
// 時間
A:function(){return jsdate.gethours()> 11? 「PM」:「Am」}、
a:function(){return fa()。touppercase()}、
B:function(){
//ピーター・ポール・コッホ:
var off =(jsdate.getTimeZoneOffset() + 60)*60;
var theseconds =(jsdate.gethours() * 3600) +(jsdate.getminutes() * 60) + jsdate.getSeconds() + off;
var beat = math.floor(theconds/86.4);
if(beat> 1000)beat- = 1000;
if(beat <0)beat += 1000;
if((string(beat))。length == 1)beat = "00"+beat;
if((string(beat))。length == 2)beat = "0"+beat;
ビートを返します。
}、
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)}、
// Uはまだサポートされていません
// TimeZone
// eはまだサポートされていません
//まだサポートされていません
O:function(){
var t = pad(math.abs(jsdate.getTimeZoneOffset()/60*100)、4);
if(jsdate.getTimeZoneOffset()> 0)t = " - " + t; else t = " +" + t;
tを返します。
}、
p:function(){var o = fo(); return(o.substr(0、3) + ":" + o.substr(3、2))}、
// tまだサポートされていません
// zまだサポートされていません
//完全な日付/時刻
c:function(){return fy() + " - " + fm() + " - " + fd() + "t" + fh() + ":" + fi() + ":" + fs() + fp()}、
// rまだサポートされていません
u:function(){return math.round(jsdate.gettime()/1000)}
};
return format.replace(/[//]?([a-za-z])/g、function(t、s){
if(t!= s){
//逃げました
ret = s;
} else if(f [s]){
//日付関数が存在します
ret = f [s]();
} それ以外{
//特別なものはありません
ret = s;
}
返品;
});
}
//テスト
alert(date( 'ymd h:i:s'、(new Date).getTime()/1000));
Alert(date( 'ym-d'、(new Date).getTime()/1000));
Alert(date( 'ymd h:i:s'、 '1355252653'));