Home >Web Front-end >JS Tutorial >How to convert javascript timestamp to date

How to convert javascript timestamp to date

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-07-20 14:07:274172browse

The methods for converting javascript timestamps into dates are: 1. Use the toLocaleString() method, the syntax format is "time object.toLocaleString()"; 2. Use the Date attribute method.

How to convert javascript timestamp to date

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

What is Unix timestamp (Unix timestamp): Unix timestamp (Unix timestamp), also known as Unix time (Unix time), POSIX time (POSIX time), is a time representation method, defined as starting from The total number of seconds since 00:00:00 GMT on January 1, 1970 to the present. Unix timestamps are not only used in Unix systems and Unix-like systems, but are also widely used in many other operating systems.

Currently, quite a few operating systems use 32-bit binary numbers to represent time. Unix timestamps for such systems can be used up to January 19, 2038, 03:14:07 GMT (binary: 01111111 11111111 11111111 11111111). One second later, the binary number will change to 10000000 00000000 00000000 00000000, and an overflow error will occur, causing the system to misunderstand the time as 20:45:52 on December 13, 1901. This is likely to cause software failure or even system paralysis. Systems that use 64-bit binary numbers to represent time (which can be used up to 15:30:08 on December 4, 292,277,026,596 Greenwich Time) will basically not encounter this type of overflow problem.

js converts timestamp to ordinary date format

1.Date toLocaleString method

function getLocalTime(nS) {  
 return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');  
}

parseInt() function can parse a string, and returns an integer.

The unit of time operation in js is milliseconds.

The toLocaleString() method can convert the Date object into a string according to the local time and return the result.

Thereplace() method is used to replace some characters with other characters in a string, or replace a substring that matches a regular expression.

replace(/:\d{1,2}$/,' ') Verify that the replacement starts with: a string that ends with one or two digits, which is seconds; replace it with nothing

Displayed as follows:

How to convert javascript timestamp to date

#So we can use regular expressions to change the date format we want.

2.Date attribute method

function add0(m){return m<10?&#39;0&#39;+m:m }
function format(shijianchuo)
{
//shijianchuo是整数,否则要parseInt转换
var time = new Date(shijianchuo);
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
return y+&#39;-&#39;+add0(m)+&#39;-&#39;+add0(d)+&#39; &#39;+add0(h)+&#39;:&#39;+add0(mm)+&#39;:&#39;+add0(s);
}

How to convert javascript timestamp to date

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to convert javascript timestamp to date. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn