JS Timestamp converted to date format

Source: Internet
Author: User

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

[PHP]View Plaincopy
    1. <script>
    2. function Getlocaltime (NS) {
    3. return new Date (parseint (NS) *). toLocaleString (). replace (/:\d{1,2}$/,");
    4. }
    5. Alert (Getlocaltime (1293072805));
    6. </script>

Pop-up: December 23, 2010 10:53

You can also use:

[PHP]View Plaincopy
    1. <script>
    2. function Getlocaltime (NS) {
    3. return new Date (parseint (NS) *). toLocaleString (). SUBSTR (0,17)}
    4. Alert (Getlocaltime (1293072805));
    5. </script>

If you want to pop up: 2010-10-20 10:00:00 This format is done

[PHP]View Plaincopy
    1. <script>
    2. function Getlocaltime (NS) {
    3. return new Date (parseint (NS) *). toLocaleString (). Replace (/year | month/g, "-"). Replace (/day/g, "");
    4. }
    5. Alert (Getlocaltime (1177824835));
    6. </script>


In addition, I also took a look at some of the other sites of the East, as follows:

[PHP]View Plaincopy
  1. function FormatDate (now) {
  2. var year=now.getyear ();
  3. var month=now.getmonth () +1;
  4. var date=now.      GetDate ();
  5. var hour=now.gethours ();
  6. var minute=now.getminutes ();
  7. var second=now.getseconds ();
  8. return year+"-" +month+"-" +date+"" +hour+":" +minute+":" +second;
  9. }
  10. var d=new Date (1230999938);
  11. 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!!!!! I have the following:

[PHP]View Plaincopy
    1. var time_num = $ ("date", message). text ();
    2. var time_num = parseint (time_num); //Pass back a string
    3. var d = new Date (time_num*1000); //This is important to *1000
    4. var temp_time = formatdate (d);


The first example is more regular, and what is the case?

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.