Home > Web Front-end > JS Tutorial > Multiple ways to format js timestamp into date format_javascript skills

Multiple ways to format js timestamp into date format_javascript skills

WBOY
Release: 2016-05-16 17:16:16
Original
1391 people have browsed it

js needs to convert the timestamp into a normal format, which may not be used under normal circumstances.
Let’s look at the first one first

Copy code The code is as follows:

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

The result is
December 23, 2010 10:53
The second type
Copy code The code is as follows:

function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17)}
alert(getLocalTime(1293072805));

If you want to get it like this What to do with the format?
2010-10-20 10:00:00
Look at the code below
Copy the code The code is as follows:

function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/year|month/g, "-").replace(/ day/g, " ");
}
alert(getLocalTime(1177824835));

can also be written like this
Copy code The code is as follows:

function formatDate(now) {
var year=now.getYear();
var month=now .getMonth() 1;
var date=now.getDate();
var hour=now.getHours();
var minute=now.getMinutes();
var second=now. getSeconds();
return year "-" month "-" date " " hour ":" minute ":" second;
}
var d=new Date(1230999938);
alert( formatDate(d));

The problem is solved
It should be noted that
do not pass the Date (such characters in the string), you must process them first, so
which is very convenient to handle can be processed using the replace method
as follows:
Copy the code The code is as follows:

replace("/Date(","").replace(")/","");
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template