JavaScript Date Formatting

In JavaScript, we have too many useful methods for the Date data type using which we can easily format the Date value for display. Please check the following for your reference.

Sample JavaScript Code:

let selectedDate = new Date( '2024-09-30' );
let timeOffset = selectedDate.getTimezoneOffset();
selectedDate = new Date( selectedDate.getTime() + ( timeOffset * 60000 ) );
console.log( selectedDate );
const days = [ 
	"Sunday", 
	"Monday", 
	"Tuesday", 
	"Wednesday", 
	"Thursday", 
	"Friday", 
	"Saturday" 
];
const months = [
	'JAN',
	'FEB',
	'MAR',
	'APR',
	'MAY',
	'JUN',
	'JUL',
	'AUG',
	'SEP',
	'OCT',
	'NOV',
	'DEC'
];
let strMessage = selectedDate.getDate().toString() + ' ' + 
months[ selectedDate.getMonth() ] + ' ' +
selectedDate.getFullYear() + ', ' +
days[ selectedDate.getDay() ];
console.log(
	'Formatted string is',
	strMessage
);

Leave a Reply