formatDate() Function Update
Phil Hudson
phil.hudson at iname.com
Wed Feb 28 13:54:09 EST 2024
Very useful function, thanks for sharing.
I believe the format you call "SQL format" is literally *the* standard
format, as in ISO 8601.
https://www.startpage.com/do/dsearch?query=iso+date+format&cat=web&pl=opensearch&language=english
I'd suggest "North American format" for the other one, assuming they
use it in Mexico too. Otherwise I'm at a loss.
On Wed, 28 Feb 2024 at 18:44, Bob Sneidar via use-livecode
<use-livecode at lists.runrev.com> wrote:
>
> Not to be confused with the built-in dateFormat() function, I wrote a special formatDate() function a few years back because I had a need to get a given date in unsupported formats. Particularly I needed an SQL Date format in the form of yyyy-mm-dd.
>
> I have been frustrated also that there is no built-in function to return a date in the mm/dd/yyyy format, so I just updated my function to include that form using “standard date” as the format parameter. (I can’t think what else to call it. "Y2K Date” seems cheesy.)
>
> Here is the code if anyone is interested. I should also submit it to the Master Library. I will have to grok how to do that again.
>
> Bob S
>
>
>
> FUNCTION formatDate theDate, theFormat
> /*
> Accepts any valid date for the first parameter. If not a valid date, it simply returns
> what was passed. Second parameter can be any of the following:
> sql date: date in the yyyy-mm-dd format
> short date, abbreviated date, internet date, long date: LC versions of the same
> julian date: Julian number based on (I believe) Jacques formula
> standard date: The date in the form of theFormat
> */
>
> put theDate into tSavedDate
> put the itemdelimiter into theOldDelim
> set the itemdelimiter to "-"
>
> IF the length of item 1 of theDate = 4 AND \
> the number of items of theDate = 3 AND \
> item 1 of theDate is a number AND \
> item 2 of theDate is a number AND \
> item 3 of theDate is a number THEN
> put item 2 of theDate & "/" & \
> item 3 of theDate & "/" & \
> item 1 of theDate into theDate
> END IF
>
> -- replace "." with "/" in theDate
> convert theDate to dateitems
> set the itemdelimiter to theOldDelim
>
> if the number of items of theDate <> 7 then
> answer "'" & theDate & "' is not a valid date format!"
> return tSavedDate
> end if
>
> SWITCH theFormat
> CASE "sql date"
> /*
> put item 1 of theDate & "-" & \
> format("%02d",item 2 of theDate) & "-" & \
> format("%02d",item 3 of theDate) into theDate
> */
> put format("%s-%02d-%02d", item 1 of theDate, item 2 of theDate, \
> item 3 of theDate) into theDate
> break
> CASE "short date"
> convert theDate from dateitems to short date
> break
> CASE "abbreviated date"
> convert theDate from dateitems to abbreviated date
> break
> CASE "abbr date"
> convert theDate from dateitems to abbreviated date
> break
> CASE "internet date"
> convert theDate from dateitems to internet date
> break
> CASE "long date"
> convert theDate from dateitems to long date
> break
> CASE "julian date"
> put the date into theDate
> convert theDate to dateItems
> IF ((item 2 of theDate = 1) OR (item 2 of theDate = 2)) THEN
> put 1 into theDay
> ELSE
> put 0 into theDay
> END IF
> put item 1 of theDate + 4800 - theDay into theYear
> put item 2 of theDate + (12 * theDay) - 3 into theMonth
> put item 3 of theDate + \
> ((153 * theMonth + 2) div 5) + \
> (365 * theYear) + \
> (theYear div 4) - \
> (theYear div 100) + \
> (theYear div 400) - \
> 32045 into theDate
> break
> case "standard date"
> put format("%02d/%02d/%04d", item 2 of theDate, item 3 of theDate, \
> item 1 of theDate) into theDate
> break
> default
> Answer info “‘“ & theFormat & “‘ is not a validate parameter.” As sheet
> put tSavedDate into theDate
> END SWITCH
>
> return theDate
> END formatDate
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
More information about the use-livecode
mailing list