且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在javascript中将时间戳转换为人类日期的功能

更新时间:2023-12-02 12:15:10

值1382086394000 可能是一个时间值,这是自1970-01-01T00:00:00Z以来的毫秒数。您可以使用它使用 Date构造函数创建ECMAScript Date对象:

The value 1382086394000 is probably a time value, which is the number of milliseconds since 1970-01-01T00:00:00Z. You can use it to create an ECMAScript Date object using the Date constructor:

var d = new Date(1382086394000);

您如何将其转换为可读取的内容取决于您。简单地将其发送到输出应该调用通常以人类可读形式打印等效的系统时间的内部(和完全依赖于实现的) toString 方法,例如

How you convert that into something readable is up to you. Simply sending it to output should call the internal (and entirely implementation dependent) toString method that usually prints the equivalent system time in a human readable form, e.g.

Fri Oct 18 2013 18:53:14 GMT+1000 (EST) 

在ES5中还有一些其他内置的格式化选项:

In ES5 there are some other built-in formatting options:

  • toDateString
  • toTimeString
  • toLocaleString

等等。请注意,大多数是实现依赖的,并且在不同的浏览器中将是不同的。如果您想要在所有浏览器中使用相同的格式,您需要自己格式化日期,例如:

and so on. Note that most are implementation dependent and will be different in different browsers. If you want the same format across all browsers, you'll need to format the date yourself, e.g.:

alert(d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear());