The best JavaScript Date (date) objects Tutorial In 2024, In this tutorial you can learn Online examples,Complete Date Object Reference,Creation Date,Set Date,Compare two dates,

JavaScript Date (date) objects

Date object is used to handle dates and times.


Examples

Online examples


How to use the Date () method to get today's date.

getFullYear ()
Use getFullYear () Gets the year.

getTime ()
getTime () returns the number of milliseconds since January 1, 1970 to the present.

setFullYear ()
How setFullYear () to set a specific date.

toUTCString ()
How toUTCString () will be the date (according to UTC) to a string.

getDay ()
How getDay () and array of shows a week, and not just a number.

Display a clock
How to display a clock on the page.


Complete Date Object Reference

We provide JavaScript Date Object Reference Manual, including all properties and methods that can be used to date objects. JavaScript Date Object Reference .

The manual contains a detailed description and relevant examples for each of the properties and methods.


Creation Date

Date object is used to handle dates and times.

Date objects can be defined by the new keyword. The following code defines a Date object called myDate of:

There are four ways to initialize date:

new Date() // 当前日期和时间
new Date(milliseconds) //返回从 1970 年 1 月 1 日至今的毫秒数
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)

Most of the above parameters are optional, in case you do not specify, the default parameter is 0.

Some examples of instantiating a date:

var today = new Date()
var d1 = new Date("October 13, 1975 11:13:00")
var d2 = new Date(79,5,24)
var d3 = new Date(79,5,24,11,33,0)


Set Date

By using the method for Date object, we can easily date operation.

In the following example, we set a specific date (January 14, 2010) is a Date object:

var myDate=new Date();
myDate.setFullYear(2010,0,14);

In the following example, we will object to the date five days after the date of:

var myDate = new Date ();
myDate.setDate (myDate.getDate () + 5);

Note: If you increase the number of days to change the month or year, then the date object will automatically perform this conversion.


Compare two dates

Date objects can also be used to compare two dates.

The following code will be the current date and January 14, 2100 were compared:

var x=new Date();
x.setFullYear(2100,0,14);
var today = new Date();

if (x>today)
{
	alert("今天是2100年1月14日之前");
}
else
{
	alert("今天是2100年1月14日之后");
}
JavaScript Date (date) objects
10/30