function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhishek Sharma 527Abhishek Sharma 527 

Date formatting in Exported Data

Hello Experts, I'm working on exporting the data in excel format, report is getting generated but the date format needs to be changed.
here's my Js function
exportNBReport() {
		let doc = '<table>';
		// Add styles for the table
		doc += '<style>';
		doc += 'table, th, td {';
		doc += '    border: 1px solid black;';
		doc += '    border-collapse: collapse;';
		doc += '}';
		doc += '</style>';
		// Add all the Table Headers
		doc += '<tr>';
		this.columnHeader.forEach(element => {
			doc += '<th>' + element + '</th>'
		});
		//alert(JSON.stringify(this.planData));
		doc += '</tr>';
		// Add the data rows
		this.event_session_Array.forEach(record => {
			doc += '<tr>';
      if (record.ProviderName != undefined) {
				doc += '<th>' + record.ProviderName + '</th>';
			} else {
				doc += '<th>' + '</th>';
			}
      if (record.ServiceType1 != undefined) {
				doc += '<th>' + record.ServiceType1 + '</th>';
			} else {
				doc += '<th>' + '</th>';
			}
     
      if (record.AppointmentStart != undefined) {
				doc += '<th>' + record.AppointmentStart + '</th>';
			} else {
				doc += '<th>' + '</th>';
			}
 doc += '</tr>';
		});
		

		doc += '</table>';
		var element = 'data:application/vnd.ms-excel,' + encodeURIComponent(doc);

		let downloadElement = document.createElement('a');
		downloadElement.href = element;
		downloadElement.target = '_self';
		downloadElement.download = 'Appointment Report.xls';
		document.body.appendChild(downloadElement);
		downloadElement.click();
  }

this code is working properly and generating Excel sheet which looks like 
2022-12-30T08:00:00.000Z
2022-12-31T03:45:00.000Z
2023-01-02T02:15:00.000Z
2023-01-03T15:00:00.000Z
2023-01-08T19:30:00.000Z
2023-01-09T19:30:00.000Z
2023-01-10T19:30:00.000Z

I want it to be as - Friday, Dec 30, 2022, 3 AM (this format)
Can anyone please help !!
 
Best Answer chosen by Abhishek Sharma 527
SubratSubrat (Salesforce Developers) 
Hello Abhishek ,

Have you tried using the JavaScript built-in toLocaleString() method to format the date as per your requirement. 

In the above code, we are creating a new Date object from the AppointmentStart property and then using the toLocaleString() method to format it as per the specified options.
 
if (record.AppointmentStart != undefined) {
            const date = new Date(record.AppointmentStart);
            const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', hour12: true };
            const formattedDate = date.toLocaleString('en-US', options);
Hope it helps !
Thank you.