Home  >  Article  >  Web Front-end  >  javascript time display code collection (Date object)_time and date

javascript time display code collection (Date object)_time and date

WBOY
WBOYOriginal
2016-05-16 17:50:051041browse
javascript time function
javascript provides Date objects for time and date calculations.

Date objects have multiple constructors:

new Date() //Current time
new Date(milliseconds) //1970 years from the starting time The number of milliseconds on January 1
new Date(datestring) //The date and time represented by the string. 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, the default is 0 if you don’t write them.


When using it, first create a Date object, such as:
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)


Setting 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 the minutes , seconds, milliseconds
setMonth() function--Set the month and day
setSeconds() function--Set the day of the month
setTime() function--Set the date object using milliseconds
setYear( ) Function -- Set the year (real year minus 1900)


Conversion display class function:
toLocalString() Function -- Return the localized string representation
toLocaleDateString function--returns the localized string of the date part
toLocaleTimeString function--returns the localized string of the time part

relative to the local output, there are also:

toString( )
toDateString()
toTimeString()

The difference is that the former has different local language formats according to different machines, and the latter is an internal representation format


Date parsing function
Date.parse() function--parse a date string and return the number of milliseconds between the date and midnight on January 1, 1970

For time zone related parts, please see javascript time zone function

javascript display time example

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

JavaScript display system Time with date

JavaScript displays system time, displays year, month, day, hour, minute, second and day of the week, automatically refreshes every second, and adds date color difference:
Monday to Friday is displayed in black
Saturday is displayed as Green
Sunday is shown in red

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn