JS converts timestamps into time format strings (compatible with various browsers)

Source: Internet
Author: User

At first, the toLocaleString () method was used for brevity, and the problem arose.

<script type= "Text/javascript" >    new Date (parseint (1421683200) *). toLocaleString ();    The results under IE Browser are: January 20, 2015 0:00:00    //google Browser results as follows: 2015/1/20 morning 12:00:00    //firefox browser results are: 2015/1/20 12:00:00    The result of the//safari browser is: Tuesday,january 00:00:00</script>

The results of the various browsers are not the same as the wood there? So how do you make the results of each browser the same? In fact, it is very simple, is to obtain the time date, and then get the date, if necessary, you can also get the time and seconds and other required data, and then according to their own needs of the format, the use of the data obtained by themselves stitching a time string can be, so that each browser display is your own definition of the string , of course, there will be no display effect of the situation is not uniform.

<script type= "Text/javascript" >    var test = new Date (parseint (1421683200) *);    var $_year = Test.getfullyear ();    var $_month = parseint (Test.getmonth ()) +1;    var $_day = Test.getdate ();    var $_f_date =  $_year + "-" +$_month+ "-" +$_day    alert ($_f_date);//2015-1-20    var $_f_date2 =  $_year + "Year" +$_month+ "Month" +$_day+ "Day";    alert ($_f_date2);//January 20, 2015 </script>
Original address: http://www.iyuze.cn/article/574.html

JS converts timestamps into time format strings (compatible with various browsers)

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.