[quote=381533:@dave duke]Yes, sounds good, posting some could would be even better!
Thanks
Dave[/quote]
Sure. Here’s the script from my XojoScript presentation in at the Berlin conference last year:
[code]class dateClass
private dateID as integer
sub constructor()
dateID = date_newDate
end sub
property year as integer
get
return date_getYear(dateID)
end get
set
date_setYear(dateID, value)
end set
end property
property month as integer
get
return date_getMonth(dateID)
end get
set
date_setMonth(dateID, value)
end set
end property
property day as integer
get
return date_getDay(dateID)
end get
set
date_setDay(dateID, value)
end set
end property
function SQLDate() as string
return date_SQLDate(dateID)
end function
function SQLDateTime() as string
return date_SQLDateTime(dateID)
end function
function abbreviatedDate() as string
return date_abbreviatedDate(dateID)
end function
function longDate() as string
return date_longDate(dateID)
end function
function shortDate() as string
return date_shortDate(dateID)
end function
end class
dim d as dateClass
d = new dateClass
print “Default date:”
print d.SQLDate
d.year = 2013
d.month = 6
d.day = 30
print “”
print “New date:”
print d.SQLDate
[/code]
Note that the class definition is the main part – the code after that just demonstrates using the new date class in the script, so you can experiment with that to test different things.
For this to work, you’ll need a Private Property dateList as dictionary
property added to your context object and the following context methods:
[code]Public Sub date_setMonth(dateID as integer, month as integer)
dim d as date = returnDateFromID(dateID)
d.month = month
setDateFromID(dateID, d)
End Sub
Public Function date_abbreviatedDate(dateID as integer) as string
return returnDateFromID(dateID).abbreviatedDate
End Function
Public Function date_getDay(dateID as integer) as integer
return returnDateFromID(dateID).day
End Function
Public Function date_getMonth(dateID as integer) as integer
return returnDateFromID(dateID).month
End Function
Public Function date_getYear(dateID as integer) as integer
return returnDateFromID(dateID).year
End Function
Public Function date_longDate(dateID as integer) as string
return returnDateFromID(dateID).longDate
End Function
Public Function date_newDate() as integer
if dateList = nil then dateList = new dictionary
dim id as integer = dateList.count + 1
dateList.value(id) = new date
return id
End Function
Public Sub date_setDay(dateID as integer, day as integer)
dim d as date = returnDateFromID(dateID)
d.day = day
setDateFromID(dateID, d)
End Sub
Public Sub date_setYear(dateID as integer, year as integer)
dim d as date = returnDateFromID(dateID)
d.year = year
setDateFromID(dateID, d)
End Sub
Public Function date_shortDate(dateID as integer) as string
return returnDateFromID(dateID).shortDate
End Function
Public Function date_SQLDate(dateID as integer) as string
return returnDateFromID(dateID).SQLDate
End Function
Public Function date_SQLDateTime(dateID as integer) as string
return returnDateFromID(dateID).SQLDateTime
End Function
Public Sub init()
dateList = new dictionary
End Sub
Private Function returnDateFromID(dateID as integer) as date
if dateList = nil then dateList = new dictionary
return dateList.lookup(dateID, new date)
End Function
Private Sub setDateFromID(dateID as integer, aDate as date)
if dateList = nil then dateList = new dictionary
dateList.value(dateID) = aDate
End Sub
[/code]
As you can probably figure out, these are the context methods the script methods call. Because the class we set up in out script mirrors the normal date class, once you’ve built this, you can copy/paste normal (classic framework) date code right into your script and it will run since the syntax is the same. The actual date objects and calculations are all being managed inside the main Xojo program, but your script doesn’t need to know that. From the perspective of the script’s code, you’re working with a date object in the script and it just works.
You can do the same thing with other Xojo objects, such as folderitems, but it is some work to set it up and you probably don’t need all that power in the script. (Like for folderitems, I usually just set up simple load/save text file context routines and that’s all I need in my script.) But the date class is pretty simple and it can be useful to have access to all its functions within your scripts.
Let me know if you have questions.