Home > Web Front-end > JS Tutorial > Detailed explanation of usage examples of converting javascript timestamp into date format

Detailed explanation of usage examples of converting javascript timestamp into date format

伊谢尔伦
Release: 2017-07-22 11:41:11
Original
1626 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

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

The result is
December 23, 2010 10:53
Second

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

What if you want to get it in this format?
2010-10-20 10:00:00
Look at the code below

function getLocalTime(nS) { 
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " "); 
} 
alert(getLocalTime(1177824835));
Copy after login

You can also write it like this

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));
Copy after login

Okay, the problem is solved
It should be noted that
Don't pass in the Date (characters like this) in the string. You need to process them first, so that they can be processed easily.
You can use the replace method
as follows:

replace("/Date(","").replace(")/","");
Copy after login


The above is the detailed content of Detailed explanation of usage examples of converting javascript timestamp into date format. For more information, please follow other related articles on the PHP Chinese website!

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