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

Community site session details

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

How to validate Date

(0) ShareShare
ReportReport
Posted on by

Hi all,

Wondering how to validate a date through coding. The case is I have 3 fields : Days, Month, and Year, when  it put together this will be date, but of course it needs to make sense, especially for Leap year. For example 29 February 2021 is not make sense.

For the normal month I can make an If condition saying if it is April or June, and the date is > 30 it is incorrect, but Leap year, is there any special function or I should create further validation just to acknowledge it is Leap Year and 29 February is wrong.

I tried a simple way, by using MKDATE function combine with Try and Catch, but unfortunately this function does not give error if the calendar date is wrong, instead it is automatically correct it.

The code is like this :

try 
      {
            
            TransDate   checkDate = mkDate(enum2int(this.Days), enum2int(this.Months), (this.Years));
      }
catch 
      {
             ret = checkFailed("Wrong date");
      }

When I choose Days = 29, Month = FEB, Year = 2021, it didn't falls to error catch instead in debug I saw the checkDate became 28/2/2021.

It is great function, however.

Thanks

I have the same question (0)
  • Suggested answer
    Sukrut Parab Profile Picture
    71,699 Moderator on at
    RE: How to validate Date

     Take a look at example from the link below to find out leap year

    axraja.blogspot.com/.../find-leap-year-or-not.html

  • Suggested answer
    Martin Dráb Profile Picture
    236,503 Most Valuable Professional on at
    RE: How to validate Date

    It seems to be an artificial problem caused by the fact that you ignore the standard way how dates are handled in F&O (using date and utcDateTime data types and corresponding form controls). I think you should reconsider your approach and let you form behave the same way as users are used to from all other forms in the system.

    If you insist on making unnecessary problems for you and your users, you can validate "Days" field by System.DateTime::DaysInMonth(), for example.

    The check for the leap date (mentioned by Sukrut) is wrong; don't use it.

  • Verified answer
    udaY-ch Profile Picture
    5,106 on at
    RE: How to validate Date

    Hi,

    There is a standard function for finding a leap year.

    System.DateTime::IsLeapYear(year(Yourdate);

    Uday

  • Community Member Profile Picture
    on at
    RE: How to validate Date

    Hi all,

    Thank you very much for the advises.

    Yes, basically it all going back to business needs, and I'm requested to create this kind of table setting :

    pastedimage1597729082890v1.png

    For the Days, I already using the standard Enum DaysOfMonth,but the combination after it, is the one I need to validate further (if they didn't choose 'ALL')

    I think should be ok now, I'm using the IsLeapYear, to check special for month February.

    Thanks again.

  • Martin Dráb Profile Picture
    236,503 Most Valuable Professional on at
    RE: How to validate Date

    Have you considered using the standard recurrence functionality?

    recurrence.PNG

    It also give you extra options such as "last Thursday each month".

  • Verified answer
    Martin Dráb Profile Picture
    236,503 Most Valuable Professional on at
    RE: How to validate Date

    I wonder why you're using IsLeapYear(). I didn't mention it, because it's not really what you need to know. You want to check if the date is valid for the given month and year and you can easily do it with DaysInMonth(), as I suggested.

    IsLeapYear() will give you just one little piece of information needed for a single month and you would have to replicate all the logic that DaysInMonth() already does for you. Spending your time to develop, test and maintain code that isn't needed at all doesn't sound wise to me.

  • Community Member Profile Picture
    on at
    RE: How to validate Date

    Hi Martin,

    Yes, DaysInMonth looks good to me. Wish I know it sooner.

    Thanks

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 1,882

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 794 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 525 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans