Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / How to have millisecon...
Finance forum
Answered

How to have millisecond from current time ?

Posted on by 345
Hi,
 
When creating log table, I want to record what time my method being called, up to millisecond.
At first I'm using this :
         System.DateTime localDateTime = DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::getSystemDateTime(), DateTimeUtil::getUserPreferredTimeZone());
         logTable.ID = localDateTime.ToString('yyyyMMdd_HHmmss.fff');
 
This is no working, so change to this:
         System.DateTime localDateTime = DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::getSystemDateTime(), DateTimeUtil::getUserPreferredTimeZone());
         logTable.ID = strFmt(/%1%2/,localDateTime.ToString('yyyyMMdd_HHmmss'), localDateTime.Millisecond);        
 
Also only give me 0 for he millisecond.
 
Some thread give idea to use WinAPI::GetTickcount(), https://community.dynamics.com/forums/thread/details/?threadid=a7ca3807-2760-4b5e-bab8-7dce477659c6, but I think it is not suitable since it is normally for calculate process time between two tickCount.
 
Thanks
  • Martin Dráb Profile Picture
    Martin Dráb 223,370 Super User on at
    How to have millisecond from current time ?
    Teevo, the problem likely is that you're converting the System.DataTime value to utcDateTime (by using DateTimeUtil) and because utcDateTime doesn't support milliseconds, the information gets lost there.
  • Teevo Profile Picture
    Teevo 345 on at
    How to have millisecond from current time ?
    Yes,
     
    I ended up using that instead.
     
    Many thanks.
  • Verified answer
    Mohamed Amine Mahmoudi Profile Picture
    Mohamed Amine Mahmoudi 4,429 User Group Leader on at
    How to have millisecond from current time ?
    Hi @Teevo,
     
    Try this (it worked for me) 
    System.DateTimeOffset dt = System.DateTimeOffset::Now;
    int ms = dt.Millisecond;
    Best regards,
    Mohamed Amine MAHMOUDI
  • Teevo Profile Picture
    Teevo 345 on at
    How to have millisecond from current time ?
    Hi Martin,
     
    When testing, I always get zero if using System.DateTime::Now(). 
    I'm wondering whether I'm doing correctly or not ? 
    Here is my test :
     
    Thanks.
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 223,370 Super User on at
    How to have millisecond from current time ?
    AX/F&O normally doesn't work with miliseconds, therefore I don't expect that getSystemDateTime() gives you this information. System.DateTime::Now should.
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    Mohamed Amine Mahmoudi 4,429 User Group Leader on at
    How to have millisecond from current time ?
    Hi @Teevo,
     
    if you want to get milliseconds you can use DateTimeOffset
    System.DateTimeOffset dt = System.DateTimeOffset::Now;
    int ms = dt.Millisecond;
    Best regards,
    Mohamed Amine MAHMOUDI
  • Teevo Profile Picture
    Teevo 345 on at
    How to have millisecond from current time ?
    Hi Layan,
     
    Meaning like the first statement ? 
              System.DateTime localDateTime = DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::getSystemDateTime(), DateTimeUtil::getUserPreferredTimeZone());
             logTable.ID = localDateTime.ToString('yyyyMMdd_HHmmss.fff');
     
     
    it's not working. or I did it wrongly ?
     
     
  • Layan Jwei Profile Picture
    Layan Jwei 3,159 Super User on at
    How to have millisecond from current time ?
    Hi Teevo,
     
    Did u try to use this directly?
    DateTimeUtil::getSystemDateTime()
     
    Thanks,
    Layan Jweihan

Helpful resources

Quick Links

Upcoming TechTalk: Copilot Capabilities…

Review the Copilot features that are currently available in Dynamics 365 Finance…

Demystifying Copilot: Service Edition with Sundar Raghavan

Sundar answers more questions about Copilot for Service...

Dynamics 365 Business Central vs Finance and SCM

Take a look at the key differences between Business Central and…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,405 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 223,370 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,140

Featured topics

Product updates

Dynamics 365 release plans