JS time operation

Source: Internet
Author: User

var mydate = new Date ();

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) (add 1)

Mydate.getdate (); Get current day (1-31)

Mydate.getday (); Get Current week x (0-6, 0 for Sunday)

Mydate.gettime (); Gets the current time (the number of milliseconds since 1970.1.1)

Mydate.gethours (); Get 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 date

var mytime=mydate.tolocaletimestring (); Get current time

Mydate.tolocalestring (); Get Date and time

How to set the time:
Setyear ()       //Change year
How to convert time:

toGMTString () Turns the Date object's Day (a numeric value) into a GMT time string and returns a value similar to the following: Weds,14 May 10:02:02 GMT (exact format depends on the operating system running on the computer) toLocaleString () Turns the Date object's dates (a numeric value) into a string, using the specific date format that is configured on the computer on which UTC () uses date UTC (Year, month, day, time, minute, second), since January 1, 1997 00:00:00 ( The number of milliseconds since the time, minutes, and seconds are optional to return the date

1. Current system locale format (tolocaledatestring and tolocaletimestring)

Example:(new Date ()). toLocaleDateString () + "" + (new Date ()). toLocaleTimeString ()
Results: January 29, 2008 16:13:11

2. Ordinary strings (todatestring and toTimeString)

Example: (new Date ()). toDateString () + "" + (new Date ()). toTimeString ()
Results: Tue Jan 16:13:11 utc+0800

3. Greenwich Mean Time (togmtstring)

Example: (New Date ()). toGMTString ()
Results: Tue, Jan 08:13:11 UTC

4. Global Standard Time (toutcstring)

Example: (New Date ()). toUTCString ()
Results: Tue, Jan 08:13:11 UTC

5.Date Object String (toString)

Example: (New Date ()). ToString ()
Results: Tue Jan 16:13:11 utc+0800 2008

A few points to note:

1, get the date and year and set the date and year time, the very strange problem is that the month can not be set (strange comparison):

<script type= "Text/javascript" language= "javascript" >//d = new Date (); alert (d.tolocalestring ());

D.setdate (14);

Alert (d.tolocalestring ());

D.setyear (2007);

Alert (d.tolocalestring ());

</script>

2, when the year is best to use the getFullYear () method to do 3, due to the month, JS is starting from 0, so need to enter the month
Add 1 for line operation The following are some of the classic and often used examples of time, for everyone will be helpful: 1, will 2005-8
-5 converted to 2005-08-05 format

<script type= "Text/javascript" language= "JavaScript" >

//

var strdate = ' 2005-8-5 ';

Window.alert (Strdate.replace (/\b (\w) \b/g, ' 0$1 '));

</script>

2, get the interval days <script type= "Text/javascript" >//&amp;amp;lt;! --Alert ("Days Between Days:" + (new Date (' 2005/8/15 ')-new date (' 2003/9/18 '))/1000/60/60/24+ "Day")//</script>

3. Get interval time <script type= "Text/javascript" >//var d1=new Date ("2004/09/16 20:08:00");  var d2=new Date ("2004/09/16 10:18:03");  var d3=d1-d2;  var h=math.floor (d3/3600000);  var M=math.floor ((d3-h*3600000)/60000);  var s= (d3-h*3600000-m*60000)/1000;  Alert ("Difference" +h+ "hour" +m+ "minute" +s+ "seconds"); </script>

4. Get today's date <script type= "Text/javascript" language= "javascript" >//d = new Date ();  Alert (d.getfullyear () + "year" + (D.getmonth () +1) + "month" +d.getdate () + "Day"); </script>

6. Get the date of the first n days or the last n days Method 1: <script type= "Text/javascript" >//function showdate (n) {var uom = new Date (new date () -0+n*864  00000);  UoM = uom.getfullyear () + "-" + (Uom.getmonth () +1) + "-" + uom.getdate ();  return UOM; }

Window.alert ("Today is:" +showdate (0));  Window.alert ("Yesterday is:" +showdate (-1));  Window.alert ("Tomorrow is:" +showdate (1));  Window.alert ("10 days ago is:" +showdate (-10));  Window.alert ("5 days later is:" +showdate (5)); </script>

Method 2: <script type= "Text/javascript" >//function showdate (n) {var uom = new Date ();  Uom.setdate (Uom.getdate () +n);  UoM = uom.getfullyear () + "-" + (Uom.getmonth () +1) + "-" + uom.getdate ();  return UOM; }

Window.alert ("Today is:" +showdate (0));  Window.alert ("Yesterday is:" +showdate (-1));  Window.alert ("Tomorrow is:" +showdate (1));  Window.alert ("10 days ago is:" +showdate (-10));  Window.alert ("5 days later is:" +showdate (5)); </script>

method 3: <script type= "Text/javascript" Language= "Javascript" >//date.prototype.getdays=function () { var _newdate=new Date ();  _ Newdate.setmonth (_newdate.getmonth () +1);  _newdate.setdate (0);  $_days=_newdate.getdate ();  delete _newdate;  return $_days;  }  function showdate (n)  { var uom = new Date ();  uom.setdate (Uom.getdate () +n);  uom = UoM . getFullYear () + "-" + (Uom.getmonth () +1) + "-" + uom.getdate () + "\ n Week" + (' Day twos
3456 '. CharAt (Uom.getday ())) + "\ n this month" + Uom.getdays () + "Day";  return UoM;  }   window.alert ("Today is:" +showdate (0));  window.alert ("Yesterday is:" +showdate (-1));  window.alert ("Tomorrow is:" +showdate (1));  window.alert ("10 days ago is:" +showdate (-10));  window.alert ("5 days later is:" +showdate (5));  //</script>

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.