Hey guys, today in this post we are going to learn about How to convert date format Using @track with html tag in LWC.
Displays a date and time based on the locale using lightning-formatted-date-time. Displaying Date and Time Using Type Attributes.
A lightning-formatted-date-time component displays formatted date and time. This component uses the Intl.DateTimeFormat JavaScript object to format date values. The locale set in the Salesforce user preferences determines the default formatting. The following input values are supported. To know more details about Formatted Date Time, Click Here.
Final Output
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
Create Lightning Web Component HTML
Step 1:- Create Lightning Web Component : dateFormatLwc.html
SFDX:Lightning Web Component >> New >> dateFormatLwc.html
dateFormatLwc.html [Lightning Web Component HTML]
<template>
<lightning-card title="Convert Date Format in LWC">
<p style="margin:0; padding:10px 0 0 35px; font-size:18px; color:#ff0000;">{forNowDate} </p>
</lightning-card>
</template>
Create Lightning Web Component JavaScript
Step 2:- Create Lightning Web Component : dateFormatLwc.js
SFDX:Lightning Web Component >> New >> dateFormatLwc.js
dateFormatLwc.js [LWC JavaScript File]
import { LightningElement,track } from 'lwc';
export default class DateFormatLwc extends LightningElement {
@track forNowDate;
//Start ConnectedCallback
connectedCallback() {
/*Start Date Format*/
let todayTate = new Date();
const dateStr = new Intl.DateTimeFormat('en', {
year: 'numeric',
month: 'short',
day: '2-digit'
})
const [{value: mo}, , {value: da}, , {value: ye}] = dateStr.formatToParts(todayTate);
let formatedDate = `${da}-${mo}-${ye}`;
this.forNowDate=formatedDate;
console.log('nowdDate### ' , this.v);
/*End Date Format*/
}
//End ConnectedCallback
}
Create Lightning Web Component Meta XML
Step 3:- Create Lightning Web Component : dateFormatLwc.js-meta.xml
SFDX:Lightning Web Component >> New >> dateFormatLwc.js-meta.xml
dateFormatLwc.js-meta.xml [LWC Meta Data XML]
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>56.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Further post that would you like to learn in Salesforce
What is the date format in Salesforce LWC?
Salesforce uses the ISO8601 format YYYY-MM-DDThh:mm:ss.SZ for date/time fields, which stores date/time in UTC.
Can you change the date format in Salesforce?
One can use formulas to reformat a date that is generated from Salesforce to another format such as MM/DD/YYYY or MM-DD-YYYY. Create a Formula Field of the type Text to convert Date or Date/Time fields or Functions to the required format.
How do I change the format of a date object?
The function new Date() when called returns the Date object and works like a constructor. Explanation: Here, the above Javascript function new Date() returns the Date object and we get the formatted string as day/ month(in string format) and year.
Related Topics | You May Also Like
Our Free Courses β
π Get Free Course β
![]() π Salesforce Administrators π Salesforce Lightning Flow Builder π Salesforce Record Trigger Flow Builder |
π Get Free Course β
![]() π Aura Lightning Framework π Lightning Web Component (LWC) π Rest APIs Integration |