Skip to main content

Notifications

Announcements

No record found.

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

To pass string date value into the date value in form .

Posted on by 1,457
hi everyone 
i am having two dates user expiry date and user date which is string , so when the user enter the date in this two fields the both the date should be converted  pass into the calander start date and calander expire date .how can i get this done plz help me on this .
  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    To pass string date value into the date value in form .
    Please test your code to see whether it works or not.
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,457 on at
    To pass string date value into the date value in form .
    hi , martin i am trying to move the logic on table , 
    can you plz let me know if it is correct , code is below , 
    public class DTPropertyLine extends common
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name = "_fieldId"></param>
        public void modifiedField(FieldId _fieldId)
        {
            super(_fieldId);
    
            switch (_fieldId)
            {
                case fieldNum(DTPropertyLine, DTHijriDate):
                    this.DTCalenderStartDate = DTHijri2date::hijri2GrDate(this.DTHijriDate);
                    break;
                case fieldNum(DTPropertyLine, DTHijriExpiryDate):
                    this.DTCalenderExpireDate = DTHijri2date::hijri2GrDate(this.DTHijriExpiryDate);
                    break;
            }
    
        }
    
        /// <summary>
        ///
        /// </summary>
        /// <param name = "_fieldIdToCheck"></param>
        /// <returns></returns>
        public boolean validateField(FieldId _fieldIdToCheck)
        {
            boolean ret;
        
            ret = super(_fieldIdToCheck);
            if (ret)
            {
                switch (_fieldIdToCheck)
                {
                    case fieldnum(DTPropertyLine,DTCalenderExpireDate):
                      if (this.DTCalenderExpireDate <= this.DTCalenderStartDate)
                      ret = checkFailed("Calender Start Date must no be greater than Calender Expire Date.");
                }
            }
    
            return ret;
        }
    }
    thanks ,
    Regards,
    Dinesh
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,457 on at
    To pass string date value into the date value in form .
    hi 
    martin 
    thanks for helping me out , 
    the values are getting converted now , due to the size of array system.string[18]  there was error which i updated to system.string[2] .the values are getting inserted on rwquired fields now.
    thanks,
    Regards ,
    Dinesh
  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    To pass string date value into the date value in form .
    Your last question isn't related to the topic of this thread, which is To pass string date value into the date value in form. It has nothing to do with a form, it's about a conversion of Hirji date 10/04/1445. Please create a new thread (with an appropriate title) and we can discuss it there. Here we'll discuss the form logic, although I suspect that there should be none (because the logic should actually be implemented on the table, as discussed below).
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,457 on at
    To pass string date value into the date value in form .
    hi martin 
    thanks for code , i tried to take value from formstringcontrol now the value is passing into method but the date is not getting converted , i have debugged the code and getting error
    "Unexpected Hirji date format." my date is not converting but i am getting the value in variable  "hijriDateStr". but in try statement its not converting the values , i have entered this value from front end 10/04/1445 to convert . and i have changed the code in modified ,method  the code is below . 
      [Control("String")]
        class DTPropertyLine_DTHijriDate
        {
            /// <summary>
            ///
            /// </summary>
            /// <returns></returns>
            public boolean modified()
            {
                boolean ret;
                DTHijriDate   hijriDateStr;
                ret = super();
                hijriDateStr =  this.text();
                DTPropertyLine.DTCalenderStartDate = DTHijri2date::hijri2GrDate(hijriDateStr);
        
                return ret;
            }
    
        }
     
     
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    To pass string date value into the date value in form .
    First of all, hijriDateStr variable will always be empty, because you never assign any value to it. I wonder how you debugged your code that you didn't notice such an obvious problem.
     
    Then you should pay more attention to where you put your logic.
     
    I'm assuming that DTPropertyLine_DTHijriDate control is bound to a data source field (DTPropertyLine.DTHijriDate). Your code suggests that the conversion should happen only for this specific form control and not to any other control bound to the same field (which may be added both by developers and users). This sounds like a bug in your design.
     
    Another possible place is a data source field, but I think it would be wrong to in this case. If the logic isn't specific to this single form, the right place is doing it on the table. The table method executed when a field changes is modifiedField().
    public void modifiedField(FieldId _fieldId)
    {
        switch (_fieldId)
        {
            case fieldNum(DTPropertyLine, DTHijriDate):
                this.DTCalenderStartDate = DTHijri2date::hijri2GrDate(this.DTHijriDate);
                break;
        }
    }
    Another option is doing it when saving the record (insert()/update()).
     
    Also, I've noticed that you still didn't throw away the extra conversions back to string and to date again. Read my first reply again if you missed the problem report.
     
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,457 on at
    To pass string date value into the date value in form .
    hi martin ,
      I have made the class , code is below 
    final class DTHijri2date
    {
        static date hijri2GrDate(DTHijriDate   hijriDateStr)
        {
            System.Globalization.CultureInfo arCul = new System.Globalization.CultureInfo("ar-SA");
            System.Globalization.CultureInfo enCul = new System.Globalization.CultureInfo("en-US");
            System.DateTime                  tempDateTime;
            str                              strTemp;
            System.String[]                  arr;
            date                             grDate;
            ;
     
            //all expected dates formats
            arr = new System.String[18]();
        
            arr.SetValue("dd/MM/yyyy",  1);
            arr.SetValue("dd-MM-yyyy",  2);
     
            try
            {
                tempDateTime = System.DateTime::ParseExact(hijriDateStr, arr, arCul, System.Globalization.DateTimeStyles::AllowWhiteSpaces);
            }
            catch
            {
                error("Unexpected Hirji date format.");
                return datenull();
            }
            strTemp = tempDateTime.ToString("dd/MM/yyyy");
            grDate = str2date(strTemp, 123);
     
            return grDate;
        }
    
    }
    
     [Control("String")]
        class DTPropertyLine_DTHijriDate
        {
            /// <summary>
            ///
            /// </summary>
            /// <returns></returns>
            public boolean modified()
            {
                boolean ret;
                DTHijriDate   hijriDateStr;
                ret = super();
                       
                DTPropertyLine.DTCalenderStartDate = DTHijri2date::hijri2GrDate(hijriDateStr);
        
                return ret;
            }
          
        }
     
    call it in my modified method , plz let me know if i am missing any thing .
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,457 on at
    To pass string date value into the date value in form .
     hi martin ,
    i the user enter the hijri date it should be converted to date which i want to insert in calander start date , thats why i need to use this code . 
     
     plz correct me if i am repeating any steps in my code .like i am converting the date to string again and again  .
     
    "By the way, what's the point of converting the string to a date, then again to string and then to date once more? Why don't you do just one conversion, from string to date? You can declare tempDateTime at utcDateTime and then extract the date part by DataTimeUtil::date()."
  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    To pass string date value into the date value in form .
    Putting this business logic to a form control is a bad idea, because you couldn't call it from other places where you may need such a conversion. Use a class instead.
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,457 on at
    To pass string date value into the date value in form .
    hi 
    martin ,
    thanks for reply ,
     i need to convert the date value to and pass it to calander date value , 
    i have override these two methods , below is my code plz correct me where i am getting wrong ,
      [Control("String")]
        class DTPropertyLine_DTHijriDate
        {
            /// <summary>
            ///
            /// </summary>
            /// <returns></returns>
            public boolean modified()
            {
                boolean ret;
                DTHijriDate   hijriDateStr;
                ret = super();
                       
                DTPropertyLine.DTCalenderStartDate = this.hijri2GrDate(hijriDateStr);
        
                return ret;
            }
    
            /// <summary>
            ///
            /// </summary>
            //private void FormControlMethod1()
            //{
                private date hijri2GrDate(DTHijriDate   hijriDateStr)
                {
                    System.Globalization.CultureInfo arCul = new System.Globalization.CultureInfo("ar-SA");
                    System.Globalization.CultureInfo enCul = new System.Globalization.CultureInfo("en-US");
                    System.DateTime                  tempDateTime;
                    str                              strTemp;
                    System.String[]                  arr;
                    date                             grDate;
                    ;
     
                    //all expected dates formats
                    arr = new System.String[18]();
        
                    arr.SetValue("dd/MM/yyyy",  1);
                    arr.SetValue("dd-MM-yyyy",  2);
     
                    try
                    {
                        tempDateTime = System.DateTime::ParseExact(hijriDateStr, arr, arCul, System.Globalization.DateTimeStyles::AllowWhiteSpaces);
                    }
                    catch
                    {
                        error("Unexpected Hirji date format.");
                        return datenull();
                    }
                    strTemp = tempDateTime.ToString("dd/MM/yyyy");
                    grDate = str2date(strTemp, 123);
     
                    return grDate;
                }
            //}
        }
     

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans