JavaScript Date Object Introduction

Source: Internet
Author: User
Tags instance method local time time and seconds

Date and Time object 1. Introduced

A Date object that is the object that operates on the date and time. The Date object operates on dates and times only through methods.

2. Constructor 2.1 new Date (): Returns the current local date and time

Parameters: None

return value:

{Date} returns a Date object that represents the local date and time.

Example:

var dt = new Date (); console.log (DT); = = Returns a Date object that represents the local date and time

2.2 New Date (milliseconds): Converts the number of milliseconds to a Date object

Parameters:

①milliseconds {int}: Number of milliseconds, indicating the number of milliseconds to begin stacking from ' 1970/01/01 00:00:00 '.

Note: The starting point of time to add the time zone of the current, Beijing times the timezone is the East 8 district, the starting time is actually: ' 1970/01/01 08:00:00 '

return value:

{Date} returns an overlay of the Date object.

Example:

var dt = new Date (1000 * 60 * 1); The number of milliseconds to forward 1 minutes console.log (DT); = = {Date}:1970/01/01 08:01:00DT = new Date (-1000 * 60 * 1); The number of milliseconds to reverse 1 minutes console.log (DT); + = {date}:1970/01/01 07:59:00

2.3 New Date (DATESTR): Converts a string to a Date object

Parameters:

①datestr {string}: A string that can be converted to a Date object (time can be omitted), and there are two main formats for strings:

1) yyyy/mm/dd HH:mm:ss (recommended): If time is omitted, the time of the returned Date object is 00:00:00.

2) Yyyy-mm-dd HH:mm:ss: If time is omitted, the time of the returned Date object is 08:00:00 (plus the local time zone). If you do not omit the time, this string will fail to convert in IE!

return value:

{Date} returns a converted Date object.

Example:

2.4 New date (year, month, Opt_day, Opt_hours, Opt_minutes, Opt_seconds, opt_milliseconds): Converts the month-date, time-division-second to date object

Parameters:

①year {int}: year; 4 digits. such as: 1999, 2014

②month {int}: month; 2 digits. Starting from 0, 0 means January, 11 means December.

③opt_day {int} optional: number, 2 digits, starting from 1, 1 for number 1th.

④opt_hours {int} optional: time, 2 digits, value 0~23.

⑤opt_minutes {int} optional: minute, 2 digits, value 0~59.

⑥opt_seconds {int} optional: seconds; 2 no number; value 0~59.

⑦opt_milliseconds {int} optional: milliseconds; value 0~999.

return value:

{Date} returns a converted Date object.

Example:

var dt = new Date (2014, 11); December 2014 (The month number entered here is one) console.log (DT); = = {Date}:2014/12/01 00:00:00DT = new Date (2014, 11, 25); December 25, 2014 Console.log (DT); = = {Date}:2014/12/25 00:00:00DT = new Date (2014, 11, 25, 15, 30, 40); December 25, 2014 15:30 40 sec console.log (DT); = = {Date}:2014/12/25 15:30:40DT = new Date (2014, 12, 25); March 25, 2014 (the month number entered here is 12, indicating the 13th month, jump to the second year of January) console.log (DT); + = {DATE}:2015/01/25

3. Properties

None; Date objects operate on dates and times only through methods.

4. Example Methods

The instance method of a Date object is divided into 2 main forms: local time and UTC time. The same method typically has this 2 time format operation (the method name is UTC, which is the operation of UTC time), which mainly describes the operation of local time.

