function to get MM-DD-YY

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

wee wullie
Posts: 78
Joined: Fri Oct 03, 2008 10:13 am

function to get MM-DD-YY

Post by wee wullie » Wed Sep 04, 2013 1:39 pm

Does anyone know of a function to change a date to MM/DD/YY from DD/MM/YYYY ?
The reason is that i am having problems converting date formats i.e. the date format changes to either DD-MM-YY or it just stays as DD/MM/YYYY , not sure why this happens.

wullie

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: function to get MM-DD-YY

Post by dunbarx » Wed Sep 04, 2013 3:03 pm

Hi.

Check out the "dateItems" keyword. You can do this.

Craig Newman

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

Re: function to get MM-DD-YY

Post by Klaus » Wed Sep 04, 2013 4:04 pm

Hi wullie,

no built-in function, you need to roll your own, "itemdelimiter" is your friend! :D

Code: Select all

function date_from_DDMMYYYY_2_MMDDYY tOldDate

  # tOlDate = DD/MM/YYYY
  set itemdel to "/"

  ## create string: MM/DD/YY
  return item 2 of tOldDate & "/" & item 1 of tOldDate & "/" & char 3 to 4 of item 3 of tOldDate
end date_from_DDMMYYYY_2_MMDDYY
Best

Klaus

wee wullie
Posts: 78
Joined: Fri Oct 03, 2008 10:13 am

Re: function to get MM-DD-YY

Post by wee wullie » Thu Sep 05, 2013 2:40 am

Thanks for the Function Klaus, how do i use it?, not sure where it goes :?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: function to get MM-DD-YY

Post by dunbarx » Thu Sep 05, 2013 4:44 am

Hi.

OK.

Between my post and Klaus's you should have a way forward.

The "dateItems" provides an unchanging format for dates and times, regardless of local settings. The dictionary explains this. Klaus mentioned the "itemDelimiter" as a way to parse data in a convenient way.

A method that uses both suggestions: If you have a date in any format, convert it to dateItems ("convert" is a command, see the dictionary again). In a button script:

Code: Select all

on mouseUp
   get the date
   convert it to dateItems
   answer it
end mouseUp
You now have your date in the format: Year,Month,Day,H,M,S,day of the week. Using the itemdelimiter (default is a comma) as a tool to reconstruct this data, can you manage? If not write back, but do try. What happens when you run the above script, but need the day in "DD" format? Or the year in "YY" format?

Are these concepts clear? If not, you must practice a bit. I would like you to write back only when you are stuck, not just to ask for the answers. It will be more that worth it if you do.

Klaus actually took your posted format and simply rearranged the items, using the itemdelimiter again, though set to "/" since that was a convenient way to take your posted format apart and rearrange it. This will work if you start off with explicit formatted data. But is that all there might be to a robust solution?

Craig Newman

wee wullie
Posts: 78
Joined: Fri Oct 03, 2008 10:13 am

Re: function to get MM-DD-YY

Post by wee wullie » Thu Sep 05, 2013 10:43 am

Hi Dunbarx, i'm not too familiar with Functions which is why i was inquiring as to where i would use it, anyway i dissected Klaus' Function and i used dateItems as you suggested and it works (*kind of) using the following code:

Code: Select all

   on mouseUp
   put text of fld "Date1" into tDate
   convert tDate to dateItems
   answer tDate  
   put item 3 of tDate & "/" & item 2 of tDate & "/" & char 3 to 4 of item 1 of tDate into tAns
   put tAns into fld "Date1"
   end mouseUp


*(kind of) works means that if i pick a date up to the 12th of any month it works fine but from the 13th on-wards i get "///0 " as the date, i used an answer dialog and strangely it answers with the dateitems up to the 12th and with the date format: 24/09/2013 from the 13th on-wards and by the way i'm using a date picker to input the dates, the picker in question is the one on revonline from Marty that BVG updated, the dates that appear in the picker field are in the format: 24/09/2013 regardless of which date range used and in any case, i tested it with a regular field and got the same result so it is definitely not the picker as i use it in a number of stacks without incident, Very strange behavior indeed!!!

kind regards
Wullie

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

Re: function to get MM-DD-YY

Post by Klaus » Thu Sep 05, 2013 11:50 am

Your script works fine for me with day 1 to day 30 of this month!
Make sure that in fld "Date1" is a really valid date!

wee wullie
Posts: 78
Joined: Fri Oct 03, 2008 10:13 am

Re: function to get MM-DD-YY

Post by wee wullie » Thu Sep 05, 2013 12:21 pm

Hi Klaus, it appears as a valid date in the field but as i mentioned, it does not convert to the date or to dateitems if the date is more than the 12th so i think i will try using an if statement to let it convert up to the 12th then i will put it in a variable and re-arrange the format and removing the first two chars of the year, do you think that'll work?



Wullie

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

Re: function to get MM-DD-YY

Post by Klaus » Thu Sep 05, 2013 12:43 pm

There is no logical reason, why this should stop working with day >12! 8)
As I said, this works for the complete current month here from 9/1/13 to 9/30/13!

How do you enter the date into the field "Date1" that you are going to convert?
I guess the format is not what Livecode exspects to convert to dateitems...

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: function to get MM-DD-YY

Post by bn » Thu Sep 05, 2013 12:59 pm

Hi wee,

to use what Klaus did in his function and put it into a mouseUp handler here is what works for me.

Code: Select all

on mouseUp
   put text of fld "Date1" into tDate
   set the itemDelimiter to "/" -- this lets you treat a slash as itemDelimiter
   
   put item 2 of tDate into item 1 of tDestFormat -- items are automatically formed with the current itemDelimiter
   put item 1 of tDate into item 2 of tDestFormat
   put item 3 of tDate into item 3 of tDestFormat
   
   set the itemDelimiter to comma -- be nice and reset the itemDelimiter, would be reset at end of handler anyways though
   
   answer tDestFormat
   
end mouseUp
Kind regards
Bernd

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: function to get MM-DD-YY

Post by dunbarx » Thu Sep 05, 2013 2:56 pm

Try this in a button script:

Code: Select all

on mouseUp
   put getDayOfWeek(the date) into tDay
   answer "Today is:" &&  item tDay of "Sun,Mon,Tue,Wed,Thu,Fri,Sat"
end mouseUp

function getDayOfWeek var
   convert var to dateItems
   return item 7 of var
end getDayOfWeek
Set a breakpoint at the "on mouseUp" line, and "step into" each line following. Functions are control structures that return a value, whereas "on" control structures run a process. The replies you have now are all "on" types. The two are sometimes interchangeable, but you will find as you gain experience that functions are priceless tools, especially when they might be called from many different locations.

Can you write a function using the hints you have, that will do your date thing?

Craig

wee wullie
Posts: 78
Joined: Fri Oct 03, 2008 10:13 am

Re: function to get MM-DD-YY

Post by wee wullie » Fri Sep 06, 2013 12:13 am

Klaus wrote:There is no logical reason, why this should stop working with day >12! 8)
As I said, this works for the complete current month here from 9/1/13 to 9/30/13!

How do you enter the date into the field "Date1" that you are going to convert?
I guess the format is not what Livecode exspects to convert to dateitems...
Yes Klaus, i agree There is no logical reason, why this should stop working with day >12, i doesn't make much sense to me either, the date is entered via a date picker, however i made new mainstack with regular fields, i inputted the date manually and the same thing happened upon conversion, its weird.

wee wullie
Posts: 78
Joined: Fri Oct 03, 2008 10:13 am

Re: function to get MM-DD-YY

Post by wee wullie » Fri Sep 06, 2013 12:23 am

bn wrote:Hi wee,

to use what Klaus did in his function and put it into a mouseUp handler here is what works for me.

Code: Select all

on mouseUp
   put text of fld "Date1" into tDate
   set the itemDelimiter to "/" -- this lets you treat a slash as itemDelimiter
   
   put item 2 of tDate into item 1 of tDestFormat -- items are automatically formed with the current itemDelimiter
   put item 1 of tDate into item 2 of tDestFormat
   put item 3 of tDate into item 3 of tDestFormat
   
   set the itemDelimiter to comma -- be nice and reset the itemDelimiter, would be reset at end of handler anyways though
   
   answer tDestFormat
   
end mouseUp
Kind regards
Bernd
Hi Bernd, your code gives me MM/DD/YYYY rather than a 2 digit year.

wee wullie
Posts: 78
Joined: Fri Oct 03, 2008 10:13 am

Re: function to get MM-DD-YY

Post by wee wullie » Fri Sep 06, 2013 12:30 am

dunbarx wrote:Try this in a button script:

Code: Select all

on mouseUp
   put getDayOfWeek(the date) into tDay
   answer "Today is:" &&  item tDay of "Sun,Mon,Tue,Wed,Thu,Fri,Sat"
end mouseUp

function getDayOfWeek var
   convert var to dateItems
   return item 7 of var
end getDayOfWeek
Set a breakpoint at the "on mouseUp" line, and "step into" each line following. Functions are control structures that return a value, whereas "on" control structures run a process. The replies you have now are all "on" types. The two are sometimes interchangeable, but you will find as you gain experience that functions are priceless tools, especially when they might be called from many different locations.

Can you write a function using the hints you have, that will do your date thing?

Craig
Hi Craig, i understand functions a lot better now thanks to your example code, much appreciated.

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

Re: function to get MM-DD-YY

Post by Simon » Fri Sep 06, 2013 12:31 am

Hi Wullie,
Could you specify which date picker you are using?
I couldn't locate it.
or maybe just zip it up and post it here.

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

Post Reply