In this post, we are going to learn how to Format the date In JavaScript.The JavaScript does not have a date type. The date object is used to work with dates and times in Javascript. The date class methods help us get the date in different formats as per need.
- getDate():Use to get day of calendar month 1 to 31.
- getMonth(): Use to get month number 0 to 11.
- getFullYear(): Use to get year in 4-digits format.
- getHours():Use to get the hou1. r in 24-hour format.
- getMinutes():Use to get the minutes 0 to 59.
- getSeconds() : Use to get seconds 0 to 59.
let mydate = new Date();
console.log(mydate);
console.log(typeof mydate)
console.log('Date:',mydate.getDate());
console.log('Month:',mydate.getMonth());
console.log('Fullyear:',mydate.getFullYear());
console.log('Hours:',mydate.getHours());
console.log('Minutes:',mydate.getMinutes());
console.log('getSeconds',mydate.getSeconds());
Output
2022-04-12T14:44:38.603Z
Date: 12
Month: 3
Fullyear: 2022
Hours: 16
Minutes: 21
getSeconds 13
1. Get Date in yyyy-mm-dd format
In this javascript program, we are getting a date in yyyy-mm-dd format by using the different function of the date class method that we have mentioned,We have to define a function that takes a date object as a parameter and return the formatted date.
let mydate = new Date()
function paddigit(val) {
return val.toString().padStart(2, '0');
}
function GetFormateddate(mydate) {
return [ mydate.getFullYear(),
paddigit(mydate.getMonth() + 1),
paddigit(mydate.getDate()),
].join('-');
}
console.log('date in yyyy-mm-dd format:',GetFormateddate(mydate))
Output
date in yyyy-mm-dd format: 2022-04-12
2. Get date String in mm-dd-yyyy format
In this javascript program, we have a date class function to get the date in format(dd-mm-yyyy)
let mydate = new Date()
function paddigit(val) {
return val.toString().padStart(2, '0');
}
function GetFormateddate(mydate) {
return [
paddigit(mydate.getMonth() + 1),
paddigit(mydate.getDate()),
mydate.getFullYear()
].join(':');
}
console.log('date dd-mm-yyyy format:',GetFormateddate(mydate))
Output
date dd-mm-yyyy format: 04:12:2022
3. JavaScript Methods for string representation of date
- toString():Convert date object to string
- toTimeString() :Retun time portion of date object as a string
- todateString() : Retun date portion of date object as a string
- toLocaleString() : returns date as string using locale convention
- toLocaleTimeString() : return time portion of date as string using locale convention
- toLocaleDateString() :returns date as string using locale convention
- toUTCString() : Used to get the date object as string according to UTC
- toISOString():Used to get date as string according to ISO standard.
let mydate = new Date()
console.log('Tostring() :\n',mydate.toString());
console.log('\ndate.toTimeString() :\n',mydate.toTimeString());
console.log('\ntoLocaleTimeString() :\n',mydate.toLocaleTimeString());
console.log('\ndate.toUTCString()() :\n',mydate.toUTCString());
console.log('date.toISOString()() :\n',mydate.toISOString());
Output
Tostring() :
Tue Apr 12 2022 16:07:35 GMT+0000 (Coordinated Universal Time)
date.toTimeString() :
16:07:35 GMT+0000 (Coordinated Universal Time)
toLocaleTimeString() :
4:07:35 PM
date.toUTCString()() :
Tue, 12 Apr 2022 16:07:35 GMT
date.toISOString()() :
2022-04-12T16:07:35.782Z
4. toLocaleString() Format date in JavaScript
The date function return date and time in localized format, We can modify the localized date string format by using toLocaleString() by simply passing the Language localization of any country. We can modify a specific section of the date by using toLocaleString() properties. Find all properties in the table below
let mydate = new Date();
console.log(mydate.toLocaleString('bn-IN'));
console.log(mydate.toLocaleString('en-us'));
console.log(mydate.toLocaleString('en-US', {
weekday: 'short',
day: 'numeric',
year: 'numeric',
month: 'short',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
}));
Output
4/12/2022, 3:04:03 PM
4/12/2022, 3:04:03 PM
//Output
//Tue, Apr 12, 2022, 03:37:07 PM
5. Get the date in a Different format using toLocaleString()
let mydate = new Date()
console.log(mydate.toLocaleDateString())
console.log(mydate.toLocaleString('en-IN', {year: 'numeric', month: 'numeric', day: '2-digit', weekday:"long", hour: '2-digit', hour12: false, minute:'2-digit', second:'2-digit'}))
console.log(mydate.toLocaleDateString('en-US', {year: 'numeric', month: '2-digit', day: '2-digit'}))
console.log(mydate.toLocaleDateString('en-ZA'))
console.log (mydate.toLocaleDateString('en-CA'))
console.log(mydate.toLocaleString("en-US", {timeZone: "America/New_York"}))
console.log(mydate.toLocaleString("en-US", {hour: '2-digit', hour12: true, timeZone: "America/New_York"}))
Output
4/12/2022
Tuesday, 4/12/2022, 16:38:22
04/12/2022
4/12/2022
4/12/2022
4/12/2022, 12:38:22 PM
12 PM
6. TodateString() in javascript
javascript to date string() returns only the date part of the data object separated by space in the following format
- The first three letters of the weekday name
- First three letters of the month’s name
- Two-digit day of the month,
- Four-digit year (at least)
let mydate = new Date().toDateString();;
console.log(mydate);
//output :Tue Apr 12 2022
Property | Values |
dateStyle | “full” “long” “medium” “short” |
timeStyle | “full” “long” “medium” “short” |
localeMatcher | “best-fit” (default) “lookup” timeZone |
hour12 | false true |
hourCycle | “h11” “h12” “h23” “h24” |
formatMatcher | “basic” “best-fit” (default) |
weekday | “long” “short” “narrow” |
month | “2-digit” “long” “narrow” “numeric” “short” |
day | “2-digit” “numeric” |
hour | “2-digit” “numeric” |
minute | “2-digit” “numeric” |
second | “2-digit” “numeric” |
timeZoneName | “long” “short” |