How to convert date format Using @track with html tag in lwc | How to format today Date in DD/MM/YYYY in lightning web components | Convert String To Current Date In Salesforce LWC

6,484 views

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.

Challenges in your career

🎯 If You Are Facing Any Of These 6 Challenges in your career.

  • Learn Salesforce Development
  • Career Confusion
  • No Interview Call
  • Low Salary
  • No Promotion/Growth
  • No Finding New Job Opportunity
  • Why you stucking from past so many years in same company?

 

 

Final Output

convert date format in lwc -- w3web.net

 

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]

  1.   <template>
  2.     <lightning-card title="Convert Date Format in LWC">
  3.  
  4.        <p style="margin:0; padding:10px 0 0 35px;  font-size:18px; color:#ff0000;">{forNowDate} </p>
  5. </lightning-card>
  6. </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]

  1.   import { LightningElement,track } from 'lwc';
  2.  
  3. export default class DateFormatLwc extends LightningElement {
  4.  
  5.     @track forNowDate;
  6.  
  7.  
  8.     //Start ConnectedCallback
  9.  
  10.     connectedCallback() {
  11.  
  12.         /*Start Date Format*/
  13.             let todayTate = new Date();
  14.             const dateStr = new Intl.DateTimeFormat('en', {
  15.                 year: 'numeric',
  16.                 month: 'short',
  17.                 day: '2-digit'
  18.             })
  19.             const [{value: mo}, , {value: da}, , {value: ye}] = dateStr.formatToParts(todayTate);
  20.  
  21.             let formatedDate = `${da}-${mo}-${ye}`;
  22.             this.forNowDate=formatedDate;
  23.             console.log('nowdDate### ' , this.v);
  24.  
  25.             /*End Date Format*/
  26.  
  27.     }
  28.  
  29. //End ConnectedCallback
  30.  
  31. }

 

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]

  1.   <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>56.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets> 
  6.         <target>lightning__AppPage</target>
  7.         <target>lightning__RecordPage</target>
  8.         <target>lightning__HomePage</target>
  9.     </targets>
  10. </LightningComponentBundle>

 
convert datetime to date in lwc -- w3web.net
 

Further post that would you like to learn in Salesforce

 


 

FAQ (Frequently Asked Questions)

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



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment