09+ Methods to Get Date in JavaScript
JavaScript provides various methods to work with dates and times. These methods allow developers to retrieve and manipulate date-related information, making it easier to build dynamic web applications. Below is a detailed explanation of 09+ methods to get dates in JavaScript along with practical examples.
01. Date.now()
The Date.now()
method returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC (Unix Epoch).
Example:
const timestamp = Date.now();
console.log("Current Timestamp:", timestamp);
02. new Date().getFullYear()
The getFullYear()
method returns the year of a date as a four-digit number.
Example:
const currentYear = new Date().getFullYear();
console.log("Current Year:", currentYear);
03. new Date().getMonth()
The getMonth()
method returns the month of a date as a zero-based index (0 for January, 1 for February, etc.). To get a one-based month, simply add 1.
Example:
const currentMonth = new Date().getMonth() + 1;
console.log("Current Month:", currentMonth);
04. new Date().getDate()
The getDate()
method returns the day of the month (1-31) for the specified date.
Example:
const currentDate = new Date().getDate();
console.log("Current Date:", currentDate);
05. new Date().getDay()
The getDay()
method returns the day of the week as a number (0 for Sunday, 1 for Monday, etc.).
Example:
const currentDay = new Date().getDay();
console.log("Current Day (0-Sunday to 6-Saturday):", currentDay);
06. new Date().getHours()
The getHours()
method returns the hour (0-23) of the specified date.
Example:
const currentHour = new Date().getHours();
console.log("Current Hour:", currentHour);
07. new Date().getMinutes()
The getMinutes()
method returns the minutes (0-59) of the specified date.
Example:
const currentMinutes = new Date().getMinutes();
console.log("Current Minutes:", currentMinutes);
08. new Date().getTime()
The getTime()
method returns the number of milliseconds since January 1, 1970.
Example:
const currentTime = new Date().getTime();
console.log("Current Time in Milliseconds:", currentTime);
09. new Date().toISOString()
The toISOString()
method converts a date object to a string in ISO 8601 format.
Example:
const isoDate = new Date().toISOString();
console.log("ISO Format Date:", isoDate);
10. new Date().toLocaleDateString()
The toLocaleDateString()
method returns the date in a localized string format.
Example:
const localDate = new Date().toLocaleDateString();
console.log("Localized Date String:", localDate);
11. new Date().toLocaleString()
The toLocaleString()
method returns the date and time in a localized string format.
Example:
const localString = new Date().toLocaleString();
console.log("Localized Date and Time:", localString);
12. new Date().toDateString()
The toDateString()
method converts the date portion of a Date object to a readable string.
Example:
const readableDate = new Date().toDateString();
console.log("Readable Date:", readableDate);
Conclusion
These methods provide a wide range of functionalities to retrieve and manipulate date and time in JavaScript. Understanding these methods is crucial for building robust and dynamic applications. By combining these methods, developers can handle complex date and time requirements efficiently.
Comments
Post a Comment