Js converts timestamp into a time format string (compatible with various browsers), js time format
At first, the toLocaleString () method was used for convenience, and the problem occurred.
<Script type = "text/javascript"> new Date (parseInt (1421683200) * 1000 ). toLocaleString (); // The result in IE browser is: January 20, 2015 0:00:00 // The result in Google browser is: 12:00:00 // The result in Firefox browser is: 12:00:00 // Safari: Tuesday, January 20,201 00:00:00 </script>
What are the results of different browsers? So how can the results of various browsers be the same? In fact, it is very easy to get the time Date first, and then get the year, month, and day respectively. If necessary, you can also get other required data such as hour, minute, and second, and then according to the time format you need, you can use the data obtained above to splice a time string, so that each browser displays your own defined string, of course, there will be no inconsistency in the display effect.
<Script type = "text/javascript"> var test = new Date (parseInt (1421683200) * 1000); 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 ); // February 1, January 20, 2015 </script>
Address: http://www.iyuze.cn/article/574.html