GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — JavaScript - Get Date Methods
Selected Reading
JavaScript - Get Date MethodsGet Date MethodsJavaScript utilizes the Date object for date and time manipulation. This object offers an array of methods that facilitate retrieval and modification of date-specific information. Here, we will discuss about the get date methods within JavaScript which fetch different components of the date/time. Below is a table of the most commonly used get date methods and their corresponding description.
ExamplesExample 1: Simple demonstration of get date methodsThe following example demonstrates the fundamental application of prevalent JavaScript date methods: It instantiates a novel Date object to represent the present date and time; subsequently, it exhibits an array of diverse components - year, month, day; hours, minutes, seconds, milliseconds; along with their corresponding UTC counterparts. The displayed elements encompass not only standard temporal divisions but also supplementary information about weekdays: thus providing comprehensive insight into current temporal dynamics. <!DOCTYPE html>
<html>
<head>
<title> Exxample to demonstrate get date methods in JavaScript</title>
</head>
<body>
<script>
// Create a new Date object
const currentDate = new Date();
function displayResult(methodName, result) {
const resultDiv = document.createElement('div');
resultDiv.innerHTML = `${methodName}: ${result}`;
document.body.appendChild(resultDiv);
}
displayResult('getFullYear()', currentDate.getFullYear());
displayResult('getMonth()', currentDate.getMonth());
displayResult('getDate()', currentDate.getDate());
displayResult('getHours()', currentDate.getHours());
displayResult('getMinutes()', currentDate.getMinutes());
displayResult('getSeconds()', currentDate.getSeconds());
displayResult('getMilliseconds()', currentDate.getMilliseconds());
displayResult('getDay()', currentDate.getDay());
displayResult('getUTCFullYear()', currentDate.getUTCFullYear());
displayResult('getUTCMonth()', currentDate.getUTCMonth());
displayResult('getUTCDate()', currentDate.getUTCDate());
displayResult('getUTCHours()', currentDate.getUTCHours());
displayResult('getUTCMinutes()', currentDate.getUTCMinutes());
displayResult('getUTCSeconds()', currentDate.getUTCSeconds());
displayResult('getUTCMilliseconds()', currentDate.getUTCMilliseconds());
</script>
</body>
</html>
Example 2: Comparison of two datesIn this example, the Date constructor creates two specific dates: date1 and date2. The script subsequently compares these dates; it displays their formatted representations, along with a message indicating if date1 is later, earlier or equal to date2. <!DOCTYPE html>
<html>
<head>
<title>Comparison of two dates in JavaScript</title>
</head>
<body>
<script>
const date1 = new Date(2024, 0, 18);
const date2 = new Date(2024, 0, 26);
function displayComparison() {
const date1Div = document.createElement('div');
date1Div.innerHTML = `Date 1: ${date1.toDateString()}`;
document.body.appendChild(date1Div);
const date2Div = document.createElement('div');
date2Div.innerHTML = `Date 2: ${date2.toDateString()}`;
document.body.appendChild(date2Div);
const resultDiv = document.createElement('div');
if (date1 > date2) {
resultDiv.innerHTML = "date 1 is later than date 2";
} else if (date1 < date2) {
resultDiv.innerHTML = "date 1 is earlier than date 2";
} else {
resultDiv.innerHTML = "Both dates are equal";
}
document.body.appendChild(resultDiv);
}
displayComparison();
</script>
</body>
</html>
Advertisements
| ||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |