NEXT Problem with date

Hi group, I have another problem with these “damned” datetimes. I would like to read in the PaintCellBackground of a listbox the date value stored in a cell for all the rows of the listbox and compare it with the current date. If the date read is greater than the current date, color the line red.

My code return error in DataRead=DateTime.FromString(convertedDate), Parse error: date needs to be in the format of YYYY-MM-DD HH:MM or YYYY-MM-DD …

Is there a way to set the format directly on this staff?


Dim convertedDate as String
Dim fieldY, fieldD, fieldM as String
Dim DataRead,DataNow as date


'Read in format dd/mm/YYYY
if listboxingressi.CellTextAt(row,6)<>"" then
  convertedDate=listboxingressi.CellTextAt(row,6)
else
  convertedDate=""
end if
DataRead=DateTime.FromString(convertedDate)



// THIS FUNCTION CONVERTS THE SQLDATE INPUT TO THE D/M/Y FORMAT
// AND REASSEMBLE DATE IN FORMAT DD/MM/YYYY
convertedDate = DateTime.Now.SQLDate
fieldY = convertedDate.NthField("-",1)
fieldM = convertedDate.NthField("-",2)
fieldD = convertedDate.NthField("-",3)
convertedDate = fieldD+"/"+fieldM+"/"+fieldY
messagebox " Oggi è la data ..:" + convertedDate
DataNow=DateTime.FromString(convertedDate)



if DataNow>DataRead then
  g.DrawingColor= &cff1100 ' rosso acceso
  g.FillRectangle(0, 0, g.Width, g.Height)
else
  if row Mod 2 = 0 Then
    g.DrawingColor= &cf0ffff ' celestino CHIARO
  Else
    g.DrawingColor= &cFFFFFF  'BIANCO
  End If
  g.FillRectangle(0, 0, g.Width, g.Height)
end if

Maybe you can use Locale.Current or define the locale that can turn dd/mm/yyyy to SQLDate using FromString(convertedDate, locale), see the documentation:
https://documentation.xojo.com/api/data_types/datetime.html#datetime-fromstring

Edit: you have convertedDate="" what do you expect for DataRead?

Why not store the actual date in the CellTag? No conversion required.

5 Likes

Hello group. Thanks for the tips. In the end I decided that for the comparison, I read the date from dd/mm/yyyy format and convert it to yyyy-mm-dd, and compare with the current date. It seems to work.

Var d1 As DateTime
Var ActualDate as datetime=DateTime.Now

  d1 = ConvertToDateTime(listboxingressi.CellTextAt(row,6))  
  if ActualDate>d1 then
 else
 end if

ConvertDateTime in a my method to convert a string in date:

if Date<>“” then
Dim theDate As New Date
Dim converted As Boolean
converted = ParseDate(date, theDate)
If converted Then
'MsgBox("Converted to: " + theDate.ShortDate)
Else
MsgBox("Incorrect DATE ")
End If
return theDate
else
return Nil
end if