JavaScript timestamp and its formatting

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.