Detailed description of javascript date operations

Source: Internet
Author: User
Tags time in milliseconds

Detailed description of javascript date operations

It is a built-in object, rather than the attributes of other objects, allowing users to execute various processes that use date and time.

Methods: Get the time method, set the time method, and convert the time method.

Obtain the time method:
GetDate (): view the Date object and return the Date
GetDay () returns the day of the week
GetHours () return hours
GetMinutes () returns the number of minutes
GetMonth () returns the month value.
GetSeconds () returns the number of seconds
GetTime () returns the complete time
GetYear () returns the year

Notes for using the date and time functions in js: When the month is obtained, it will be-1. For example, the current date is July, and the number obtained is 11.

Var date = new Date ();
Date. getYear (); // obtain the year (2 digits)
Date. getFullYear (); // obtain the complete year (4-digit, 1970 -)
Date. getMonth (); // obtain the month (0-11, 0 indicates January, so date. getMonth () + 1 is required when the current time is displayed)
Date. getDate (); // get the day (1-31)
Date. getDay (); // get the week? (0-6, 0 represents Sunday)
Date. getTime (); // obtain the time in milliseconds starting from January 1)
Date. getHours (); // hours (0-23)
Date. getMinutes (); // get the number of minutes (0-59)
Date. getSeconds (); // obtain the number of seconds (0-59)
Date. getMilliseconds (); // obtain the number of milliseconds (0-999)
Date. toLocaleString (); // obtain the date and time
  

Time Setting method:
SetDate () changes the Date of the Date object
SetHours () change the hour
SetMinutes () changes the number of minutes
SetMonth () Change month
SetSeconds () changed in seconds
SetTime () changes the complete time
SetYear () changes the year

Conversion time method:

ToGMTString () converts the Date object's Date (a value) into a GMT time string and returns a value similar to the following: Weds, 15 June l997 14:02:02 GMT (the exact format depends on the operating system running on the computer)
ToLocaleString () converts the Date (a value) of the Date object into a string and uses the specified Date format configured on the computer where it is located.
UTC () uses Date UTC (year, month, day, hour, minute, second), and is optional since January 1, 1970 00:00:00) returns the date in milliseconds.

Notes:

The most important thing is to consider the compatibility of multiple browsers. It is better to obtain the date in the following format:

var timestart = '2015-09-05'; var timeend = '2015-09-06';var time1 = (timestart+' 00:00:00').toString();var time2 = (timeend+' 23:59:59').toString();timestart = new Date(Date.parse(time1.replace(/-/g,"/"))).getTime();timeend = new Date(Date.parse(time2.replace(/-/g,"/"))).getTime();

The Helper house editor provides an advertisement code that is just written and displayed in a certain period of time.

<Script type = "text/javascript"> var timestart = '2017-09-05 '; var timeend = '2017-09-06'; var time1 = (timestart + '16:00:00 '). toString (); var time2 = (timeend + '16:00:00 '). toString (); timestart = new Date (Date. parse (time1.replace (/-/g ,"/"))). getTime (); timeend = new Date (Date. parse (time2.replace (/-/g ,"/"))). getTime (); var nowtime = new Date (). getTime (); if (nowtime> timestart & nowtime <timeend) {document. write ("advertisement") ;}</script>

1. Get the date and year and set the date and year. The strange problem is that you cannot set the month (compared with the strange ONE ):

<script language="javascript"> d = new Date(); alert(d.toLocaleString()); d.setDate(25); alert(d.toLocaleString()); d.setYear(2000); alert(d.toLocaleString()); </script> 

2. It is best to use the getFullYear () method to obtain the year.
3. Because JS starts from 0 for the month, you need to add 1 for the month operation.

The following are several classic and frequently used examples of time. I hope this will improve your performance. Thank you for continuing to follow this post...

1. Convert 2005-8-5 to 2005-08-05 format

<script language="javascript"> var strDate = '2005-8-5'; window.alert(strDate.replace(/\b(\w)\b/g, '0$1')); </script> 

2. Get the number of days

<Script type = "text/javascript"> <! -- Alert ("days separated:" + (new Date ('2014/1/15')-new Date ('2014/2/18')/2005/8/2003/9 + "day ") // --> </script>

3. Get the interval

<Script> var d1 = new Date ("20:08:00"); var d2 = new Date ("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 + "second"); </script>

4. Get the date of today

<Script language = "javascript"> d = new Date (); alert (d. getFullYear () + "year" + (d. getMonth () + 1) + "month" + d. getDate () + "day"); </script>


6. Convert numeric date to Chinese Character

<Html> 

7. Obtain the date of the previous N days or the next N days.


Method 1:

<Script type = "text/javascript"> function showdate (n) {var uom = new Date ()-0 + n * 86400000); uom = uom. getFullYear () + "-" + (uom. getMonth () + 1) + "-" + uom. getDate (); return uom;} window. alert ("today is:" + showdate (0); window. alert ("yesterday:" + showdate (-1); window. alert ("Tomorrow is:" + showdate (1); window. alert ("10 days ago:" + showdate (-10); window. alert ("5 days later:" + 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:" + showdate (-1); window. alert ("Tomorrow is:" + showdate (1); window. alert ("10 days ago:" + showdate (-10); window. alert ("5 days later:" + showdate (5); </script>

Method 3 (Sorry, vsscript is used only for learning and is not recommended for Web pages. After all, IE only ):

<Script language = "vbscript"> function showdate (n) showdate = dateadd ("d", date (), n) end function msgbox "today is:" & showdate (0) msgbox "yesterday:" & showdate (-1) msgbox "Tomorrow:" & showdate (1) msgbox "10 days ago:" & showdate (-10) msgbox "five days later:" & showdate (5) </script>

Method 4:

<Script 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 weeks" + ('day, February 5, 1234 '. charAt (uom. getDay () + "\ n this month" + uom. getDays () + "day"; return uom;} window. alert ("today is:" + showdate (0); window. alert ("yesterday:" + showdate (-1); window. alert ("Tomorrow is:" + showdate (1); window. alert ("10 days ago:" + showdate (-10); window. alert ("5 days later:" + showdate (5); </script>

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.