Home  >  Article  >  Web Front-end  >  JavaScript time format conversion and Date object

JavaScript time format conversion and Date object

高洛峰
高洛峰Original
2016-11-26 13:41:181438browse

1. Current system locale format (toLocaleDateString and toLocaleTimeString)

Example: (new Date()).toLocaleDateString() + " " + (new Date()).toLocaleTimeString()
Result: January 29, 2008 16:13:11

2. Ordinary strings (toDateString and toTimeString)

Example: (new Date()).toDateString() + " " + (new Date()).toTimeString()
Result: Tue Jan 29 2008 16:13:11 UTC+0800

3. Greenwich Mean Time (toGMTString)

Example: (new Date()).toGMTString()
Result: Tue, 29 Jan 2008 08:13:11 UTC

4.Universal Standard Time (toUTCString)

Example: (new Date()).toUTCString()
Result: Tue, 29 Jan 2008 08:13:11 UTC

5.Date object string (toString)

Example: (new Date()).toString()
Result: Tue Jan 29 16:13:11 UTC+0800 2008
Date object constructor
Date object has multiple constructors.

new Date()
new Date(milliseconds)
new Date(datestring)
new Date(year, month)
new Date(year, month, day)
new Date(year, month, day, hours)
new Date(year, month, day, hours, minutes)
new Date(year, month, day, hours, minutes, seconds)
new Date(year, month, day, hours, minutes, seconds, microseconds)
Date object construction Function parameter description

milliseconds - the number of milliseconds from the start time defined internally in JavaScript on January 1, 1970
datestring - the date and time represented by a string. This string can be converted using Date.parse()
year - a four-digit year, if the value is 0-99, add 1900 to it
month - 0 (representing January) - 11 (representing ten month between February)
day - date between 1-31
hours - number of hours between 0 (representing midnight) - 23
minutes - number of minutes between 0-59
seconds - number between 0-59 Number of seconds between
microseconds - Number of milliseconds between 0-999
Date object return value

Without any arguments, the current date will be returned
If the argument is a number, treat the number as a millisecond value and convert to a date
If The parameter is a string, treat the string as a string representation of the date, converted to a date
It can also be precisely defined using six constructors, and returns the time
Example
var d1 = new Date();
document.write( d1.toString());
var d2 = new Date("2009-08-08 12:12:12);
document.write(d2.toString());
var d3 = new Date(2009, 8, 8);
document.write(d3.toString());
Date is a built-in object of JavaScript and must be created using new.
Date object is expressed internally in JavaScript as the date since January 1, 1970. The number of milliseconds (timestamp) at midnight (GMT time). Here we also call the internal representation of Date a timestamp. You can use getTime() to convert the Date object into a Date timestamp. The method setTime() can convert it. The timestamp of Date is converted to the standard form of Date. The
Date function uses the syntax
date.methodname(parameter1,parameter2,...);
Date.methodname();
date represents an instance of a date object, Date represents a date object, and the date. method name calls a member function of the object

Date. The method name calls a static function of the object

Example
var d=new Date();
var d2=Date.UTC();
JavaScript_Date function is classified by function
Date acquisition class function
Date() function--Constructor of Date object
getDate() function- - Returns the number of days in the month in the date object (1-31)
getDay() function--Returns the number of days in the week in the date object(0-6)
getFullYear() function--Returns four digits in the date object Count the year
getHours() function--returns the number of hours (0-23) in the date object
getMilliseconds() function--returns the number of milliseconds (0-999) in the date object
getMinutes() function--returns the date object The number of minutes in the date object (0-59)
getMonth() function--returns the number of months in the date object(0-11)
getSeconds() function--returns the number of seconds in the date object(0-59)
getTime( ) Function -- Returns the timestamp representation of the date object (in milliseconds)
getTimezoneOffset() Function -- Returns the time difference between local time and the current date represented in UTC, in minutes
getUTCDate() Function -- Returns the date object The day of the month (1-31) expressed in Universal Coordinated Time (UTC)
getUTCDay() function-- Returns the day of the week (0-6) expressed in Universal Coordinated Time (UTC) in the date object
getUTCFullYear( ) Function--Returns the four-digit year in the date object expressed in Universal Coordinated Time (UTC)
getUTCHours() Function--Returns the number of hours (0-23) expressed in Universal Coordinated Time (UTC) in the date object
getUTCMilliseconds( ) Function -- Returns the number of milliseconds (0-999) expressed in Universal Coordinated Time (UTC) in the date object
getUTCMinutes() Function -- Returns the number of minutes (0-59) expressed in Universal Coordinated Time (UTC) in the date object )
getUTCMonth() function--returns the number of months (0-11) expressed in Universal Coordinated Time (UTC) in the date object
getUTCSeconds() function--returns the number of seconds expressed in Universal Coordinated Time (UTC) in the date object (0-59)
getYear() function--returns the year of the date object (real year minus 1900)
Date.UTC() function--returns the date object from midnight on January 1, 1970 Universal Coordinated Time (UTC) The number of milliseconds (timestamp) between
Date setting class function
setDate() function--sets the day of the month in the date object, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setFullYear() function -- sets the year, month and day in the date object, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setHours() function -- sets the date object Hours, minutes, seconds and milliseconds, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setMilliseconds() function--Sets the number of milliseconds in the date object and returns the number of milliseconds in the date object since 1970 The number of milliseconds (timestamp) between midnight on January 1st
setMinutes() function--Sets the minutes, seconds, and milliseconds of the date object, and returns the number of milliseconds (time Stamp)
setMonth() function--Set the month and day in the date object, and return the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setSeconds() function--Set the month in the date object day, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setTime() function -- sets the date object using the number of milliseconds, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970 The number of milliseconds (timestamp) between
setUTCDate() function -- sets the day of the month in the date object expressed in Universal Coordinated Time (UTC), and returns the number of milliseconds between the date object and midnight on January 1, 1970 (Timestamp)
setUTCFulYear() function -- Sets the year, month and day in the date object expressed in Universal Coordinated Time (UTC), and returns the number of milliseconds between the date object and midnight on January 1, 1970 (timestamp )
setUTCHours() function --- Sets the hours, minutes, seconds and milliseconds in the date object expressed in Universal Coordinated Time (UTC), and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970 )
setUTCMilliseconds() function--Set the number of milliseconds in the date object expressed in Universal Coordinated Time (UTC), and return the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setUTCMinutes() function --Set the minutes and seconds expressed in Coordinated Universal Time (UTC) in the date object, and return the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setUTCMonth() function -- Set the date object The month and day expressed in Universal Coordinated Time (UTC), and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setUTCSeconds() function -- Sets the date object in Universal Standard Time (UTC) represents seconds, milliseconds, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setYear() function--set the year of the date object (real year minus 1900)
Date printing function
toDateString() function -- returns the string representation of the date part of the date object
toGMTString() function -- returns the string representation of the Greenwich Mean Time (GMT) of the date object
toLocaleDateString function -- returns the date The localized string of the date part of the object
toLocaleTimeString function--returns the localized string of the time part of the date object
toTimeString() function--returns the string of the time part of the date object
toUTCString function--returns the localized string of the date object String representation of Universal Coordinated Time (UTC)
Date parsing function
Date.parse() function--parse a date string and return the number of milliseconds (timestamp) between the date and midnight on January 1, 1970
JavaScript_Date function is classified according to letters
Date() function--Date Object's constructor
getDate() function -- returns the number of days in the month (1-31) in the date object
getDay() function -- returns the number of days in the week (0-6) in the date object
getFullYear() Function -- Returns the four-digit year in the date object
getHours() function -- Returns the number of hours (0-23) in the date object
getMilliseconds() Function -- Returns the number of milliseconds (0-999) in the date object
getMinutes() function -- returns the number of minutes (0-59) in the date object
getMonth() function -- returns the number of months (0-11) in the date object
getSeconds() function -- returns the number of minutes in the date object Number of seconds (0-59)
getTime() function--returns the timestamp representation of the date object (in milliseconds)
getTimezoneOffset() function--returns the time difference between local time and the current date expressed in UTC, in minutes
getUTCDate() function--returns the day of the month (1-31) in the date object expressed in Universal Coordinated Time (UTC)
getUTCDay() function--returns the mid-week expressed in UTC in the date object day (0-6)
getUTCFulYear() function--returns the four-digit year in the date object expressed in Universal Coordinated Time (UTC)
getUTCHours() function--returns the four-digit year expressed in UTC in the date object Hours (0-23)
getUTCMilliseconds() function--returns the number of milliseconds (0-999) expressed in UTC in the date object
getUTCMinutes() function--returns the number of milliseconds in the date object expressed in UTC The number of minutes (0-59) expressed in UTC)
getUTCMonth() function--returns the number of months (0-11) expressed in UTC in the date object
getUTCSeconds() function--returns the number of months in the date object expressed in UTC The number of seconds (0-59) expressed in Universal Coordinated Time (UTC)
getYear() function--returns the year of the date object (real year minus 1900)
Date.parse() function--parses a date string, And returns the number of milliseconds (timestamp) between the date and midnight on January 1, 1970
setDate() function--sets the day of the month in the date object, and returns the number of milliseconds between the date object and midnight on January 1, 1970 The number of milliseconds (timestamp)
setFullYear() function -- sets the year, month and day in the date object, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setHours() Function -- Set the hour, minute, second and millisecond of the date object, and return the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setMilliseconds() Function -- Set the number of milliseconds of the date object, And return the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setMinutes() function--set the minutes, seconds, milliseconds of the date object, and return the date object between midnight on January 1, 1970 The number of milliseconds (timestamp) between
setMonth() function - Set the month and day in the date object, and return the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setSeconds() function -- Set the day of the month in the date object, and return the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setTime() function -- Set the date object using the number of milliseconds, and return the distance between the date object and The number of milliseconds (timestamp) between midnight on January 1, 1970
setUTCDate() function -- sets the day of the month in the date object expressed in Universal Coordinated Time (UTC), and returns the date object since January 1, 1970 The number of milliseconds between midnight and midnight (timestamp)
setUTCFulYear() function--Set the year, month and day in the date object expressed in Universal Coordinated Time (UTC), and return the date object since midnight on January 1, 1970 The number of milliseconds (timestamp) between
setUTCHours() function --- Sets the hours, minutes, seconds and milliseconds in the date object expressed in Universal Coordinated Time (UTC), and returns the date object since midnight on January 1, 1970 The number of milliseconds (timestamp) between
setUTCMilliseconds() function--Sets the number of milliseconds in the date object expressed in Universal Coordinated Time (UTC), and returns the number of milliseconds (time) between the date object and midnight on January 1, 1970 Stamp)
setUTCMinutes() function--Set the minutes and seconds expressed in Universal Coordinated Time (UTC) in the date object, and return the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setUTCMonth( ) Function -- Sets the month and day expressed in Universal Coordinated Time (UTC) in the date object, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setUTCSeconds() Function -- Setting Seconds and milliseconds expressed in Coordinated Universal Time (UTC) in the date object, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setYear() function--Set the year of the date object ( The real year minus 1900)
toDateString() function -- Returns the string representation of the date part of the date object
toGMTString() function -- Returns the string representation of the Greenwich Mean Time (GMT) of the date object
toLocaleDateString function -- Returns the localized string of the date part of the date object
toLocaleTimeString function -- returns the localized string of the time part of the date object
toTimeString() function -- returns the string of the time part of the date object
toUTCString function -- returns the date The string representation of the object's Universal Coordinated Time (UTC)
Date.UTC() function -- returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970 Universal Coordinated Time (UTC)


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