JS Timestamp converted to date format

Source: Internet
Author: User


Transferred from: http://wyoojune.blog.163.com/blog/static/57093325201131193650725/

This in Php+mssql (date type datetime) +ajax when the ability to use, JS need to convert the timestamp to a normal format, the general situation may not be used

<script>   function Getlocaltime (ns) {      return new Date (parseint (NS) *). toLocaleString (). Replace (/:\ d{1,2}$/, ');   }   Alert (Getlocaltime (1293072805));   

Pop-up: December 23, 2010 10:53

can also be used:

<script>   function Getlocaltime (ns) {       return new Date (parseint (NS) *). toLocaleString (). substr (0,17 )}   Alert (Getlocaltime (1293072805));   </script>   

Suppose you want to pop up: 2010-10-20 10:00:00 This format is good

<script>   function Getlocaltime (ns) {      return new Date (parseint (NS) *). toLocaleString (). Replace (/year | Month/g, "-"). Replace (/day/g, "");    }   Alert (Getlocaltime (1177824835));   </script>


I also refer to some of the other web sites, such as the following:

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 special mention is:

The numbers from MySQL could be strings, and the numbers would have to be *1000, because JS is used in milliseconds!!!!! My example is the following:

var time_num = $ ("date", message). text ();        var time_num = parseint (time_num);     It comes back with a string        var d = new Date (time_num*1000);       This is very important to *1000        var temp_time = formatdate (d);


The first example is a fairly regular one, and everything is in the mood!

JS Timestamp converted to date format

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.