24 May 2023

How to show the current time and date in Lightning Web Components (LWC) in Salesforce

use JavaScript's Date object and the LWC template syntax. Here's an example of how you can achieve this:

Open your LWC component's JavaScript file (e.g., myComponent.js) and define a property to store the current date and time.


import { LightningElement, track } from 'lwc';

export default class MyComponent extends LightningElement {

    @track currentDate;

    connectedCallback() {

        this.getCurrentDateTime();

    }

    getCurrentDateTime() {

        setInterval(() => {

            const now = new Date();

            this.currentDate = now.toLocaleString(); // Format the date and time as per your preference

        }, 1000); // Update every second

    }

}

Open your LWC component's HTML file (e.g., myComponent.html) and use the currentDate property to display the date and time.


<template>

    <div>{currentDate}</div>

</template>


 Now, when you use the MyComponent in your Lightning page, it will continuously update to display the current date and time.

Note: The example above sets the interval to update the date and time every second (1000 milliseconds). You can adjust the interval value to your desired frequency.

 

To display the current date and time in a specific format in Lightning Web Components (LWC) in Salesforce, you can use the toLocaleString method of the Date object and specify the desired format options. Here's an example:

import { LightningElement, track } from 'lwc';

export default class MyComponent extends LightningElement {

    @track currentDate;

    connectedCallback() {

        this.getCurrentDateTime();

    }

    getCurrentDateTime() {

        setInterval(() => {

            const now = new Date();

            const options = {

                year: 'numeric',

                month: 'long',

                day: 'numeric',

                hour: 'numeric',

                minute: 'numeric',

                second: 'numeric',

                hour12: true // Use 12-hour format (true) or 24-hour format (false)

            };

            this.currentDate = now.toLocaleString('en-US', options); // Adjust the locale as per your requirement

        }, 1000); // Update every second

    }

}


To display the current date and time in Indian date format using Lightning Web Components (LWC) in Salesforce, you can modify the options object in the toLocaleString method to match the desired format. Here's an example:

import { LightningElement, track } from 'lwc';

export default class MyComponent extends LightningElement {

    @track currentDate;

    connectedCallback() {

        this.getCurrentDateTime();

    }

    getCurrentDateTime() {

        setInterval(() => {

            const now = new Date();

            const options = {

                weekday: 'long',

                year: 'numeric',

                month: 'long',

                day: 'numeric',

                hour: 'numeric',

                minute: 'numeric',

                second: 'numeric',

                hour12: true, // Use 12-hour format (true) or 24-hour format (false)

                timeZone: 'Asia/Kolkata', // Set the desired time zone

                timeZoneName: 'short' // Show abbreviated time zone name

            };

            this.currentDate = now.toLocaleString('en-IN', options); // Use 'en-IN' locale for Indian date format

        }, 1000); // Update every second

    }

}