Page 1 of 1

Date Formatting

Posted: Thu Jun 25, 2020 1:46 am
by wlaughto
Is there any function to convert a month in text format to a month number? I have a date stored in the format - "DD MMMMMMMM YYYY" e.g. "22 June 2020" and need to convert to a system date. The days and years are fine, however not sure if there is a function to convert the month.

Re: Date Formatting

Posted: Thu Jun 25, 2020 2:28 am
by dunbarx
Hi.

The full name of the month will to throw LiveCode for a loop the way you have it formatted. Any format is fine, but we need to make it into one that LC understands. Check out "dateItems" in the dictionary, as well as "date".

In a button script:

Code: Select all

on mouseUp
   get "22 January 2020"
   put getMonth(word 2 of it) & "/" & word 1 of it & "/" & word 3 of it into normalFormat
   convert normalFormat to long date --just because
   answer normalFormat
end mouseUp

function getMonth tMonth
   switch tMonth
      case "January"
         return "1"
         break
      case "February"
         return "2"
         break
      case "You get the picture"
       --...
   end switch
end getMonth
There are a million ways to do this. You need write this function only once, and use it wherever. If you want to save space, you can preset a custom property with the associations of name and number.

The important thing is that when you asked "is there a way...", well, of course there is. Many. The above is long because we have to substitute the long month names explicitly. You might have had a different (peculiar) format that lent itself to more compact gadgetry.

Craig

Re: Date Formatting

Posted: Thu Jun 25, 2020 5:32 am
by wlaughto
Thx Craig, this is exactly what I thought I would need to do. Was just hoping there was a function that I could not find that would do the job 😏

Re: Date Formatting

Posted: Thu Jun 25, 2020 8:47 am
by AxWald
Hi,

you may want to search the dictionary for "month". You'll find "function monthNames".

Code: Select all

   put line 2 of the monthNames
   --> "February"
Have fun!

Re: Date Formatting

Posted: Thu Jun 25, 2020 1:45 pm
by dunbarx
Was just hoping there was a function that I could not find that would do the job
Well, can you think of a way to write a universal function that would parse all possible formats, however scrambled they are in presenting the various parts of a date or time? And reformat more "standardly"? Check out the various "offset" functions in the dictionary. These can be used to extract strings from a body of text.

But how to distinguish a "3", that might indicate a month, from a "5" that might indicate a day? And those two values placed anywhere in a string. A "universal" function may not be possible. Probably why LC does not have one. :wink:

Craig

Re: Date Formatting

Posted: Thu Jun 25, 2020 5:22 pm
by jacque
AxWald wrote:
Thu Jun 25, 2020 8:47 am
you may want to search the dictionary for "month". You'll find "function monthNames".

Code: Select all

   put line 2 of the monthNames
   --> "February"
Or:

Code: Select all

put lineOffset(tMonth, the monthNames) into tMonth

Re: Date Formatting

Posted: Thu Jun 25, 2020 5:46 pm
by richmond62
Garden spade time:
-
Screenshot 2020-06-25 at 19.47.50.png
-

Code: Select all

on mouseUp
   ask "What month is it? "
   if it is not empty then
   put it into MESETS
   put 1 into LYNE
   repeat until line LYNE of fld "MONAT" is empty
      if line LYNE of fld MONAT contains MESETS then
         put MESETS && "is month number" && LYNE into fld "fM"
      end if
      add 1 to LYNE
   end repeat
   end if
end mouseUp

Re: Date Formatting

Posted: Sat Jun 27, 2020 12:03 pm
by wlaughto
jacque wrote:
Thu Jun 25, 2020 5:22 pm
AxWald wrote:
Thu Jun 25, 2020 8:47 am
you may want to search the dictionary for "month". You'll find "function monthNames".

Code: Select all

   put line 2 of the monthNames
   --> "February"
Or:

Code: Select all

put lineOffset(tMonth, the monthNames) into tMonth
Perfect, thx.