Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Time using js in d365

Posted on by 595

Hi All,

I have a requirement that I need to set shift of current logged in user current time using predefined shift range as master.

How can I get the current time in 12 hrs format with AM/PM using js?

Any suggestions please help me to achieve this.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Time using js in d365

    Hi Abhilash,

    You can use following js code to show current time with 12 hour AM/PM format:

    We will apply modulo “%” operator to find the hour in 12-hour format and use conditional “?:” operator to apply “AM” or “PM”.

     

    function formatAMPM() {
        var date = new Date();
        var hours = date.getHours();
        var minutes = date.getMinutes();
        // Check whether AM or PM
        var ampm = hours >= 12 ? 'PM' : 'AM';
        // Find current hour in AM-PM Format
        hours = hours % 12;
        hours = hours ? hours : 12; // the hour '0' should be '12'
        minutes = minutes < 10 ? '0'   minutes : minutes;
        var strTime = hours   ':'   minutes   ' '   ampm;
        return strTime;
    }
    console.log(formatAMPM(new Date));

    And following is test result:

    pastedimage1617071773367v6.png

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • Suggested answer
    Fernando Acevedo Profile Picture
    Fernando Acevedo 65 on at
    RE: Time using js in d365
    [quote user="Abhilash"]

    Hi All,

    I have a requirement that I need to set shift of current logged in user current time using predefined shift range as master.

    How can I get the current time in 12 hrs format with AM/PM using js?

    Any suggestions please help me to achieve this.

    [/quote]

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans