Problems with Date and Convert

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Problems with Date and Convert

Post by Traxgeek » Sat Jan 19, 2013 1:36 pm

Hi,

There are dozens of examples of 'How to' do lots of things with the 'Date' and 'Convert' functions but no matter how hard I try I fail to make the following idea work :
I need a date entry to be in the following format "DD/MM/YYYY" so that I can verify its range etc and continue with it in my script...
I thought I'd use the 'Convert [myDateVariable] to [short system date]' to ensure that, at least, the format of the entry was correct before I started checking day(DD), month(MM) and year(YYYY) components for validity...
So, I thought...

Code: Select all

local dMyDate, dToday
put "19/01/13" into dMyDate                  --dMyDate now contains "19/01/13"
put the short system date into dToday     --dToday now contains "19/01/2012"
convert dMyDate to short system date    --dMyDate still contains "19/01/13"  WHEREAS I THOUGHT IT WOULD CONTAIN "19/01/2013"
                                                            --i.e. I THOUGHT the Year format would have been modified from my entered YY to YYYY 
I have tried using 'it' in case the result of my 'convert' was not placed back into my originating variable (dMyDate), but that doesn't work... In fact I've 'played' about twith this last night and, now, all morning and still come up blank... time to ask for some kicks in the right direction please !


In fact, to add to my confusion, I often seem to end up (after the 'Convert') with the first two fileds (DD / MM) inverted by the 'Convert'... :? What ?

I'm failing to make the 'convert' function as I thought it would... What am I doing wrong please ?

TIA

Regards.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Problems with Date and Convert

Post by Klaus » Sat Jan 19, 2013 2:44 pm

Hi Traxgeek,

where is the problem?
The docs say that "short date" always returns MM/DD/YY, "short system date", too, in english speaking regions!
I get "19.01.13" for "short system date" here in germany.

You can always use (word -1 of the long date) to get the 4 digit year 8)


Best

Klaus

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: Problems with Date and Convert

Post by Traxgeek » Sat Jan 19, 2013 3:58 pm

Hi Klaus,

I'm confused (not a rare occurrence these days !)

Try this :

Code: Select all

put the text of field "txtMyDate" into dDate --where txtMyDate = "02/11/04" - i.e. two digit Year field
convert dDate to short system date
Answer dDate                                             -- returned : "02/11/2004" - i.e. 4 digit year field
now try this

Code: Select all

put the text of field "txtMyDate" into dDate --where txtMyDate = "02/11/2004" - i.e. 4 digit year field
convert dDate to short system date
Answer dDate                                             -- returned : "02/11/04" - i.e. 2 digit year field
Why is this ? My tiny brain is struggling with this one !

For clarity : all I'm trying to do is standardise a text / field input into a specific format - DD/MM/YY or DD/MM/YYYY. At this stage I don't care if the user has inverted the days / months to give me an invalid entry all I'm trying to do is standardise whatever they've input to I can begin checking each 'field' (delimited by the slash) for validity...

Other times I try similar things and I get the DD / MM fields inversed after calling 'Convert'.
This, to me, is a little like the DataGrid issue I have - I'm missing something VERY simple but I don't know what it is...

Thanks

Regards.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: Problems with Date and Convert

Post by Traxgeek » Sat Jan 19, 2013 4:22 pm

Further to my last couple of posts...

It seems to me (I probably missed this in the documentation) that if I should enter an invalid day/month combination - i.e. I invert the DD/MM for MM/DD or make a trypo with the month/MM such that it's invalid (>12) then LC, in its helpfulness, trys to sort this out for me and switches/inverst/swaps the DD/MM BUT ONLY if the DD value can be considered a valid MM value (a number > 0 and < 13)...

Which leads me onto a more simple question / probably better than my first and certainly the reson for my post in the first instance :

If I want to validate a date entry typed in by a user, what would be the best way, considering that I intend to use the 'convert...dateparts' statement on said date entry to do some other funky checks / calculations... ?

TIA

Regards.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Problems with Date and Convert

Post by Klaus » Sat Jan 19, 2013 4:32 pm

Hi traxgeek,

hmmmm, I get "11.02.04" in both cases with "short system date" and "2/11/04" in both cases with "short date",
which is always in english format MM/DD/YY.

But you as the developer need to check the entereded data, which is a bit tedious sometimes 8)
Create a function like this:

Code: Select all

function checkMyDate tDate
  set itemdel to "/"
  
  ## 1. check for complete date:
  if the num of items of tDate <> 3 then
    beep
    answer "Incomplete data!"
    exit to top
  end if

   ## 2. check for number of digits of year:
  if the num of chars of item 3 of tDate = 2 then
    ## add year etc...
  end if
  ## Etc. check for all ITEMS separately if neccessary and modify tDate to your needs...
  ## you get the picture :-)
  return tDate
end checkMyDate
Best

Klaus

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: Problems with Date and Convert

Post by Traxgeek » Sat Jan 19, 2013 4:38 pm

Hi Klaus, thanks for that.

I had incorrectly assumed that the 'is a date' function would check the format and (even) validity for me !. I'm wrong !

I have no issues writing my own date / time checker but had wished to avoid re-inventing someone else's wheel !! Not wishing to be rude or critical, I had simply assumed that this was such a basic requirement it must surely exist... No worries - I'll create one.

One further question and I shut up - at least until tomorrow - well, I'll try...

What does the 'is a date' function actually check please ? What are the parameters for a 'true / false' result ?

Sorry to be a pain and thanks again.

Regards.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Problems with Date and Convert

Post by Klaus » Sat Jan 19, 2013 5:19 pm

Hi traxgeek,

yep, unfortunately "the date" is not reliable, "put (1 is a date)" will give TRUE, as all integers will! :D

I do not know what exactly is going on behind the curtain, when the engine validates a date,
but maybe it will also interpretate the "UNIX" seconds as a valid date, too?

And yes, using a custom function will do the trick.
And a combination of "the date", "dateitems", "seconds" etc... with "convert" will surely help.

You could make the function even THAT clever that it will check for date delimiters first,
I mean to allow the user to enter all possible forms of a date:
11-1-01
11.11.2006
12/12/03
12,12,01
etc...
8)


Best

Klaus

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: Problems with Date and Convert

Post by Traxgeek » Sat Jan 19, 2013 5:52 pm

Hi Klaus,

Thanks and... already done :D

The function checks for your 'chosen' delimiter, uses that to verify all 3 fields of the entry, checks whether the year is 2 or 4 digits and then recombines the lot with the 'normal' slash ('/') character as the 'correct' delimiter before returning a valid, formatted valid DD/MM/YYYY date or an empty date for decision making (true/false) in the calling script...

I could post it up if anyone's interested but, to be frank, it's very simple 80 lines of code heavily remarked.

Klaus. I couldn't have gotten this far (however 'far' that is !) without your continued help - AND it's a Saturday evening!! Wihout meaning to be 'gushy' - THANKS a million. There's very little more frustrating than having to wait 48hours (Sat -> Mon) to get an answer to a question that you're already frustrated over and have spent quite enough time on...

Thanks again.

Have a great weekend.

Regards.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Problems with Date and Convert

Post by Klaus » Sat Jan 19, 2013 6:46 pm

Hi Traxgeek,

I learned Livecode (actually it was Metacard in those days in 1999) without internet access (sic!),
so I could not ask anyone and had to learn it "the hard way" with trial and error. That's one of the
reasons why I am quite good at this today :D

So I can feel your frustration about the high learning curve of Livecode and try to help whenever I can.
Don't thank me, thank my Mom and her good education 8)


Best

Klaus

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Problems with Date and Convert

Post by jacque » Sat Jan 19, 2013 8:37 pm

The reason any integer is considered a date is because the seconds and the milliseconds return integers. Both seconds and milliseconds can be used with the convert command to produce a date.

The system date should produce the same format specified in the OS control panel. That's what I see here. Both your tests return "2/11/04" for me, which is how my Mac is set to display short dates. Have you set the numberformat anywhere in your script?

"Is a date" will validate any integer, or sequence of integers separated by a consistent delimiter, or any string that consists of the same formats that the convert, time, or date functions can produce.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply