In JavaScript, the Date
object provides several methods for working with dates and times. Here are the commonly used methods of the Date
object:
getMilliseconds(): Returns the milliseconds (from 0 to 999) of the specified date and time.
getSeconds(): Returns the seconds (from 0 to 59) of the specified date and time.
getMinutes(): Returns the minutes (from 0 to 59) of the specified date and time.
getHours(): Returns the hour (from 0 to 23) of the specified date and time.
getDate(): Returns the day of the month (from 1 to 31) of the specified date and time.
getMonth(): Returns the month (from 0 to 11) of the specified date and time.
getFullYear(): Returns the year (four digits for dates between 1000 and 9999) of the specified date and time.
getDay(): Returns the day of the week (from 0 for Sunday to 6 for Saturday) of the specified date and time.
toDateString(): Returns the date portion of the
Date
object as a human-readable string.toISOString(): Returns a string representing the
Date
object as an ISO-8601 formatted string (e.g., "2024-02-16T12:34:56.789Z").toString(): Returns a string representing the
Date
object.toLocaleDateString(): Returns a string representing the date portion of the
Date
object using the locale's conventions.toLocaleTimeString(): Returns a string representing the time portion of the
Date
object using the locale's conventions.toLocaleString(): Returns a string representing the
Date
object using the locale's conventions for both date and time.getTime(): Returns the number of milliseconds since January 1, 1970, 00:00:00 UTC represented by the
Date
object.getTimezoneOffset(): Returns the time zone difference, in minutes, between the current locale's time zone and UTC.
These methods allow you to manipulate and extract various parts of a Date
object in JavaScript. You can use them to perform tasks such as displaying dates, calculating durations, or formatting date and time information.
const currentDate = new Date();
// Method 1: getMilliseconds()
const milliseconds = currentDate.getMilliseconds();
// Method 2: getSeconds()
const seconds = currentDate.getSeconds();
// Method 3: getMinutes()
const minutes = currentDate.getMinutes();
// Method 4: getHours()
const hours = currentDate.getHours();
// Method 5: getDate()
const dayOfMonth = currentDate.getDate();
// Method 6: getMonth()
const month = currentDate.getMonth();
// Method 7: getFullYear()
const year = currentDate.getFullYear();
// Method 8: getDay()
const dayOfWeek = currentDate.getDay();
// Method 9: toDateString()
const dateString = currentDate.toDateString();
// Method 10: toISOString()
const isoString = currentDate.toISOString();
// Method 11: toString()
const dateStringVerbose = currentDate.toString();
// Method 12: toLocaleDateString()
const localeDateString = currentDate.toLocaleDateString();
// Method 13: toLocaleTimeString()
const localeTimeString = currentDate.toLocaleTimeString();
// Method 14: toLocaleString()
const localeString = currentDate.toLocaleString();
// Method 15: getTime()
const timeInMillis = currentDate.getTime();
// Method 16: getTimezoneOffset()
const timezoneOffset = currentDate.getTimezoneOffset();
// Assuming you have a function to post data to a server, here's an example of posting the results
const postData = {
milliseconds,
seconds,
minutes,
hours,
dayOfMonth,
month,
year,
dayOfWeek,
dateString,
isoString,
dateStringVerbose,
localeDateString,
localeTimeString,
localeString,
timeInMillis,
timezoneOffset
};
Top comments (0)