Date date and time object
1. Introduction
Date object is an object that operates on date and time. The Date object can only operate on date and time through methods.
2. Constructor
2.1 new Date(): Return the current local date and time
Parameters: None
Return value:
{Date} returns a Date object representing the local date and time.
Example:
2.2 new Date(milliseconds): Convert milliseconds to Date object
Parameters:
①milliseconds {int}: Number of milliseconds; indicating the number of milliseconds starting from '1970/01/01 00:00:00' as the starting point.
Note: The current time zone must be added to the starting point. The time zone of Beijing time is East 8th District. The actual starting time is: '1970/01/01 08:00:00'
Return value:
{Date} returns a superimposed Date object.
Example:
2.3 new Date(dateStr): Convert string to Date object
Parameters:
①dateStr {string}: A string that can be converted into a Date object (time can be omitted); there are two main formats of strings:
1) yyyy/MM/dd HH:mm:ss (recommended): If the time is omitted, the time of the returned Date object is 00:00:00.
2) yyyy-MM-dd HH:mm:ss: If the time is omitted, the time of the returned Date object is 08:00:00 (plus the local time zone). If the time is not omitted, this string will fail to be converted 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): Convert year, month, day, hours, minutes and seconds into Date objects
Parameters:
①year {int}: year; 4 digits. Such as: 1999, 2014
②month {int}: month; 2 digits. Calculation starts from 0, 0 represents January and 11 represents December.
③opt_day {int} Optional: number; 2 digits; counting from 1, 1 means No. 1.
④opt_hours {int} Optional: hours; 2 digits; value 0~23.
⑤opt_minutes {int} Optional: minutes; 2 digits; value 0~59.
⑥opt_seconds {int} Optional: seconds; 2 unnumbered; value 0~59.
⑦opt_milliseconds {int} Optional: milliseconds; value 0~999.
Return value:
{Date} returns a converted Date object.
Example:
3. Attributes
None; Date objects can only operate on date and time through methods.
4. Instance methods
The instance methods of Date objects are mainly divided into two forms: local time and UTC time. The same method generally operates on these two time formats (the method name with UTC is the operation of UTC time). Here we mainly introduce the operation of local time.
4.1 get method
4.1.1 getFullYear(): Returns the year value of the Date object; 4-digit year.
4.1.2 getMonth(): Returns the month value of the Date object. Starts from 0, so real month = return value 1 .
4.1.3 getDate(): Returns the date value in the month of the Date object; the value range is 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 seconds 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 value of the Date object; 0 is Sunday, 1 is Monday, 2 is 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 of Beijing time is East 8th District, 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): Set the year value of the Date object; 4-digit year.
4.2.2 setMonth(month, opt_date): Set the month value of the Date object. 0 represents January and 11 represents December.
4.2.3 setDate(date): Set the date value in the month of the Date object; the value range is 1~31.
4.2.4 setHours(hour, opt_min, opt_sec, opt_msec): Set the hour value of the Date object.
4.2.5 setMinutes(min, opt_sec, opt_msec): Set the minute value of the Date object.
4.2.6 setSeconds(sec, opt_msec): Set the seconds value of the Date object.
4.2.7 setMilliseconds(msec): Set the millisecond value of the Date object.
Example:
4.3 Other methods
4.3.1 toString(): Convert Date into a 'year, month, day, hour, minute, and second' string
4.3.2 toLocaleString(): Convert Date into a local format string of 'year, month, day, hour, minute and second'
4.3.3 toDateString(): Convert Date into a 'year, month, day' string
4.3.4 toLocaleDateString(): Convert Date into a local format string of 'year, month and day'
4.3.5 toTimeString(): Convert Date into a string of 'hours, minutes and seconds'
4.3.6 toLocaleTimeString(): Convert Date into a local format string of 'hours, minutes and seconds'
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 of Beijing time is East 8th District, the starting time is actually is: '1970/01/01 08:00:00')
Example:
5. Static method
5.1 Date.now()
Description: Returns the millisecond value between the Date object of the current date and time and '1970/01/01 00:00:00' (the time zone of Beijing time is East 8th District, the starting time is actually: '1970/01/01 08 :00:00')
Parameters: None
Return value:
{int}: The number of milliseconds between the current time and the starting time.
Example:
5.2 Date.parse(dateStr)
Description: Convert the string to a Date object, and then return the millisecond value between this Date object and '1970/01/01 00:00:00' (the time zone of Beijing time is East 8th District, the starting time is actually: '1970 /01/01 08:00:00')
Parameters:
①dateStr {string}: A string that can be converted into a Date object (time can be omitted); there are two main formats of strings:
1) yyyy/MM/dd HH:mm:ss (recommended): If the time is omitted, the time of the returned Date object is 00:00:00.
2) yyyy-MM-dd HH:mm:ss: If the time is omitted, the time of the returned Date object is 08:00:00 (plus the local time zone). If the time is not 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 starting time.
Example:
6. Practical operation
6.1 Convert C#’s DateTime type to Js’s Date object
Note: The format of C#'s DateTime type returned to the front desk through Json serialization is: "/Date(1419492640000)/". The number in the middle represents the number of milliseconds between the DateTime value and the starting time.
Example:
Backend code: simple ashx
Front-end code:
6.2 Get Countdown
Description: Calculate how many days, hours and minutes the current time is from the destination time.
Example:
6.3 Compare the sizes of two Date objects
Note: You can compare the two with the number of milliseconds of the starting time to distinguish the size.
Example: