how do I check for invalid date?

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
OzSanta
Posts: 34
Joined: Tue Apr 16, 2013 5:48 pm

how do I check for invalid date?

Post by OzSanta » Thu Apr 18, 2013 3:21 am

G'day

I'm trying to allow the user of my stack to set a date using 3 scroll buttons to enter DD/MM/YY (in Australian format)

Trouble is, while I'm limiting the DD field to 1 thru 31, the user can enter an invalid date eg 31/6/2013. When I try to convert this to the long date, it doesn't correct it to 1/7/2013, but shows the american short date in the form of 6/31/2013.

Is there any way of checking and correcting date input?

Regards

OzSanta

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: how do I check for invalid date?

Post by Simon » Thu Apr 18, 2013 3:52 am

Here I think this is silly but:

Code: Select all

on mouseUp
   convert "6/1/2013" to dateItems --MMDDYYYY
   put it into tDate
   add 31 to item 3 of tDate
   convert tDate to dateItems
   subtract 1 from item 3 of tDate
end mouseUp
You will have to capture the date in the US format but you can present it any way. Most of the numbers up there would be variables.
But do you really want the date to roll into the next month?

Once again... There must be a better way

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: how do I check for invalid date?

Post by Simon » Thu Apr 18, 2013 4:09 am

Ok answering my own statement:

Code: Select all

on mouseUp
   convert "2013,6,31,0,0,0,0" to dateItems
   put it into tDate
end mouseUp
This will convert to 2013,7,1,0,0,0,2
Just the year,month,day, with 4 0's

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

OzSanta
Posts: 34
Joined: Tue Apr 16, 2013 5:48 pm

Re: how do I check for invalid date?

Post by OzSanta » Thu Apr 18, 2013 4:21 am

G'day, & thanks

I just worked out a better way.

Regards

OzSanta

Code: Select all

on setStartTime
   put field"Start Date Month"&"/"&field "Start Date Day"&"/"&field "Start Date Year" into checkDate
   convert checkDate to dateitems
   repeat
      if  item 3 of checkdate = "" then 
         put field "Start Date Day" -1 into field "Start Date Day"
         put field"Start Date Month"&"/"&field "Start Date Day"&"/"&field "Start Date Year" into checkDate
         convert checkDate to dateitems
      else
         exit repeat
      end if
   end repeat
   convert checkDate to long system date
   put checkDate into field "Start Date"
end setStartTime

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: how do I check for invalid date?

Post by Simon » Thu Apr 18, 2013 4:33 am

If you did:

Code: Select all

on setStartTime
   put field"Start Date Month"&"comma"&field "Start Date Day"&"comma"&field "Start Date Year" &"0,0,0,0" into checkDate
   convert checkDate to dateitems
You wouldn't need the if/thens

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

OzSanta
Posts: 34
Joined: Tue Apr 16, 2013 5:48 pm

Re: how do I check for invalid date?

Post by OzSanta » Thu Apr 18, 2013 7:55 am

G'day Simon, and thank you.

At first I thought it would be necessary to convert 31/6/2013 to 1/7/2013, but my routine leaves the month alone, and reverses the 31 to 30 , or even 34 back to 30.

I've also worked out a way to convert the long date day if it's out of range, without altering the month.

Code: Select all

on checkTheDate thefield
   put field id thefield of this card into tempdate   
   convert tempdate to dateitems
   repeat
      if  item 3 of tempdate > 2000 then 
         put word 2 of item 2 of tempdate into temp2
         put temp2 -1 into temp3
         put replaceText  (tempdate,temp2,temp3) into tempdate 
         convert tempdate  to dateitems 
      else
         exit repeat
      end if
   end repeat
   convert tempdate to long date
   put tempdate into  field id thefield of this card
end checkTheDate

OzSanta
Posts: 34
Joined: Tue Apr 16, 2013 5:48 pm

Re: how do I check for invalid date?

Post by OzSanta » Thu Apr 18, 2013 10:22 am

I've realized my code was restricted to long dates, and long system dates did not work, so I've modified it. This will reduce any number of extraneous days for any month.

I'm finding LiveCode to be great fun to program, after only a few days.

The handler is called like so...

on returninField
checkTheDate id of me
end returninField

Regards

OzSanta

Code: Select all

on checkTheDate thefield
   put field id thefield of this card into tempdate   
   convert tempdate to dateitems
   put "long date" into keepTypeFlag 
   if number of items of tempdate = 2 then 
      put item 1 of tempdate &", "& word 2 of item 2 of tempdate&"  "& word 1 of item 2 of tempdate&", "&word 3 of item 2 of tempdate into theStoredSystemDate
       convert theStoredSystemDate  to dateitems
      put  theStoredSystemDate into tempdate
      put "long system date" into keepTypeFlag 
   end if
   repeat
      if   item 3 of tempdate > 2000 then 
         put item 2 of tempdate into tempTIME
         put word 2 of tempTIME into temp2
         put temp2 -1 into temp3
         put item 1 of tempdate & ", " & word 1 of item 2 of tempdate&" "&temp3&","&item 3 of tempdate into testdate
         convert testdate to dateitems
         put testdate into tempdate
      else
         exit repeat
      end if
   end repeat
   if keepTypeFlag = "long date" then 
      convert tempdate to long date
   else
      convert tempdate to long system date
   end if
   put tempdate into  field id thefield of this card
end checkTheDate

Post Reply