4.1 Get Method 4.1.1 getFullYear (): Returns the year value of the Date object; 4-bit year. 4.1.2 GetMonth (): Returns the month value of the Date object. Starting from 0, so the real month = return value +1. 4.1.3 GetDate (): Returns the date value in the month of the date object; The range of values 1~31. 4.1.4 getHours (): Returns the hour value of the Date object. 4.1.5 getminutes (): Returns the minute value of the Date object. 4.1.6 getseconds (): Returns the second value of the Date object. 4.1.7 getmilliseconds (): Returns the millisecond value of the Date object. 4.1.8 GetDay (): Returns the day of the week of the date object, 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on 4.1.9 getTime (): Returns the millisecond value between the Date object and ' 1970/01/01 00:00:00 ' ( The time zone in Beijing is East 8, and the starting time is actually: ' 1970/01/01 08:00:00 '.

Example:

4.2 Set Method 4.2.1 setFullYear (year, Opt_month, opt_date): Sets the years value of the Date object; 4-bit year. 4.2.2 Setmonth (Month, opt_date): Sets the month value of the Date object. 0 represents January, 11 represents December. 4.2.3 SetDate (date): Sets the date value in the month of the date object; The range of values 1~31. 4.2.4 sethours (Hour, opt_min, Opt_sec, opt_msec): Sets the value of the Date object. 4.2.5 setminutes (min, Opt_sec, opt_msec): Sets the minute value of the Date object. 4.2.6 setseconds (sec, opt_msec): Sets the number of seconds for the date object. 4.2.7 setmilliseconds (msec): Sets the millisecond value of the Date object. Example:
var dt = new Date ();d t.setfullyear (2014); = 2014: Year dt.setmonth (11); = 11: Month; actual December (month starting from 0) dt.setdate (25); + 25: Day dt.sethours (15); = 15: Time Dt.setminutes (30); = 30: Sub-dt.setseconds (40); = 40: Seconds dt.setmilliseconds (333); = 333: Ms Console.log (DT); =  December 25, 2014 15:30 40 sec 333 ms

4.3 Other methods 4.3.1 toString (): Converts date to a ' month Day time Minute ' string 4.3.2 tolocalestring (): Converts a date to a ' month day time seconds ' local format string 4.3.3  toDateString (): Converts date to a ' month day ' string 4.3.4 tolocaledatestring (): a local format string that converts date to a ' month and day ' 4.3.5  toTimeString (): Converts date to a ' time seconds/minute ' string 4.3.6 tolocaletimestring (): Converts date to a ' time seconds ' local format string 4.3.7 valueof () : Same as gettime (),  returns the millisecond value between the Date object and ' 1970/01/01 00:00:00 ' (The time zone in Beijing is East 8, and the start time is actually: ' 1970/01/01 08:00:00 ')   Example:
var dt = new Date (); Console.log (dt.tostring ()); = Tue Dec 22:56:11 gmt+0800 (China Standard Time): Converts a date to a ' month-and-day time and Seconds ' string Console.log (Dt.tolocalestring ()); = December 23, 2014 10:56:11  

5. Static method 5.1 Date.now ()

Description: returns the millisecond value between the Date object and ' 1970/01/01 00:00:00 ' in the current day and time (the time zone in Beijing time is East 8, and the starting point is actually: ' 1970/01/01 08:00:00 ')

Parameters: None

return value:

{int}: The number of milliseconds between the current time and the start time.

Example:

5.2 Date.parse (DATESTR)

Description: converts a string to a Date object and then returns the millisecond value between this Date object and ' 1970/01/01 00:00:00 ' (The time zone in Beijing time is East 8, and the starting point is actually: ' 1970/01/01 08:00:00 ')

Parameters:

①datestr {string}: A string that can be converted to a Date object (time can be omitted), and there are two main formats for strings:

1) yyyy/mm/dd HH:mm:ss (recommended): If time is omitted, the time of the returned Date object is 00:00:00.

2) Yyyy-mm-dd HH:mm:ss: If time is omitted, the time of the returned Date object is 08:00:00 (plus the local time zone). If you do not omit the time, this string returns Nan (not a number) in IE!

return value:

{int} returns the number of milliseconds between the converted Date object and the start time.

Example:

Console.log (Date.parse (' 2014/12/25 12:00:00 ')); = 1419480000000 Console.log (date.parse (' 2014-12-25 12:00:00 ')); = = 1419480000000  (Note: This conversion is returned in IE nan! )

6. Actual operation 6.1 The DateTime type of C # to a Date object of JS

Description: The datetime type of C # is returned to the foreground through JSON serialization in the format: "\/date (1419492640000) \". The median number that represents the number of milliseconds between the datetime value and the start time.

Example:

Backstage code: Simple ASHX

public void ProcessRequest (HttpContext context) {    System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer ();    DateTime dt = DateTime.Parse ("2014-12-25 15:30:40");    String rs = js. Serialize (DT); Serialized into JSON    context. Response.ContentType = "Text/plain";    Context. Response.Write (RS);}

Front Code:

6.2 Getting the Countdown

Description: calculates how many days the current time differs from the destination time.

Example:

/*** return countdown * @param dt {Date}: Destination Date Object * @return {Strin}: Return countdown: X days x x */function getdowntime (DT) {    //1. Get Countdown    var i Ntervalmsec = Dt-date.now (); Destination time minus the current time, get the difference between the two milliseconds    var intervalsec = intervalmsec/1000;//convert to seconds    var day = parseint (intervalsec/3600/ 24); Days    var hour = parseint ((intervalsec-day * 24 * 3600)/3600);//h    var min = parseint ((Intervalsec-day * 2 4 * 3600-hour * 3600)/60); Minutes    //2. If the difference between milliseconds is less than 0, indicating that the destination time is less than the current time, then the value is negative:-X day-time-minute, display, only show the number of days before negative on the line.    if (Intervalmsec < 0) {        hour = 0-hour;        min = 0-min;    }    3. Splice the string and return    var rs = day + ' days ' + hour + ' when ' + min + ' points ';    return RS;} Current time: 2014/12/28 13:26console.log (Getdowntime (New Date (' 2015/06/01 ')); = 154 days 10:33 console.log (getdowntime (' 2014/01/01 ')); +-361 days 13:26

6.3 Comparing the size of 2 date objects

Note: You can compare the number of milliseconds between 2 and the start time to differentiate the size.

Example:

var dt1 = new Date (' 2015/12/01 '), var dt2 = new Date (' 2015/12/25 '); Console.log (Dt1 > DT2); = False

================================== Series article ==========================================

This article: 3.4 JavaScript Date Object Introduction

Web Development Road Series articles

JavaScript Date Object Introduction

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.