时间日期和时间戳相互转换代码实例
分类:实例代码
关于什么是时间戳可以参阅javascript getTime()一章节。
下面分别介绍一下两者如何实现相互转换。
一.时间戳转换为时间日期:
var date = new Date(1398250549123); Y = date.getFullYear() + '-'; M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'; D = date.getDate() + ' '; h = date.getHours() + ':'; m = date.getMinutes() + ':'; s = date.getSeconds()+ ':'; ss = date.getMilliseconds(); console.log(Y + M + D + h + m + s + ss);
二.时间日期转换为时间戳:
var date = new Date('2014-04-23 18:55:49:123'); var time1 = date.getTime(); var time2 = date.valueOf(); console.log(time1); console.log(time2);
时间日期和时间戳相互转换代码实例,这样的场景在实际项目中还是用的比较多的,关于时间日期和时间戳相互转换代码实例就介绍到这了。
时间日期和时间戳相互转换代码实例属于前端实例代码,有关更多实例代码大家可以查看。