JavaScript provides several ways to work with dates and times, including various date formats. Here are some common date formats in JavaScript:
ISO Date Format: This is a simplified version of the ISO 8601 standard. It follows the pattern YYYY-MM-DD
, where:
YYYY
represents the year (4 digits)MM
represents the month (2 digits, 01-12)DD
represents the day (2 digits, 01-31)let isoDate = new Date('2024-04-21');
Long Date Format: This format includes the full name of the weekday, month, day, and year. For example: April 21, 2024
.
let longDate = new Date().toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });
Short Date Format: This format includes the numeric representation of the month, day, and year. For example: 4/21/2024
.
let shortDate = new Date().toLocaleDateString('en-US');
Time Format: This format represents the time using hours, minutes, seconds, and optionally milliseconds. For example: 12:30:45
.
let time = new Date().toLocaleTimeString('en-US');
DateTime Format: This format combines both the date and time components. For example: 4/21/2024, 12:30:45 PM
.
let dateTime = new Date().toLocaleString('en-US');
Custom Format: You can also create custom date formats using various methods like getFullYear()
, getMonth()
, getDate()
, getHours()
, getMinutes()
, getSeconds()
, etc., and concatenate the values as needed.
let customFormat = `${new Date().getFullYear()}-${(new Date().getMonth() + 1).toString().padStart(2, '0')}-${new Date().getDate().toString().padStart(2, '0')}`;
These are just a few examples of how you can format dates in JavaScript. Depending on your requirements, you may need to use different methods and libraries to achieve specific date formatting needs.