Three Date Methods Using Xojo.Core.Date

These three date methods written using Xojo.Core.Date, can, as far as I can tell, be used to 1) Get a Date Mask that matches the locale (which also gets and saves the date order and the date separator), 2) Pad a Date: allows you to show 02/05/2015 instead of 2/5/15, and Validate a Date to make sure it’s valid. This return an empty string if the date isn’t valid, and a SQL date if it is valid so it’s ready to store in your database.

I have two private Properties in my module called dateOrder and dateSep. Those are populated by dateGetMask, so that method should be called first.

As a note, the padded date, if it already exists, can be put into a text field to give the user an idea of the way to enter. If you’re going to do that, I recommend putting the mask returned into the Mask property and not the Format property. For some reason, for me, dates shown with the Format property displaying the mask disappear when the TextField no longer has the focus.

You are welcome to make suggestions about this and changes that make it better, more universal, etc. are helpful. I’ve only tested it Eng-US, so any other testing would be welcome. Please don’t flame me. I’m old. (See bearded man to the left. And that’s kind of an old photo.)

[code]Function dateGetMask(pforce4DigYear As Boolean = True) As String
Using xojo.Core
// don’t know who created the bulk of this code, but it was posted on the Xojo Forum
// 2014-02-19 gfmc
dim tmpDate as New date( 2256, 01, 30, Timezone.Current ) // important that you set this so each component can be identified individually
dim tmpStr as string = tmpDate.ToText(locale.Current, Date.FormatStyles.Short, Date.FormatStyles.None)
// get the sep by replacing 2256, 56, 01, 1 and 30 IN THAT ORDER
dim sep as string = replaceall(tmpStr, “2256”, “”)
sep = replaceall(sep, “56”, “”)
sep = replaceall(sep, “01”, “”)
sep = replaceall(sep, “1”, “”)
sep = replaceall(sep, “30”, “”)
// whats left should be the separator(s)
// in theory you should have multiple (at least 2) but lord knows
// this DOES assume its one character
sep = Left(sep, 1)
dateSep = sep // this stores the value for dateSep

dim firstpart as string
dim secondpart as string
dim thirdpart as string

// now figure out the order
// pforce4DigYear will force the mask into YYYY rather than the short YY
dim part() as string = REALbasic.split(tmpStr, sep)
// we should have 3 parts
if part(0) = “2256” then firstPart = “YYYY”
if part(0) = “56” then
If pforce4DigYear Then
firstPart = “YYYY”
Else
firstPart = “YY”
End If
End If
if part(0) = “1” then firstPart = “MM”
if part(0) = “01” then firstPart = “MM”
if part(0) = “30” then firstpart = “DD”

if part(0) = “2256” then secondpart = “YYYY”
if part(0) = “56” then
If pforce4DigYear Then
secondpart = “YYYY”
Else
secondpart = “YY”
End If
End If
if part(1) = “1” then secondpart = “MM”
if part(1) = “01” then secondpart = “MM”
if part(1) = “30” then secondpart = “DD”

if part(1) = “2256” then thirdpart = “YYYY”
if part(1) = “56” then
If pforce4DigYear Then
thirdpart = “YYYY”
Else
thirdpart = “YY”
End If
End If
if part(2) = “1” then thirdpart = “MM”
if part(2) = “01” then thirdpart = “MM”
if part(2) = “30” then thirdpart = “DD”

// and this for dateOrder
dateOrder = firstPart.Left(1) + " " + secondpart.Left(1) + " " +thirdpart.Left(1)

// Now put it back together and return it
Dim retStr As String
Dim i, slen As Integer
slen = firstpart.Len
For i = 1 To slen
retStr = retStr + “9”
Next i
retStr = retStr + dateSep

slen = secondpart.Len
For i = 1 To slen
retStr = retStr + “9”
Next i
retStr = retStr + dateSeparator

slen = thirdpart.Len
For i = 1 To slen
retStr = retStr + “9”
Next i

Return retStr
End Function

Function datePad(pDate As String) As String
// This function assumes a SQL date is passed in. It returns it in the padded, short date format.
// 2014-02-19 gfcm

// SQL date without time is always a length of 10
pDate = pDate.Left(10).Trim

Dim aDate() As String = pDate.Split("-")
// if a time was passed in,
Dim aOrder() As String = dateOrder.Split
// below, by the way, is the order in SQL date
Const cYear = “Y”
Const cMonth = “M”
Const cDay = “D”

Dim retStr As String
For i As Integer = 0 To 2
Select Case aOrder(i)
Case cMonth
retStr = retStr + aDate(1)
Case cYear
retStr = retStr + aDate(0)
Case cDay
retStr = retStr + aDate(2)
Case Else
MsgBox "Error in datePad. Unknown value: " + aOrder(i)
End Select
If i <> 2 Then retStr = retStr + dateSep
Next i

Return retStr
End Function

Function dateValid(pstrDate As String) As String
// simplified version of the date Validation as the order and seperator are known
// if the date is valid, this will return the SQL date, else an empty string
// 2015.02.19 gfmc

Using xojo.core

// dateOrder is a string containing the letters M Y D separated by spaces.
// we will make them an array for easy usage
Dim aOrder() As String = dateOrder.Split

// split up the date into an array of three parts
Dim adVals() As String = pstrDate.Split(dateSep)

// now we will make the integers to make the date
Dim i, iYear, iDay, iMonth As Integer
For i = 0 To 2
Select Case aOrder(i)
Case “Y”
iYear = adVals(i).CLong
Case “D”
iDay = adVals(i).CLong
Case “M”
iMonth = adVals(i).CLong
Case Else
MsgBox "Unknown Case in dateValid: " + aOrder(i)
End Select
Next i

// now let’s make a new date
// IMPORTANT: it will not throw an error if it is an invalid date
// should the date passed in be 2/31/2015 (an invalid date), the Constructor will use that date but make it 3/3/2015
// so it’s invalid if the d.ToText date parsed into values is not the same as the passed date.
Dim d As New Date(iYear, iMonth, iDay, TimeZone.Current)
Dim retStr As String = d.ToText.Left(10)

// now let’s break that up because we know that the Xojo.Core.Date.ToText is going to return a SQL date, 2015-02-19
ReDim adVals(-1)
adVals = retStr.Split("-")
Dim isValid As Boolean = True
If adVals(0).CLong <> iYear Then
isValid = False
Elseif isValid And adVals(1).CLong <> iMonth Then
isValid = False
Elseif isValid And adVals(2).CLong <> iDay Then
isValid = False
End If

// return the proper value
If isValid Then
Return retStr
Else
Return “”
End If
End Function
[/code]

Don’t need these right now, but I have archived them for when I may.
Thanks for sharing, Gary.

Nice. I’m just starting to play around with the new framework (as it is so far on Windows) so these may come in handy.

Thanks. I may have some updates. I’ve been working some more and will probably update it on 2/21.

I’m from Philadelphia. We might boo Santa Claus, but we wouldn’t flame him. :wink: