web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Extracting the second from current time

(0) ShareShare
ReportReport
Posted on by

I am using this code to extract the hour minutes and seconds ,and i have given the code inside the loop and i have noticed that while running the code  the time does not changes for each and every loop (even the seconds remains same for every loop ) 

datetime = DateTimeUtil::getSystemDateTime();

filenameToRet = (int2str(DateTimeUtil::year(datetime)))+(int2str(DateTimeUtil::month(datetime)))+(int2str(DateTimeUtil::day(datetime)))+(int2str(DateTimeUtil::hour(datetime)))+(int2str(DateTimeUtil::minute(datetime)))+(int2str(DateTimeUtil::second(datetime)))

Any one please give me the solution 

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Sohaib Cheema Profile Picture
    49,443 User Group Leader on at

    It is working  with me, e.g. execute below code and check results. Don't forget to set your own timezone

     TransDateTime   myDateTime=DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::getSystemDateTime(),Timezone::GMT_CASABLANCA);
        int             hours;
        int             minutes;
        int             seconds;
        
        str filenameToRet;
        utcDateTime datetime = DateTimeUtil::getSystemDateTime();
        
        info(datetime2str(myDateTime));
         
        hours=DateTimeUtil::hour(myDateTime);
        minutes=DateTimeUtil::minute(myDateTime);
        seconds=DateTimeUtil::second(myDateTime);
        info(strfmt('Hours %1 - Minutes %2 - Seconds %3',int2str(hours),int2str(minutes),int2str(seconds)));
        
        
        filenameToRet = (int2str(DateTimeUtil::year(datetime)))+(int2str(DateTimeUtil::month(datetime)))+(int2str(DateTimeUtil::day(datetime)))+(int2str(DateTimeUtil::hour(datetime)))+(int2str(DateTimeUtil::minute(datetime)))+(int2str(DateTimeUtil::second(datetime)));
        info(filenameToRet);



  • Martin Dráb Profile Picture
    237,987 Most Valuable Professional on at

    Make sure you set datetime inside the loop.

    By the way, your code can be simplified to:

    filenameToRet = System.String::Format('{0:yyyyMdHms}', datetime);
  • Community Member Profile Picture
    on at

    Thank you soo much Sohib and Martin

    ur code works perfectly but when i gave inside a while loop the time remains the same for all loop but value of filenameToRet  

    changes for each and every loop how it is happening

    ill attach the screen shot

  • Community Member Profile Picture
    on at

    1537.err.png

  • Sohaib Cheema Profile Picture
    49,443 User Group Leader on at

    it is possible that a loop can take less than one second.

    keeping that in mind for all 5 times, you will get same results.

    Makes sense?

  • Suggested answer
    Rudi Hansen Profile Picture
    4,075 on at

    You are setting the Variable dataTime to current time inside the loop, but you are calculating the values based on the myDateTime variable.

    Also its possible with this loop that you would run all your code in under one second, so your values would not change.

    Try to put in a Sleep(1000) in your loop.

  • Sohaib Cheema Profile Picture
    49,443 User Group Leader on at

    one more point which is not related to your question, but that's related general logic or algorithm

    in your given screenshot, you are setting  i=1; inside loop

    that means you want to go into an infinite loop

    keep it outside loop, if you don't want to be in an infinite loop . it will always be <5 because you are setting every time  i=1

  • Community Member Profile Picture
    on at

    but what is while debugging it makes a delay but even then the time was not changing

    but after changing the mydatetime variable into datetime its working fine :)

    Thank you soo much sohaib for us valuable time

  • Community Member Profile Picture
    on at

    thank you rudi it works

  • Martin Dráb Profile Picture
    237,987 Most Valuable Professional on at

    Try it with stopping the execution for half a second:

    urcDateTime d;
    int i; str s; for (i = 0; i < 5; i++) { d = DateTimeUtil::getSystemDateTime(); s = System.String::Format('{0:yyyyMdHms}', d); info(s); sleep(500); }

    Update: I see I was way too slow. :-)

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans