JavaScript compatible with each browser's date conversion

Source: Internet
Author: User
Tags getdate

var date = new Date (Date.parse ("2015-09-05". Replace (/-/g, "/")));
' 2015-09-05 ' is not available in each browser, using new Date (str) to correctly generate the Date object. The correct usage is ' 2015/09/06 '.

-------------------------------------------------

Date usage for javascript:

varMyDate =NewDate (); Mydate.getyear ();//get Current year (2-bit)Mydate.getfullyear ();//Get the full year (4-bit, 1970-????)Mydate.getmonth ();//Get the current month (0-11, 0 for January)Mydate.getdate ();//get current day (1-31)Mydate.getday ();//get Current week x (0-6, 0 for Sunday) why ask Hovertree.comMydate.gettime ();//Gets the current time (the number of milliseconds since 1970.1.1)Mydate.gethours ();//gets the current number of hours (0-23)Mydate.getminutes ();//gets the current number of minutes (0-59)Mydate.getseconds ();//gets the current number of seconds (0-59)Mydate.getmilliseconds ();//gets the current number of milliseconds (0-999)Mydate.tolocaledatestring ();//Get Current datevarMytime=mydate.tolocaletimestring ();//Get current TimeMydate.tolocalestring ();//get date and time


==========================================================================
JS Gets the current timestamp method-javascript gets the current timestamp
JavaScript gets the current timestamp:
The first method:

var timestamp =date.parse (new Date ());

Results: 1280977330000

The second method:
var timestamp = (new Date ()). ValueOf ();
results: 1280977330748
The third method:
var timestamp=New Date (). GetTime ();
results: 1280977330748
the first: The time stamp obtained is the change of milliseconds to 000 display,
The second and third is to get the timestamp of the current millisecond.
my colleague and I in the process of using JS to display the analysis of the time remaining, the time is always changed to 0, the result is very strange, and finally found that the time of acquisition with Date.parse (Newdate ()) obtained by the time stamp to change the millisecond to 000 display, So the time difference calculation is inaccurate.
The time difference can be calculated using the second or third method. http://hovertree.com/menu/javascript/
JS, call the new date () Separately, for example document.write (new Date ());
displaying the result: Mar 10:10:43 utc+0800 2012 time in this format
but participating in the calculation with the new Date () is automatically converted to the number of milliseconds starting from 1970.1.1
--------------------------------------------------------------------------------------------------
Convert a date in string form to a Date object

var //  varnew// converted to data (); var // Get Current Month

----------------------------------------------------

1 //Gets the current timestamp (in s)2 vartimestamp = Date.parse (NewDate ());3timestamp = timestamp/1000;4 //the current timestamp is: 14031495345Console.log ("Current timestamp is:" +timestamp);6 7 //gets the timestamp of a time format8 varStringTime = "2014-07-10 10:21:12";9 varTIMESTAMP2 = Date.parse (NewDate (StringTime));TenTIMESTAMP2 = timestamp2/1000; One //the timestamp for 2014-07-10 10:21:12 is: 1404958872 AConsole.log (StringTime + "timestamp is:" +timestamp2); -  - //change the current time to a time format string the varTimestamp3 = 1403058804; - varNewdate =NewDate (); -Newdate.settime (Timestamp3 * 1000); - //Wed June + Console.log (newdate.todatestring ()); - //Wed, June 02:33:24 GMT + Console.log (newdate.togmtstring ()); A //2014-06-18t02:33:24.000z at Console.log (newdate.toisostring ()); - //2014-06-18t02:33:24.000z - Console.log (Newdate.tojson ()); - //June 18, 2014 - Console.log (newdate.tolocaledatestring ()); - //June 18, 2014 morning 10:33:24 in Console.log (newdate.tolocalestring ()); - //Morning 10:33:24 to Console.log (newdate.tolocaletimestring ()); + //Wed June 10:33:24 gmt+0800 (China Standard Time) - Console.log (newdate.tostring ()); the //10:33:24 gmt+0800 (China Standard Time) * Console.log (newdate.totimestring ()); $ //Wed, June 02:33:24 GMTPanax Notoginseng Console.log (newdate.toutcstring ()); -  theDate.prototype.format =function(format) { +        varDate = { A"m+": This. GetMonth () + 1, the"D+": This. GetDate (), +"H +": This. GetHours (), -"m+": This. getminutes (), $"S+": This. getseconds (), $"q+": Math.floor (( This. GetMonth () + 3)/3), -"S+": This. Getmilliseconds () -        }; the        if(/(y+)/i.test (format)) { -Format = Format.replace (Regexp.$1, ( This. getFullYear () + "). substr (4-regexp.$1. length));Wuyi        } the         for(varKinchdate) { -               if(NewRegExp ("(" + K + ")"). Test (format)) { WuFormat = Format.replace (regexp.$1, regexp.$1.length = = 1 -? Date[k]: ("XX" + date[k]). substr ("" +Date[k]) (length)); About               } $        } -        returnformat; - } -Console.log (Newdate.format (' Yyyy-mm-dd h:m:s '));



JavaScript compatible with each browser's date conversion

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.