Date question

is there a way to check if a string is a date, regardless of OS regional config, withouth using ParseDate?

Dates can have too many forms to do this easily. There is a huge difference in SQLDates (2016-12-30) to dates from a mail (1 Jul 2005 13:55:47 -0000"). How do you expect your check to work?

Right. If you are looking for a particular form of date, you could use a regular expression, but otherwise, if it doesn’t conform to the regional settings, you would have to fake it and could never be 100% certain, or even close if other languages are involved.

the user input in my app would be as follows


i want to check if the entered data is a valid date

I feel we already went through that.

Let us say you encounter 10/05/2016

How do you know that 10/5/2016 is October 5, or May 10 ?

Of course, if day > 12 then it is easy to spot day versus month. Otherwise, you will never know.

Given that, you can very well verify that the structure complies to certain criterias :

  • Only one pair of digits can > 12
  • Years can be within a valid period

Pass that, you can indeed infer a structure xx/xx/xxxx or xx-xx-xxxx or xxxx-xx-xx or xx/xx/xxxxx is a date.

If you are certain there will not be US style dates, then you can verify that the second pair of digits does not exceed 12, that no pair equals zero…

It is not terribly difficult to do.

OR… is it an invalid date input? Hmmmmmm?
All that can say it the format is “valid” … but valid for the US? or valid for certain non-US locales?
you cannot say it contains a “correct” date

[quote=306260:@Dave S]OR… is it an invalid date input? Hmmmmmm?
All that can say it the format is “valid” … but valid for the US? or valid for certain non-US locales?
you cannot say it contains a “correct” date[/quote]

[quote=306255:@Michel Bujardet]Pass that, you can indeed infer a structure xx/xx/xxxx or xx-xx-xxxx or xxxx-xx-xx or xx/xx/xxxxx is a date.

Never said “correct”.

i know previously if a number is a day or a month or a year because i use three text fields to enter data. based on that, i want to know if a string is a valid date. i.e user input 31/02/2016 OR 2016/31/02 OR 31-02-2016 OR 2016-31-02, would not be correct because february does not have 31 days. i want this type of validation regardless of OS regional config, withouth using ParseDate

What is you aversion to PARSEDATE?

if the user input a valid string “30/12/2016” and the system is configured as yyyy/mm/dd, even if it is a valid entry, ParseDate would throw error… i don’t want to be system dependent

System locale is the only way you can be sure that 03/12/2016 is March 12th and not December 3rd.
If ParseDate errors, tell the user they put the date in wrong.

I also remember that topic came for iOS, where ParseDate simply does not exist.

What you do is parse the input, and use the data to create a new date object.

Dim d As New Xojo.Core.Date(2015, 8, 1, Xojo.Core.TimeZone.Current)

If you create a with for instance February 31, 2015, the resulting date will be March 3, 2015. So when you check the entered date versus the created date, they won’t be the same.

In theory at least there should not be a reason. If the user enters a date on their machine, it is reasonable to assume you should be able to parse it and a failure would suggest an invalid date.

Having said that I can’t use it!!! I live in Switzerland, where things are different - we have four nation languages and the politics of choosing a company language can be difficult. I once worked in a company where if you send an email in German to a French speaking area they just behaved as if it never happened!!!

To avoid such issues many companies solve the problem by configuring all machines to use a neutral language - US English :wink: They then add local keyboard drivers and depending on the company they may or may not configure date, time, currency etc… But there is still a rub - French and Italian Swiss formats are not exactly the same as French and Italian formats… So are you dealing an Italian PC configured to the Swiss or Italian standard???

The result is that ParseDate is about as useful as a chocolate teapot to me. We normally provide user parameters that allow them to overwrite our default identification of the machines and of course internally everything is done in UTC.

in this link you can see the IsDate function in VB
—code from that link—
Dim firstDate, secondDate As Date
Dim timeOnly, dateAndTime, noDate As String
Dim dateCheck As Boolean
firstDate = CDate(“February 12, 1969”)
secondDate = #2/12/1969#
timeOnly = “3:45 PM”
dateAndTime = “March 15, 1981 10:22 AM”
noDate = “Hello”
dateCheck = IsDate(firstDate)
dateCheck = IsDate(secondDate)
dateCheck = IsDate(timeOnly)
dateCheck = IsDate(dateAndTime)
dateCheck = IsDate(noDate)

In the previous example, IsDate returns True on the first four calls and False on the last one

i think we need that powerful and simple function in xojo

Maybe we all can create an opensource function called IsDate that works the same as in vb?? (Although perhaps it is a very crazy idea)


Nicols, you may not notice, but we are trying to help here, and have understood a while ago what you are after.

I hate to break to you, but my previous post was precisely an attempt to show you how to the same thing.

Either go back to VB, or try to learn how to do things in Xojo. Like build your own isDate. It would be nice if instead of constantly crying after what VB does great, you simply reacted to others trying to help :confused:

@Michael Bujardet, i really appreciate your help, but i think you misunderstood what i am trying to achieve.
i want to have a peaceful conversation about xojo. i love xojo. and i want to contribute to the xojo community. i saw that questions about date are very common, so create the IsDate function for the community would be much appreciated for a lot of programmers, including me.
what you say about me crying about what VB does great, I think that’s not the case. Although a tool like xojo can be great, can have its weaknesses and accept constructive criticism is not bad, on the contrary, it forces us all to not accept everything as it comes. Rather, it pushes us to constantly improve. I avoid being conformist and also avoid being blind fan of any technology, since almost everything is perfectible and objectionable, especially in the field of programming. I’m not crying, just comparing tools without blind fanaticism

Use a different way to get an always valid date from the user. Say… display a calendar or three PopupMenus or…

Doing that will allow you to be sure the user given date is 100% correct.

If you get 3 text fields with, presumably, day, month & year then create a new date object with those values
Then read out the values that it ends up with and see if they match
If person enters mm/dd/yyyy (or however you have the text fields set up) like 31/02/2016 and you put that into a new Date you wont get back 31/02/2016

  Function IsValidDate( year as integer, month as integer, day as integer) as boolean
        dim tmp as New Date( year, month, day)

       return (tmp.Year = year) and (tmp.Month = month) and ( = day)
   end function

If what you want is to get that feature, then simply file a feature request, and hope for Xojo to create it one of these days.

What you don’t get is that I was, plain honest, trying to guide you on the path to create a validation method. Sorry, I was mistaken.

Edit : I see that Norman gave you the fish to eat.