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

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

伊谢尔伦
伊谢尔伦Original
2017-07-22 11:41:111561browse

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));

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));

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));

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));

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(")/","");


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!

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