Jump to content
Lainkes

check if string date

Recommended Posts

Hello,

 

I have a TEdit component.

Now I need to check if the value is a date. (Date is not from user input, so I cannot use a dateTimePicker)

I tried to use the TryStrToDate function but then I have an error.

Error : [dcc32 Error] frm_xxx.pas(691): E2250 There is no overloaded version of 'TryStrToDate' that can be called with these arguments.

 

Is there a better solution to achieve this?

 

Thanks

 

Lainkes

Share this post


Link to post

TryStrToDate is the way to go. The error you mention tells you that you have wrong argument type. As we don't know how you called the function, we cannot help you more.

Always show the code you are using !!

Share this post


Link to post
22 minutes ago, Lainkes said:

There is no overloaded version of 'TryStrToDate' that can be called with these arguments.

Let me guess. Your code looks something like this:

if TryStrToDate(Edit1, theDate) then

But it should be

if TryStrToDate(Edit1.Text, theDate) then

 

  • Like 1

Share this post


Link to post
2 minutes ago, Uwe Raabe said:

Let me guess. Your code looks something like this: 


if TryStrToDate(Edit1, theDate) then

But it should be


if TryStrToDate(Edit1.Text, theDate) then

 

I had exactly the same guess.  :classic_cool:

Share this post


Link to post

I wonder why you think you can't use a TDateTimePicker? Have you tried? Whence is the date if not from the user? Because anything else should already be under your total control.

  • Like 1

Share this post


Link to post

This is my code.

So I guess I miss an argument in my statement.

 

if TryStrToDate(edtFrom.Text) then

if TryStrToDate(edtFrom.Text) then

 

Share this post


Link to post
21 minutes ago, Lainkes said:

So I guess I miss an argument in my statement.

Yes, the variable where the date is going to be stored.

Share this post


Link to post

The second argument is missing...

should be like:

 

if TryStrToDate(edtFrom.Text, DateVariable) then

where DateVariable has a type TDateTime.

Share this post


Link to post

You can even do better to forgive some user common error:

 

if TryStrToDate(Trim(edtFrom.Text), DateVariable) then

Share this post


Link to post

Sorry, but @Lainkes clearly stated, the data is not entered by the user. So no focus on fixing user errors needed here. Remains the unanswered question, about the origin of the date. How did this possibly erroneous value get into the Edit in the first place? Is it from a database? Or maybe some text file? Retrieved from a web site? All of which have appropriate methods to ensure correct data types and values. Fixing the issue on the GUI is way to late.

Share this post


Link to post
1 hour ago, Fr0sT.Brutal said:

Don't forget locales.

'10/22/2022' => OK in USA, invalid in EU

Welcome to date format hell. Once you go that way, you will find out that there are many more different date formats than you ever imagined and that many of them are incompatible to each other.

 

My favourite example:

US: MM/DD/YY

UK: DD/MM/YY

 

If you have the choice always go with an international standard: ISO 8601

  • Like 1

Share this post


Link to post
8 minutes ago, dummzeuch said:

My favourite example:

US: MM/DD/YY

UK: DD/MM/YY

And it's fun to have '01/02/03' 🙂

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×