Date Formatting

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
wlaughto
Posts: 19
Joined: Fri Apr 07, 2017 5:06 am

Date Formatting

Post by wlaughto » Thu Jun 25, 2020 1:46 am

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6797
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Date Formatting

Post by dunbarx » Thu Jun 25, 2020 2:28 am

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
Last edited by dunbarx on Thu Jun 25, 2020 1:38 pm, edited 1 time in total.

wlaughto
Posts: 19
Joined: Fri Apr 07, 2017 5:06 am

Re: Date Formatting

Post by wlaughto » Thu Jun 25, 2020 5:32 am

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 😏

AxWald
Posts: 435
Joined: Thu Mar 06, 2014 2:57 pm

Re: Date Formatting

Post by AxWald » Thu Jun 25, 2020 8:47 am

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!
Livecode programming until the cat hits the fan ...

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6797
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Date Formatting

Post by dunbarx » Thu Jun 25, 2020 1:45 pm

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5466
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Date Formatting

Post by jacque » 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
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4690
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Date Formatting

Post by richmond62 » Thu Jun 25, 2020 5:46 pm

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
Attachments
Month Numberer.livecode.zip
Here's the stack.
(1.22 KiB) Downloaded 8 times

wlaughto
Posts: 19
Joined: Fri Apr 07, 2017 5:06 am

Re: Date Formatting

Post by wlaughto » Sat Jun 27, 2020 12:03 pm

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.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”