Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

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日之后");
}