Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

get current datetime

(0) ShareShare
ReportReport
Posted on by

What the best line of code for getting the current date time for user preferred time zone?

  • GirishS Profile Picture
    GirishS 27,832 Super User 2024 Season 1 on at
    RE: get current datetime

    I guess both will return same output.

    Have you check that?

    Thanks

    Girish S.

  • brianmaric Profile Picture
    brianmaric on at
    RE: get current datetime

    is that different from this..

    DateTimeUtil::newDateTime(DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()),

                             DateTimeUtil::getTimeNow(DateTimeUtil::getUserPreferredTimeZone()));

  • Suggested answer
    nunomaia Profile Picture
    nunomaia 10,684 Super User 2024 Season 1 on at
    RE: get current datetime
    [quote user="Nuno Maia"]

    DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::getSystemDateTime(), DateTimeUtil::getUserPreferredTimeZone()))

    [/quote]

    You can apply any time zone using  applyTimeZoneOffset method 

  • brianmaric Profile Picture
    brianmaric on at
    RE: get current datetime

    nice, but need the time zone applied

  • Suggested answer
    GirishS Profile Picture
    GirishS 27,832 Super User 2024 Season 1 on at
    RE: get current datetime

    Hi brianmaric,

    In addition to the Nuno Maia, You can also make use of "utcNow"method in "DateTimeUtil" class.

    Thanks,

    Girish S.

  • Verified answer
    nunomaia Profile Picture
    nunomaia 10,684 Super User 2024 Season 1 on at
    RE: get current datetime

    DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::getSystemDateTime(), DateTimeUtil::getUserPreferredTimeZone()))

  • brianmaric Profile Picture
    brianmaric on at
    RE: get current datetime

    That's date not datetime

  • nunomaia Profile Picture
    nunomaia 10,684 Super User 2024 Season 1 on at
    RE: get current datetime

    Try this 

    DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()))

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 Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,503 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans