Home >Web Front-end >JS Tutorial >Basic introduction to javascript time function_javascript skills
javascript time function
Javascript provides Date objects for time and date calculations.
Date objects have multiple constructors:
new Date() //Current time
new Date(milliseconds) //The number of milliseconds from the starting time on January 1, 1970
new Date(datestring) //The date represented by the string and time. This string can be converted using Date.parse(), such as "Jannuary 1, 1998 20:13:15"
new Date(year, month, day, hours, minutes, seconds, microseconds) //Time value, you can You don’t need to write them all. If you don’t write them, the default is 0
Create a Date object first when using it, for example:
dateObj=new Date();
Then call the function, such as
year=dateObj.getFullYear();//Get the year value
The following is a list of functions.
Get class function:
getDate() function--returns the number of days (1-31)
getDay() function--returns the number of weeks (0-6)
getFullYear () function--returns the four-digit year
getHours() function--returns the number of hours (0-23)
getMilliseconds() function--returns the number of milliseconds (0-999)
getMinutes() Function -- Returns the number of minutes (0-59)
getMonth() Function -- Returns the number of months (0-11)
getSeconds() Function -- Returns the number of seconds (0-59)
getTime () function--returns the timestamp representation (in milliseconds)
getYear() function--returns the year (real year minus 1900)
Set class function:
(The following functions all return the number of milliseconds between the date object and midnight on January 1, 1970)
setDate() function--set the day of the month
setFullYear() function -- set the year, month and day
setHours() function -- set the hours, minutes, seconds and milliseconds
setMilliseconds() function -- set the number of milliseconds
setMinutes( ) Function -- Set minutes, seconds, milliseconds
setMonth() Function -- Set month, day
setSeconds() Function -- Set day of month
setTime() Function -- Set date using milliseconds Object
setYear() function--set the year (real year minus 1900)
Conversion display class function:
toLocalString() function -- returns the localized string representation
toLocaleDateString function -- returns the localized string of the date part
toLocaleTimeString function -- returns the time Partial localized string
Relative to local output, there are also:
toString()
toDateString()
toTimeString()
The difference is that the former has different local language formats according to different machines, while the latter is an internal representation format
Date parsing function
Date.parse( ) function -- parses a date string and returns the number of milliseconds between the date and midnight on January 1, 1970
For time zone related parts, please see javascript time zone function