Date Input
ISO Date | "2015-03-25" (The International Standard) |
Short Date | "03/25/2015" |
Long Date | "Mar 25 2015" or "25 Mar 2015" |
Date Get Methods
getFullYear() | Get year as a four digit number (yyyy) |
getMonth() | Get month as a number (0-11) |
getDate() | Get day as a number (1-31) |
getDay() | Get weekday as a number (0-6) |
getHours() | Get hour (0-23) |
getMinutes() | Get minute (0-59) |
getSeconds() | Get second (0-59) |
getMilliseconds() | Get millisecond (0-999) |
getTime() | Get time (milliseconds since January 1, 1970) |
getFullYear() Method
returns the year of a date as a four digit number:
<p id="demo"></p>
<script>
const d = new Date("2021-03-25")
document.getElementById("demo").innerHTML = d.getFullYear();
</script>
Return the full year of a date object:
2021
Old JavaScript code might use the non-standard method getYear().
getYear() is supposed to return a 2-digit year.
getYear() is deprecated. Do not use it!
getMonth() Method
returns the month of a date as a number (0-11).
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getMonth() + 1;
</script>
3
<p id="demo"></p>
<script>
const months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
const d = new Date("2021-03-25");
let month = months[d.getMonth()];
document.getElementById("demo").innerHTML = month;
</script>
March
getDate() Method
returns the day of a date as a number (1-31)
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getDate();
</script>
25
getHours() Method
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getHours();
</script>
10
getMinutes() Method
returns the minutes of a date as a number (0-59)
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getMinutes();
</script>
37
getSeconds() Method
returns the milliseconds of a date as a number (0-999)
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getMilliseconds();
</script>
709
getDay() Method
returns the weekday of a date as a number (0-6).
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getDay();
</script>
getTime() Method
returns the number of milliseconds since January 1, 1970
<p id="demo"></p>
<script>
const d = new Date("1970-01-01");
document.getElementById("demo").innerHTML = d.getTime();
</script>
0
Date.now() Method
returns the number of milliseconds since January 1, 1970.
<p id="demo"></p>
<script>
// Calculate milliseconds in a year
const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const year = day * 365;
// Divide Date.now() with a year
let years = Math.round(Date.now() / year);
document.getElementById("demo").innerHTML = years;
</script>
53
'JavaScript' 카테고리의 다른 글
JavaScript - Math Object (0) | 2022.11.17 |
---|---|
JavaScript - Set Date Methods (0) | 2022.11.17 |
JavaScript - Array Iteration (0) | 2022.11.15 |
JavaScript - Sorting Arrays (0) | 2022.11.15 |
JavaScript - Arrays (0) | 2022.11.15 |