Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

date2str Error occurred in comparison after conversion to YMD format

(0) ShareShare
ReportReport
Posted on by 75

Event description

In my code, the following judgment is implemented.

In recent years, it has been able to return successfully. However, in the recent implementation process, there have been misjudgments.

I can only deduce based on the log results, so I can judge that the judgment here may be wrong.

Can you help me analyze the reason?

//Get the business date of the day

  businessDate = BusinessDay::getBusinessDay(#DateCategory_Day, 0);

// Get the date one year ago
  businessLastYearDay = uaBusinessDay::getBusinessDay(#DateCategory_Day, -364);
  lastYearBusinessDayStr = date2str(businessLastYearDay,DateFormat::YMD,
                                      DateDay::Digits2,DateSeparator::None,
                                      DateMonth::Digits2,DateSeparator::None,
                                      DateYear::Digits2,DateFlags::None);


// Get the date after one month
businessNextMonthDay = BusinessDay::getBusinessDay('03', 1);
nextMonthBusinessDayStr = date2str(businessNextMonthDay,DateFormat::YMD,
                                      DateDay::Digits2,DateSeparator::None,
                                      DateMonth::Digits2,DateSeparator::None,
                                      DateYear::Digits2,DateFlags::None);



VoucherDate = ‘220701’ ;

// Time valid range check
    if (VoucherDate < lastYearBusinessDayStr||VoucherDate > nextMonthBusinessDayStr)
    {

        errorMessage = "Time error";

    }

                                    

  • Martin Dráb Profile Picture
    232,850 Most Valuable Professional on at
    RE: date2str Error occurred in comparison after conversion to YMD format

    No, things like memory usage won't change the behavior of your method. It seems that there was data that your code didn't expect, but I can't guess what it was.

  • xiaoshuai.fan Profile Picture
    75 on at
    RE: date2str Error occurred in comparison after conversion to YMD format

    As you said, there is no problem with date2str.

    Based on the phenomenon that has entered the IF judgment of the following logic, the reason for the error may be the calculation of the time one year ago or one month later?

    I've tried to reproduce the error, once the error reproduces, the problem is solved, isn't it?

    But the current difficulty is not being able to reproduce this error.

    The debug code is also fine. However, in the production environment, the phenomenon that 220701 failed to pass the verification did occur, and the output was output through Log. There have only been two such mistakes in the recent five-year record.

    If exclusion is an error in the code, what could cause an assignment, calculation, or judgment error? High memory usage, multiple different programs executing at the same time?

    //Get the business date of the day
    date businessDate = '2022-07-01';
    
    // Get the date one year ago
    date businessLastYearDay = businessDate   -364;
    str lastYearBusinessDayStr = date2str(businessLastYearDay,DateFormat::YMD,
                                          DateDay::Digits2,DateSeparator::None,
                                          DateMonth::Digits2,DateSeparator::None,
                                          DateYear::Digits2,DateFlags::None);
    
    // Get the date after one month
    date businessNextMonthDay = prevMth(businessDate);
    str nextMonthBusinessDayStr = date2str(businessNextMonthDay,DateFormat::YMD,
                                          DateDay::Digits2,DateSeparator::None,
                                          DateMonth::Digits2,DateSeparator::None,
                                          DateYear::Digits2,DateFlags::None);
    
    str VoucherDate = '220701';
                                          
    // Time valid range check
    if (VoucherDate < lastYearBusinessDayStr||VoucherDate > nextMonthBusinessDayStr)
    {
        errorMessage = "Time error";
    }

  • Martin Dráb Profile Picture
    232,850 Most Valuable Professional on at
    RE: date2str Error occurred in comparison after conversion to YMD format

    I don't believe that there is a bug in the standard function date2str() and it's irrelevant when you do the comparison correctly. Throw away the conversion to string and compare the values as dates, not as strings.

    Note that I have no idea what you mean by things "a formal environment" or "the value at the time of judgment execution cannot be known now". Things like lastYearBusinessDayStr are not values - they're names of variables. It doesn't tell us what values are inside those variable at the time of execution.

    It seems to me that you have no idea what happens in your code and your assumptions (such as "there is a conversion error in date2str") are wild guesses not based on any data. What you need is reproducing the problem, debugging code and testing assumptions.

  • xiaoshuai.fan Profile Picture
    75 on at
    RE: date2str Error occurred in comparison after conversion to YMD format

    Thank you for your reply

    Because it is the code running in the formal environment, it cannot be changed to the judgment of date for the time being. This will be noticed in the future development.

    At the same time, I also believe that the value when entering the if block is inconsistent with that when debugging fails. The value at the time of judgment execution cannot be known now, but the source of this value can be found, that is, these three values, and the assignment process is also recorded above.

    VoucherDate, lastYearBusinessDayStr||VoucherDate ,nextMonthBusinessDayStr

    In particular, the value of voucherdate is the business date of the day and will not be changed at will.

    So now I can only think that there is an unknown conversion error in date2str, or an unknown error in the comparison between strymd.

    What suggestions do you have in this regard

  • Martin Dráb Profile Picture
    232,850 Most Valuable Professional on at
    RE: date2str Error occurred in comparison after conversion to YMD format

    Why exactly don't you compare dates? You're adding a lot of potential problems by converting dates to strings and comparing strings. Comparing dates is easier and more efficient.

    If you didn't get inside the 'if' block when debugging your code, then you either debugged different code or (more likely) there were different values when you debugged than when it failed.

  • xiaoshuai.fan Profile Picture
    75 on at
    RE: date2str Error occurred in comparison after conversion to YMD format

    Question supplement

    When my value is 220701,

    it is obvious that I meet the condition from 1 year ago to 1 month later,

    but in the latest execution, I reported the error 'time error' in if judgment

    VoucherDate = '220701';
    
    // Time valid range check
        if (VoucherDate < lastYearBusinessDayStr||VoucherDate > nextMonthBusinessDayStr)
        {
    
            errorMessage = "Time error";
    
        }

    I debugged the code, and the execution result told me that it was OK,

    There was no problem in the implementation of the plan for one year, but there were two such misjudgments recently.


    This makes me not even sure whether it is the error of converting date type to date2str(YMD) or the error of date2str(YMD) comparison

    Could you please guide me in what aspects to investigate.

    At present, I have carried out the following operations:

    Debug the code and go through the whole process, OK

    Run the overall code in the simulated environment, OK

    Thank you.

  • Martin Dráb Profile Picture
    232,850 Most Valuable Professional on at
    RE: date2str Error occurred in comparison after conversion to YMD format

    Can you explain your problem, please? You mentioned an error, but you didn't tell us what the error says and where it occurs.

    The title also says that the problem is related to a comparison, but I don't see any on your code.

  • xiaoshuai.fan Profile Picture
    75 on at
    RE: date2str Error occurred in comparison after conversion to YMD format

    The following is the method of the method 【Table:BusinessDay::getBusinessDay】

    public static server BusinessDateEdt getBusinessDay(str _dateCategory, int _value)
    {
        #define.ErrorMethoName('Business day acquisition')
    
        BusinessDateEdt   BusinessDateEdt;
        BusinessDay       BusinessDay;
        int                 loop;
    
        if(_dateCategory)
        {
            select firstOnly BusinessDate from BusinessDay;
            if(BusinessDay.BusinessDate)
            {
                BusinessDateEdt = BusinessDay.BusinessDate;
    
                switch(_dateCategory)
                {
                    case #DateCategory_Day :
                        BusinessDateEdt = BusinessDateEdt   _value;
                        break;
    
                    case #DateCategory_Week :
                        BusinessDateEdt = BusinessDateEdt    #daysPerWeek * _value;
                        break;
    
                    case #DateCategory_Month :
                        //Calculate date based on minus addition date
                        if(_value < 0)
                        {
                            for(loop = 1; loop <= abs(_value); loop  )
                            {
                                BusinessDateEdt = prevMth(BusinessDateEdt);
                            }
                        }
                        else
                        {
                            for(loop = 1; loop <= _value; loop  )
                            {
                                BusinessDateEdt = nextMth(BusinessDateEdt);
                            }
                        }
    
                        break;
    
                    case #DateCategory_Year :
                        //Calculate date based on minus addition date
                        if(_value < 0)
                        {
                            for(loop = 1; loop<= abs(_value); loop  )
                            {
                                BusinessDateEdt = prevYr(BusinessDateEdt);
                            }
                        }
                        else
                        {
                            for(loop = 1; loop<= _value; loop  )
                            {
                                BusinessDateEdt = nextYr(BusinessDateEdt);
                            }
                        }
    
                        break;
                    default :
                        // Parameter wrong
                        throw Global::error(strFmt("@SYS325448", _dateCategory, #ErrorMethoName));
                }
            }
            else
            {
                throw Global::error("@CME00114");
            }
        }
    
        return BusinessDateEdt;
    }

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,998 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,850 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans