Detailed description of the JavaScript Date object, javascriptdate

Source: Internet
Author: User

Detailed description of the JavaScript Date object, javascriptdate

This article describes how to operate Date and Time objects.

Directory
1. Introduction: Describes the Date object.

2. constructor: describes several methods of the constructor of the Date object, new Date.

3. instance method: Describes the get, set, and other instance methods of the Date object.

4. Static Method: describes the static method of the Date object: Date. now (), Date. parse (), and so on.

5. Actual Operation: some examples of Date objects are introduced: Getting countdown, comparing the size of two Date objects, and so on.

I. Introduction
1.1 description

Date object, which is the object that operates on the Date and time. The Date object can only operate on the Date and time through methods.

1.2 attributes

None; the Date object can only operate on the Date and time through methods.

Ii. Constructor
2.1 new Date (): returns the current local Date and time.

Parameter: 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 the Date object.

Parameters:

① Milliseconds {int}: Number of milliseconds, indicating the number of milliseconds starting from '2014/1/01 00:00:00.

Note: the start time, minute, and second must be added to the current time zone. The Beijing Time Zone is East 8, and the start time is actually '2017/01 08:00:00'

Return Value:

{Date} returns a superimposed Date object.

Example:

Var dt = new Date (1000*60*1); // The number of milliseconds before 1 minute. log (dt); // =>{ Date}: 1970/01/01 08: 01: 00dt = new Date (-1000*60*1 ); // returns the number of milliseconds in one minute. log (dt); // >{date }:1970/01/01 07:59:00

2.3 new Date (dateStr): converts a string to a Date object.

Parameters:

① DateStr {string}: string that can be converted to a Date object (time can be omitted). There are two main types of string formats:

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

2) yyyy-MM-dd HH: mm: ss: if the time is omitted, the returned Date object time is 08:00:00 (plus the local time zone ). If no time is omitted, this string will fail to be converted in IE!

Return Value:

{Date} returns a converted Date object.

Example:

Var dt = new Date ('2014/1/25'); // yyyy/MM/ddconsole. log (dt); // =>{ Date}: 2014/12/25 00: 00: 00dt = new Date ('2017/25 12:00:00 '); // yyyy/MM/dd HH: mm: ssconsole. log (dt); // =>{ Date}: 2014 12:00:00 dt = new Date ('2017-12-25 '); // yyyy-MM-ddconsole.log (dt ); // => {Date}: 2014 08:00:00 (with the time zone of the East 8 area added) dt = new Date ('2017-12-25 12:00:00 '); // yyyy-MM-dd HH: mm: ss (Note: This conversion method will report an error in IE !) Console. log (dt); // >{date }:2014-12-25 12:00:00

2.4 new Date (year, month, opt_day, opt_hours, opt_minutes, opt_seconds, opt_milliseconds): converts year, month, day, And hour to Date objects.

Parameters:

① Year {int}: year; 4 digits. For example, 1999 and 2014

② Month {int}: month; 2 digits. It starts from 0, and 0 indicates February and 11 indicates February.

③ Opt_day {int} (optional): No.; 2 digits; starts from 1, and 1 indicates no. 1.

④ Opt_hours {int} Optional: hour; 2 digits; value range: 0 ~ 23.

⑤ Opt_minutes {int} (optional): minute, two-digit number; value range: 0 ~ 59.

⑥ Opt_seconds {int} (optional) seconds; 2 unnumbered; value range: 0 ~ 59.

7 opt_milliseconds {int} Optional: millisecond; value range: 0 ~ 999.

Return Value:

{Date} returns a converted Date object.

Example:

Var dt = new Date (2014, 11); // December 2014 (the number of months entered here is 11) console. log (dt); // =>{ Date}: 2014/12/01 00: 00: 00dt = new Date (2014, 11, 25); // console on April 9, December 25, 2014. log (dt); // =>{ Date}: 2014/12/25 00: 00: 00dt = new Date (2014, 11, 25, 15, 30, 40 ); // console at, January 1, December 25, 2014. log (dt); // >{date}: 2014/12/25 15: 30: 40dt = new Date (2014, 12, 25 ); // Oct 13-25, 13th (the number of months entered here is 12, indicating that the second month will jump to the OCT 12 of the second year) console. log (dt); // =>{ Date}: 2015/01/25

Iii. instance method
The Date object instance method can be divided into two forms: local time and UTC time. The same method usually has these two time format operations (the method name is UTC, that is, the Operation UTC time). Here we mainly introduce the operations on the local time.

3.1 get Method

3.1.1 getFullYear (): return the Year Value of the Date object; 4-digit year.

3.1.2 getMonth (): returns the month value of the Date object. Starting from 0, so the real month = return value + 1.

3.1.3 getDate (): returns the Date value in the month of the Date object. The value range is 1 ~ 31.

3.1.4 getHours (): returns the hour value of the Date object.

3.1.5 getMinutes (): returns the minute value of the Date object.

3.1.6 getSeconds (): returns the second value of the Date object.

3.1.7 getMilliseconds (): returns the millisecond value of the Date object.

3.1.8 getDay (): returns the week value of the Date object in a week. 0 indicates Sunday, 1 indicates Monday, 2 indicates Tuesday, and so on.

3.1.9 getTime (): returns the millisecond value between the Date object and '2014/1/01 00:00:00 '(Beijing Time Zone is East 8, and the actual start time is: '2014/1/01 08:00:00 ').

Example:

Dt. getFullYear (); // => 2014: Year dt. getMonth (); // => 11: month; actual month (month starts from 0) dt. getDate (); // => 25: dt. getHours (); // => 15: dt. getMinutes (); // => 30: dt. getSeconds (); // => 40: seconds dt. getMilliseconds (); // => 333: millisecond dt. getDay (); // => 4: the value of the day of the week dt. getTime (); // => 1419492640333: returns the millisecond value between the Date object and '2014/1/01 00:00:00 '(the time zone of Beijing time is East 8, and the actual start time is: '2014/1/01 08:00:00 ')

3.2 set Method

3.2.1 setFullYear (year, opt_month, opt_date): Set the year Value of the Date object; 4-digit year.

3.2.2 setMonth (month, opt_date): Set the month value of the Date object. 0 indicates January 1, January, and 11 indicates January 1, December.

3.2.3 setDate (date): set the Date value in the month of the date object. The value range is 1 ~ 31.

3.2.4 setHours (hour, opt_min, opt_sec, opt_msec): Set the hour value of the Date object.

3.2.5 setMinutes (min, opt_sec, opt_msec): sets the minute value of the Date object.

3.2.6 setSeconds (sec, opt_msec): sets the second value of the Date object.

3.2.7 setMilliseconds (msec): sets the millisecond value of the Date object.

Example:

Var dt = new Date (); dt. setFullYear (2014); // => 2014: Year dt. setMonth (11); // => 11: month; actual month (month starts from 0) dt. setDate (25); // => 25: dt. setHours (15); // => 15: dt. setMinutes (30); // => 30: dt. setSeconds (40); // => 40: seconds dt. setMilliseconds (333); // => 333: console in milliseconds. log (dt); // => December 25, 2014 minutes, 40 seconds, 333 milliseconds

3.3 Other Methods

3.3.1 toString (): Convert Date to a 'year, month, day, hour, minute, second 'string

3.3.2 toLocaleString (): converts a Date to a local format string of 'Year, month, day, hour, minute, second '.

3.3.3 toDateString (): Convert Date to a 'year, month, and Day' string

3.3.4 toLocaleDateString (): converts a Date to a local string in the 'Year, month, and Day' format.

3.3.5 toTimeString (): converts a Date to a 'hour, minute, and second 'string.

3.3.6 toLocaleTimeString (): converts a Date to a local string in 'hour, minute, second 'format.

3.3.7 valueOf (): Same as getTime (), return the millisecond value between the Date object and '2014/1/01 00:00:00 '(the time zone of Beijing time is East 8, and the actual start time is: '2014/1/01 08:00:00 ')

Example:

Var dt = new Date (); console. log (dt. toString (); // => Tue Dec 23 2014 22:56:11 GMT + 0800 (China Standard Time): converts a Date to a 'year, month, day, hour, minute second' string console. log (dt. toLocaleString (); // => December 23, 2014 10:56:11: Convert Date to a local Format String console of 'Year, month, day, hour, minute, second. log (dt. toDateString (); // => Tue Dec 23 2014: Convert Date to a 'year, month, and Day' string console. log (dt. toLocaleDateString (); // => August 1, December 23, 2014: Convert Date to a local string in the 'Year, month, and Day' format. log (dt. toTimeString (); // => 22:56:11 GMT + 0800 (China Standard Time): converts a Date to a 'hour, minute, second 'string console. log (dt. toLocaleTimeString (); // => 10:56:11 pm: Convert Date to a local string in 'hour, minute, second. log (dt. valueOf (); // => returns the millisecond value between the Date object and '2014/1/01 00:00:00 '(the time zone of Beijing time is East 8, and the actual start time is: '2014/1/01 08:00:00 ')

Iv. Static Methods
4.1 Date. now ()

Note: return the millisecond value between the current Date object and '2014/1/01 00:00:00 '(Beijing Time Zone is East 8, and the actual start time is: '2014/1/01 08:00:00 ')

Parameter: None

Return Value:

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

Example:

console.log(Date.now()); // => 1419431519276

4.2 Date. parse (dateStr)

Description: converts a string to a Date object, and then returns the millisecond value between the Date object and '2014/1/01 00:00:00 '(the time zone of Beijing time is East 8, and the actual start time is: '2014/1/01 08:00:00 ')

Parameters:

① DateStr {string}: string that can be converted to a Date object (time can be omitted). There are two main types of string formats:

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

2) yyyy-MM-dd HH: mm: ss: if the time is omitted, the returned Date object time is 08:00:00 (plus the local time zone ). If no time is omitted, 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/1/25 12:00:00 '); // => 14194820.000console. log (Date. parse ('2017-12-25 12:00:00 '); // => 2014 (Note: This conversion method returns NaN! in IE !)

5. Actual operations
5.1 convert the DateTime type of C # To the Date object of Js

Description: The DateTime type of C # is returned to the foreground through Json serialization in the format of "\/Date (1419492640000 )\/". The number in the middle, indicating the number of milliseconds between the DateTime value and the start time.

Example:

Background code: simple ashx

Public void ProcessRequest (HttpContext context) {System. web. script. serialization. javaScriptSerializer js = new System. web. script. serialization. optional criptserializer (); DateTime dt = DateTime. parse ("15:30:40"); string rs = js. serialize (dt); // Serialize to Json context. response. contentType = "text/plain"; context. response. write (rs );}

Front-end code:

Var dateTimeJsonStr = '\/Date (1419492640000) \/'; // C # Json format of DateTime type conversion var msecStr = dateTimeJsonStr. toString (). replace (// Date \ ([-]? \ D +) \ // gi, "$1"); // => '000000': Obtain the millisecond string var msesInt = Number through regular expression replacement. parseInt (msecStr); // converts a millisecond string to a value var dt = new Date (msesInt); // initializes the console of the Date object. log (dt. toLocaleString (); // => December 25, 2014 3:30:40

5.2 get countdown

Note: calculates the time difference between the current time and the target time.

Example:

/*** Return countdown * @ param dt {Date}: Destination Date object * @ return {Strin}: return countdown: X days X hours X minutes */function getDownTime (dt) {// 1. get the countdown var intervalMsec = dt-Date. now (); // the target time minus the current time to obtain the millisecond difference between the two. var intervalSec = intervalMsec/1000; // convert to the number of seconds var day = parseInt (intervalSec/3600/24); // Number of days var hour = parseInt (intervalSec-day * 24*3600)/3600 ); // hour var min = parseInt (intervalSec-day * 24*3600-hour * 3600) /60); // minute // 2. if the difference in milliseconds is less than 0, it indicates that the target time is less than the current time. At this time, all values are negative:-X days-hour-minute, only the negative number of days is displayed. If (intervalMsec <0) {hour = 0-hour; min = 0-min;} // 3. concatenate the string and return var rs = day + 'day' + hour + 'hour + min + 'Min'; return rs;} // current time: 13: 26console. log (getDownTime (new Date ('2014/1/01'); // => 2015/06 day console. log (getDownTime (new Date ('2014/1/01'); // =>-2014/01 days

5.3 compare the size of two Date objects

Note: We can compare the milliseconds between the two 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

The above is all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • Introduction to toLocaleDateString () in JavaScript
  • How to Use the toDateString () method in JavaScript
  • Js table sorting instance analysis (supports four data types: int, float, date, and string)
  • Sample Code for converting string to date in Javascript
  • Details about Array object extension and String object extension in JS
  • The toString () method of the JavaScript Number object
  • A summary of javascript learning every day (String object)
  • Three js methods to implement the string substring Method
  • Analysis of substring and substr methods in js
  • Explain the functions of Unescape () and String () in JavaScript
  • A Brief Introduction to the javascript Date type
  • Js imitates strtotime () and date () functions in php.
  • JS Simple Method for converting String to Date

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.