首页 > 编程知识 正文

js处理时间戳(js 日期格式化)

时间:2023-05-06 13:04:41 阅读:72064 作者:2512

//获取当前时间戳(以s为单位) )

var timestamp=date.parse (new date );

timestamp=timestamp/1000;

//当前时间戳是1403149534

console.log ('当前时间戳为' timestamp );

//获取某个时间格式的时间戳

varstringtime=' 2014-07-1010336021336012 ';

var timestamp2=date.parse (new date (stringtime ) );

timestamp2=timestamp2/1000;

//2014-07-10 10:21:12的时间戳为1404958872

console.log(stringtime )的时间戳为' timestamp2';

//将当前时间更改为时间格式字符串

var timestamp3=1403058804;

var newDate=new Date (;

new date.settime (timestamp3* 1000 );

//Wed Jun 18 2014

控制台. log (new date.to datestring ) );

//Wed,18 Jun 2014 02:33:24 GMT

控制台. log (new date.togmtstring ) );

//2014-06-18t 023360:24.000 z

控制台. log (new date.toisostring ) );

//2014-06-18t 023360:24.000 z

控制台. log (new date.to JSON ) );

//2014年6月18日

控制台. log (new date.tolocaledatestring ();

//2014年6月18日上午10:33:24

控制台. log (new date.tolocalestring ) );

//上午10:33:24

控制台. log (new date.tolocaletimestring ();

//wed jun 18201410:336024 GMT 0800 (中国标准时间) ) ) ) ) ) )。

控制台. log (new date.tostring ) );

//10:33:24 GMT 0800 (中国标准时间() ) )。

控制台. log (new date.totimestring ) );

//Wed,18 Jun 2014 02:33:24 GMT

控制台. log (new date.tout cstring ) );

date.prototype.format=function {

var date={

' M ': this.getMonth () 1,

' d ': this.getDate (),

' h ': this.getHours (),

' m ': this.getMinutes (),

' s ': this.getSeconds (),

' q ' : math.floor ((this.getmonth ) )3)/3 ),

' S ': this.getMilliseconds (

(;

if(/) y )/I.test ) )格式) }

format=format.replace (regexp.$ 1,) this.getFullYear ) ' ).substr(4-regexp.$1.length );

}

for(varkindate ) {

if(newregexp () ) (k ) ) ).test ) ) )。

格式=格式. replace (regexp.$ 1,RegExp.$1.length==1

? date[k]:('00'date[k] ).substr ) ) ' date[k] ).length );

}

}

返回格式;

}

控制台. log (new date.format (yyyy-mm-ddh : m : s );

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。