Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / Is there a built-in fu...
Finance forum
Suggested answer

Is there a built-in function to check string is date or not ?

Posted on by 351
Hi guys,
 
I'm reading data from CSV, and part of the data is a date. But as usual, there will be some value error usually relate to user given wrong date format, or the value not even a date. So when I'm put in variable Transdate, we get error. Is there built-in function to check whether the value we read is a date or not ?
 
Or how is the best way to handle this ?
 
Thanks,
  • Martin Dráb Profile Picture
    Martin Dráb 224,528 Super User on at
    Is there a built-in function to check string is date or not ?
    In my opinion, trying to reimplement all the logic of System.Date::ParseExact() means spending a lot of time to get a worse result. Even such seemingly basic things like leap years are more complex than many developers realize. I wouldn't even think about it when we already have the solution available and tested for many years.
  • Suggested answer
    Anthony Blake Profile Picture
    Anthony Blake 932 on at
    Is there a built-in function to check string is date or not ?
    Have you considered using regex?
     
    It's horrible to read/write but there's load of examples on the web for date format validation, you could hide the ugliness away in a helper class.
     
     
  • Martin Dráb Profile Picture
    Martin Dráb 224,528 Super User on at
    Is there a built-in function to check string is date or not ?
    If you don't know the format, you can't meaningfully decide whether it's a date or not. Are you sure that your CSV file can contain something like 15. července 2023 and you want to accept it as a date?
     
    If you don't want to specify a format, you must assume a particular format (or a few formats) under the hood.
  • Teevo Profile Picture
    Teevo 351 on at
    Is there a built-in function to check string is date or not ?
    Hi Martin,
     
    But str2Date function means we already know the format, where is the position of the date, month, and year because we also need to specify sequence.
     
    I mean do we have function like IsDate() or something like that ? If I remembered correctly there is such function in PowerApps. It will return false if the parameter given is not a date.
     
    Thanks
  • Martin Dráb Profile Picture
    Martin Dráb 224,528 Super User on at
    Is there a built-in function to check string is date or not ?
    Sure, it's possible to do, but which function you should use depends on what exactly you need. Don't forget that there isn't a single format (e.g. 1\1\2012 or 15. července 2023 are also valid dates, although you'll likely want to reject them.
     
    For example, there is str2date() function or System.DateTime::ParseExact().

Helpful resources

Quick Links

Can you answer this forum question?

You could make someone's day!

Community Newsletter - May 2024

Kudos to our community stars!

Community Spotlight of the Month

Kudos to Mohamed Amine Mahmoudi!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 283,632 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 224,528 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,146

Featured topics

Product updates

Dynamics 365 release plans