In PHP + MySQL (date type datetime) + AJAX applications, it is sometimes necessary to format the timestamp type as a generic time-type format using JavaScript. Here are some of the ways to convert and compare some common summaries. Xiping County Quine Friends Electric
Define the timestamp with its date format dates first
var day1 = parseint (new date (). ValueOf ()/1000), var day2 = new Date (day1 * 1000);
The following is an encapsulation method that obtains a date from a timestamp, similar to the Day2 method:
function Getlocaltime (NS) { return new Date (parseint (NS) *). toLocaleString (). Replace (/:\d{1,2}$/, ');
Replace it for a moment:
function Getlocalformattime (NS) { return new Date (parseint (NS) *). toLocaleString (). Replace (/year | month/g, "-"). Replace (/day/g, ""); }
A rather stupid way to get a formatted date:
document.getElementById ("Btn5"). onclick = function () {alert (day2.getfullyear () + "-" + (Day2.getmonth () +1) + "-" + Day2.getdate () + "+day2.gethours () +": "+day2.getminutes () +": "+day2.getseconds ());
Here is the complete program:
<script type= "Text/javascript" >var day1 = parseint (new date (). ValueOf ()/1000); var day2 = new Date (Day1 *); func tion Getlocaltime (ns) {return new Date (parseint (NS) *). toLocaleString (). Replace (/:\d{1,2}$/, '); }/* With the above function */function Getlocaltimes (NS) {return new Date (parseint (NS) *). toLocaleString (). substr (0,17);} function Getlocalformattime (NS) {return new Date (parseint (NS) *). toLocaleString (). Replace (/year | month/g, "-"). Replace (/ Day/g, ""); } document.getElementById ("Btn1"). onclick = function () {alert (day1);} document.getElementById ("Btn2"). onclick = function () {alert (day2.tolocalestring ());} document.getElementById ("Btn3"). onclick = function () {alert (Getlocaltime (Day1));} document.getElementById ("Btn4"). onclick = function () {alert (Getlocalformattime (Day1));} document.getElementById ("Btn5"). onclick = function () {alert (day2.getfullyear () + "-" + (Day2.getmonth () +1) + "-" + Day2.getdate () + "+day2.gethours () +": "+day2.getminutes () +": "+day2.getseconds ()); </scriPt>
JavaScript timestamps and their formatting