时间戳和日期字符串之间相互转换

  • Post author:
  • Post category:其他


1:时间戳转日期字符串

// 第一种方式 原生的方式 但是不能修改

//时间戳为10位需*1000,时间戳为13位的话不需乘1000

var timestamp3 = 1403058804;

var newDate = new Date();

newDate.setTime(timestamp3 * 1000);

console.log(newDate.toLocaleDateString()); // 2014年6月18日

console.log(newDate.toLocaleString()); // 2014年6月18日 上午10:33:24

console.log(newDate.toLocaleTimeString()); // 上午10:33:24

// 第二种方式:给日期对象原型添加一个方法

Date.prototype.format = function(format) {

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+



版权声明:本文为zhangqin18710923356原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。