`
zhangyaochun
  • 浏览: 2563413 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

时间转换相关

阅读更多

前言

 

很多时候有一些场景需求下,我们需要对一些时间进行前端的解析和转换,本文介绍3个原生Date对象的方法。

 

 

正文

 

     我们都以"Thu Dec 06 18:06:04 +0800 2012"这个时间作为测试

 

 

  • toLocalString() 根据本地时间格式,把Date对象转换为字符串
console.log(new Date("Thu Dec 06 18:06:04 +0800 2012").toLocaleString());

//chrome返回
"Thu Dec 06 2012 18:06:04 GMT+0800 (中国标准时间)"

//ff返回
"2012年12月6日 18:06:04"

//ie返回
"NaN"
 
 

  • toLocalDateString() 根据本地时间格式,把Date对象的日期部分转换为字符串

new Date("Thu Dec 06 18:06:04 +0800 2012").toLocaleDateString();

//chrome返回
"Thursday, December 06, 2012"

//ff返回
"2012年12月6日"

//ie返回
"NaN"
 

  • toLocalTimesString() 根据本地时间格式,把Date对象的时间部分转换为字符串

 

 

console.log(new Date("Thu Dec 06 18:06:04 +0800 2012").toLocaleTimeString());

//ff & chrome
18:06:04

//ie
"NaN"
 

 

 

分享到:
评论
1 楼 airyland 2012-12-07  
我在Chrome下返回的是:"2012年12月6日 下午6:06:04"

相关推荐

Global site tag (gtag.js) - Google Analytics