JS timestamp how to turn into a date format

Source: Internet
Author: User

Original address: http://www.sufeinet.com/thread-1500-1-1.html

JS timestamp how to turn into a date format
This is a friend in the main group §☆ brazing sonnien insects ... o ask JS timestamp how to turn into a date format, his problem is this
/date (1354116249000)/How does this format turn into a time format
This is from the C # datatime format through the JSON to JS inside,
Here are the methods we provide
JS needs to convert the timestamp to a normal format, which may not be used in general cases,
Let's look at the first kind.

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

The result is
December 23, 2010 10:53
The second Kind

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

What if you want to get a format like this?
2010-10-20 10:00:00
Look at the code below.

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

It can be written 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);                   

Okay, problem solving.
It is important to note that
Do not put the date in the string (such a character is also passed in, you have to deal with it, so it is convenient to handle the
You can use the Replace method
As follows:

Replace ("/date", ""). Replace (")/", "");

JS timestamp how to turn into a date format

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.