Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade For Teachers Spaces Get Certified Upgrade For Teachers Spaces
   ❮     
     ❯   

Basic JavaScript

JS TutorialJS SyntaxJS VariablesJS OperatorsJS If ConditionsJS LoopsJS StringsJS NumbersJS FunctionsJS ObjectsJS DatesJS ArraysJS Typed ArraysJS SetsJS MapsJS MathJS RegExpJS Data TypesJS ErrorsJS EventsJS ProgrammingJS ReferencesJS Versions

JS Advanced

JS FunctionsJS ObjectsJS ClassesJS IterationsJS AsynchronousJS ModulesJS Meta & ProxyJS HTML DOMJS WindowsJS Web APIJS AJAXJS JSONJS jQueryJS GraphicsJS ExamplesJS Reference


JavaScript Set Date Methods


Set Date methods let you set date values (years, months, days, hours, minutes, seconds, milliseconds) for a Date Object.


Set Date Methods

Set Date methods are used for setting a part of a date:

MethodDescription
setDate()Set the day as a number (1-31)
setFullYear()Set the year (yyyy)
setHours()Set the hour (0-23)
setMilliseconds()Set the milliseconds (0-999)
setMinutes()Set the minutes (0-59)
setMonth()Set the month (0-11)
setSeconds()Set the seconds (0-59)
setTime()Set the time (milliseconds since January 1, 1970)

The setFullYear() Method

ThesetFullYear() method sets the year of a date object. In this example to 2020:

Example

const d = new Date("January 01, 2025");
d.setFullYear(2020);
Try it Yourself »

ThesetFullYear() method canoptionally set month and day:

Example

const d = new Date("January 01, 2025");
d.setFullYear(2020, 11, 3);
Try it Yourself »


The setMonth() Method

ThesetMonth() method sets the month of a date object (0-11):

Example

const d = new Date("January 01, 2025");
d.setMonth(11);
Try it Yourself »

The setDate() Method

ThesetDate() method sets the day of a date object (1-31):

Example

const d = new Date("January 01, 2025");
d.setDate(15);
Try it Yourself »

ThesetDate() method can also be used toadd days to a date:

Example

const d = new Date("January 01, 2025");
d.setDate(d.getDate() + 50);
Try it Yourself »

Note

If adding days shifts the month or year, the changes are handled automatically by the Date object.


The setHours() Method

ThesetHours() method sets the hours of a date object (0-23):

Example

const d = new Date("January 01, 2025");
d.setHours(22);
Try it Yourself »

ThesetHours() method can also be used to set minutes andseconds.

Example

const d = new Date("January 01, 2025");
d.setHours(22, 10, 20);
Try it Yourself »

The setMinutes() Method

ThesetMinutes() method sets the minutes of a date object (0-59):

Example

const d = new Date("January 01, 2025");
d.setMinutes(30);
Try it Yourself »

The setSeconds() Method

ThesetSeconds() method sets the seconds of a date object (0-59):

Example

const d = new Date("January 01, 2025");
d.setSeconds(30);
Try it Yourself »

Compare Dates

Dates can easily be compared.

The following example compares today's date with January 14, 2100:

Example

let text = "";
const today = new Date();
const someday = new Date();
someday.setFullYear(2100, 0, 14);

if (someday > today) {
  text = "Today is before January 14, 2100.";
} else {
  text = "Today is after January 14, 2100.";
}
Try it Yourself »

Note

JavaScript counts months from 0 to 11. January is 0. December is 11.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp