Convert date
Bob Sneidar
bobsneidar at iotecdigital.com
Fri Jul 14 11:34:24 EDT 2023
Hmmm… I read the enhancement request. I’m still in the dark though on how to get to "2023-07-14 08:30:00" from “7/14/23” using format strings. Here’s my solution for those who do not want to download the masterLibrary. Given these, what else do you need?
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
*/
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
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
END SWITCH
return theDate
END formatDate
FUNCTION formatTime theTime, theFormat
/*
accepts any valid time and returns the form of the time specified in the second parameter.
The valid formats are:
sql time: hh:mm:ss (Note: combining sql date from the formatDate() function with the
sql time will produce a valid SQL date time type).
short time: LC short time format
abbreviated time: LC abbr time format (same as short time)
long time: LC long time format
seconds: the number of seconds since the prior midnight
military: the military time 00:00 - 23:59
*/
IF theTime is empty THEN return empty
set the numberformat to "00"
SWITCH theFormat
CASE "sql time"
convert theTime to dateitems
put (item 4 of theTime +0) & ":" & \
(item 5 of theTime +0) & ":" & \
(item 6 of theTime +0) into theTime
break
CASE "short time"
convert theTime to short time
break
CASE "abbreviated time"
convert theTime to abbreviated time
break
CASE "long time"
convert theTime to long time
break
CASE "seconds"
convert theTime to seconds
break
CASE "military"
set the itemdelimiter to ":"
IF theTime contains "PM" THEN
add 12 to item 1 of theTime
END IF
put word 1 of item 2 of theTime into item 2 of theTime
break
END SWITCH
return theTime
END formatTime
And as an added bonus, those who use Spiceworks know they format time for tickets as 0h0m0s.
FUNCTION spiceTime pStartTime, pEndTime
convert pStartTime to dateItems
convert pEndTime to dateItems
subtract item 4 of pStartTime from item 4 of pEndTime
subtract item 5 of pStartTime from item 5 of pEndTime
convert pEndTime from dateItems to short time
put formatTime(pEndTime, "military") into pEndTime
set the itemdelimiter to ":"
IF item 1 of pEndTime > 11 THEN subtract 12 from item 1 of pEndTime
put item 1 of pEndTime & "h" & item 2 of pEndTime & "m" into tTotalTime
return tTotalTime
END spiceTime
Bob S
> On Jul 13, 2023, at 4:19 PM, ambassador--- via use-livecode <use-livecode at lists.runrev.com> wrote:
>
> Neville Smythe wrote:
>> I seem to have hallucinated that the built-in convert handler recognised
>> the ISO date and dateTime formats (YYYY-MM-DD, YYYY-MM-DD
>> hh:mm:ss+-http://hh.ss, etc) but I must have written my own conversion
>> routines in a former life.
>> But one would have to ask… Why doesn’t it?
>> After all, the original ISO 8601 standard was adopted 1988!
>
> https://quality.livecode.com/show_bug.cgi?id=4636
>
> --
> Richard Gaskin
> Fourth World Systems
>
>
> _______________________________________________
> 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