Conversion a string to a date

Hi All.

I have been trying to convert a string to date time, but I can’t seem to get it to work.

I’ve gone through the posts and everything I have tried seems to fail.
I’ve created text variables and passing the textfield information to it, and then using the Xojo.Core.Date, but it refuses to work. I get

I keep getting an error message saying I passed a string and it wants a text, but my variable is

var startTimeasText as Text

Any ideas?
If you wish I can post the code but it is a little long.


We need at least the Text that you are using and the Xojo.Core.Date command.

What version of Xojo are you using? Is this for a Desktop application?

Is this what you need?

What string ?

A string with numbers (2021-04-11 ?) or a string with characters (human readable) ?

I guess Michael is using

Hi Alberto, Kem,

Kem. That is the document I looked at, which I tried but couldn’t seem to get to work…
Emile: I have a textbox that is filled with a dateTime…

app.startIncidentTime = new date
app.startIncidentTime = DateTime.Now
mainWindow.startTextfield.text = str(app.startIncidentTime)

Version of Xojo is: 2020 Release 2.1
I have NOT been able to get the to work… so… there isn’t anything in the code right now. I’ve rolled it back to the last working version as I mangled the code something fierce.

Here is the code (as it is… not fancy and I really over comment. It helps me. Hopefully it won’t confuse to much):

mainWindow.techAdd1Popup.Enabled = false
mainWindow.techAdd1Popup.InitialValue = "-"

// ok I have declared the timer events in the app.
// so let's do the time

Var d As New MessageDialog                  // declare the MessageDialog object
Var b As MessageDialogButton                // for handling the result

app.endIncidentTime = new date
app.endIncidentTime = DateTime.Now

dim replacednameTextField as string // to hold the information after removing ' quotes
dim replacednotesTextArea as string // to hold the information after removing ' quotes

mainWindow.endTextField.text = str(app.endIncidentTime)

//check to see if startTime is missing.  If it is, don't let them save.  They will have to re-enter their data.  Teach them, I will!
// create a dialog box later so that they can choose to save their entered information... mind since the time is autogenerated, it still won't be 100% correct.
// maybe I'll fix that by allowing them to enter a start time and mark that this wasn't autogenerated for continuity's sake.

if mainWindow.startTextfield.text = "" then
  // change this to a message dialog to give them the option of clearing everything out, or saving and restarting
  d.Icon = MessageDialog.GraphicCaution       // display warning icon
  d.ActionButton.Caption = "Retain all but Start and End time"
  d.CancelButton.Visible = True               // show the Cancel button
  d.AlternateActionButton.Visible = True      // show the "Don't Save" button
  d.AlternateActionButton.Caption = "Delete all Information"
  d.Message = "You are missing the start Time, which means time worked will be incorrect"
  d.Explanation = "Chose one of the options displayed in the buttons. "
  // now enable the starttime and endtime so the person can enter their data
  // but append "Manually entered" so they know it is not autoentered so can't be considered "true"
  startTextfield.Enabled = true
  endTextField.Enabled = true
  b = d.ShowModal                             // display the dialog
  Select Case b                               // determine which button was pressed.
  Case d.ActionButton
    // use these for the conversion
    var manualStartTime as Xojo.Core.Date
    var manualEndTime as Xojo.Core.Date
    var convertedToTextStartTime as Text
    var convertedToTextEndTime as Text
    'mainWindow.groupPopupMenu.SelectedRowIndex = -1
    'mainWindow.nameTextField.text =""
    mainWindow.startTextfield.text = ""
    mainWindow.endTextField.text = ""
    'mainWindow.notesTextArea.text = ""
    'mainWindow.techAdd1Popup.SelectedRowIndex = -1
    'assistingTechCell.text = ""
    'assistingTechOffice.text = ""
    startTextfield.TextColor = RGB(255,0,0)
    endTextField.TextColor = RGB(255,0,0)
    convertedToTextStartTime = startTextfield.text.ToText
    convertedToTextEndTime = endTextField.text.toText
  Case d.AlternateActionButton
    mainWindow.endTextField.text = ""
    mainWindow.groupPopupMenu.SelectedRowIndex = -1
    mainWindow.nameTextField.text =""
    mainWindow.startTextfield.text = ""
    mainWindow.endTextField.text = ""
    mainWindow.notesTextArea.text = ""
    mainWindow.techAdd1Popup.SelectedRowIndex = -1
    assistingTechCell.text = ""
    assistingTechOffice.text = ""
    startTextfield.TextColor = RGB(255,0,0)
    endTextField.TextColor = RGB(255,0,0)
  Case d.CancelButton
    // user pressed Cancel
  End Select
end if

// now that the end button has been pushed, get the difference

app.timeDifference = app.endIncidentTime.TotalSeconds - app.startIncidentTime.TotalSeconds

//before you write into the database, make sure that the textfields (if the have ' single quotes are changed to _ or
//it will mess up and not write properly

replacednameTextField = nameTextField.text.Replace("'","_")
replacednotesTextArea = notesTextArea.text.Replace ("'","_")

// this is done to deal with end of line issues in an sqlite database.  We check to see what version of OS is being run on, and change accordingly

#if TargetMacOS then
  replacednotesTextArea = notesTextArea.text.ReplaceLineEndings(EndOfLine.macOS)
  replacednotesTextArea = notesTextArea.text.ReplaceLineEndings(EndOfLine.Windows)

dim sql as string // holder for SQL command

sql = "INSERT INTO Activity (groupType, Name, startTIme, endTime, Notes, seconds, minutes, hours, tech3, tech2, tech1) VALUES ('"+groupPopupMenu.getString+"','"+replacednameTextField+"','"+startTextfield.text+"','"+endTextField.text+"','"+replacednotesTextArea+"','"+app.timeDifference.ToString+"','"+str(app.timeDifference/60)+"','"+str(app.timeDifference / 3600)+"','"+techAdd1Popup.getString+"','"+assistingTechOffice.text+"','"+assistingTechCell.text+"')"



app.startIncidentTime is a DateTime right?

app.endIncidentTime = new date
app.endIncidentTime = DateTime.Now

The first line is used with Date. It works with DateTime, the only difference I can see is that it creates a DateTime without nanoseconds. So you can remove the first line.

var manualStartTime as Xojo.Core.Date
var manualEndTime as Xojo.Core.Date

I don’t see manualStartTime or manualEndTime used elsewhere in your code.

If you are going to use DateTime then you don’t need Xojo.Core.Date and you don’t need to convert String to Text.

Hi Alberto.

Yes you don’t see manualStartTime or manualEndTime used as (and I believe i mentioned this… if not put it down to old age and a failing memory) this was the working code that I supplied. I had mangled my code so badly, that it wouldn’t run.

The issue that is fighting me is when I follow the documentation, I get an error that says something like “expected Text got string”.

I’ll redo my code based on what you have told me, and if I have any questions, revisit here.

Thank you for your help.


Hi Alberto.

My problem, after I went a did another step by step debug comes when the user manually enters the date and time into the startTime and endTime text boxes.

If I do not statically enter into the endTime textfield, it works just fine… but if I enter data into the endTime textfield, it fails at

app.timeDifference = app.endIncidentTime.TotalSeconds - app.startIncidentTime.TotalSeconds

with an error 0.

When I look up the code, I gives me only 0 even if I use:

Exception err As NilObjectException
MessageBox(err.Message + ". Error Code: " + err.ErrorNumber.ToString)

Now a light has gone on to the reason this happens as when I enter the time manually it considers it text not a dateTime so that I can calculate the difference. All I need to do is make these dateTimes and it should work, right? So how in the name of the Incarnations do I do this…


If you can share a simple sample Xojo code (dropbox, google drive, other) it will be easier for us to see the code that transform the entered date from the user and see the problem.

I’ve put the project and required databases on my google drive.

How do I share it… I’ve never done it before…

Click the file to share, click the link icon
change the permission to Anyone with the link
post the link here.

Here’s the link.

Please be merciful. My coding isn’t the sweetest, but I’m self taught…
Images are pictures of my cats that show in the about screen…
And the splash image are the goodbye pictures from two of the nicest ladies who I ever worked with.

Ok, Xojo has 3 classes to handle dates, Date, Xojo.Core.Date and DateTime. DateTime is the new one that deprecates the original Date (that can still be used).

You are defining App.startIncidentTime as Date, you have 2 lines of code with DateTime.Now and your original post mentioned Xojo.Core.Date

I think you can use DateTime for all your needs.

Now, it is not clear what you want so I don’t know what the problem is. Do you want to allow users to type a date and a time on a TextField and then convert that? Maybe you can use the new datepicker control.

When I asked about a sample, just a quick code with what you want to do (with just a few fields and trying to convert the date).

Oops. Sorry about the over information.

You are correct in the fact that I want the user to be able to type in the startTime and endTime because the times are used to calculate how much time was spent per different groups, should the need arise.

But if they don’t hit the start button, when they click the end button, the time is WAY off and not really pleasing to supervisors or customers.

You can destroy the project, if you don’t want it – infecting your stuff – or I can try to make a smaller project in the same manner and get it to you. But I’m kind of busy with real work (have to pay bills) so it might be a couple of days.

Thank you ever so much for your help with this, all.