Android Question [SOLVED] Get a date from the database

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
I'm developing an application with B4A which connects to an MS SQL Server database using jRDC2

I get the date of the database (Type String)
B4X:
Main.DateServer = records (result.Columns.Get ("DateServer"))
I get an error with this code:
B4X:
Dim Month As String = DateTime.GetMonth (Main.DateServer)
I need to perform this type of operation using the Type DateTime
B4X:
Dim Month As String = DateTime.GetMonth (DateTime.Now)

How is it possible to convert my date from type String to DateTime?


Thank you very much for your valuable help.
 

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
Thanks klaus, the date I get from the MS SQL Server database is a string type that has the following format:

2018-05-14 11: 38: 01.6258711

Using the function DateTime.DateParse I would need a date with format MM/dd/YYYY, example code:

B4X:
Dim SomeTime As Long = DateTime.DateParse ("05/14/2018")

A possible solution would be to obtain the date the year, the month and the day using the SubString2 function but this would be very cumbersome.

What would then be an easy way to do this?
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
What would then be an easy way to do this?

Something like this:
B4X:
Dim myformat As String =DateTime.DateFormat
    DateTime.DateFormat  = "yyyy-MM-dd HH:mm:ss."
    Dim  SomeTime As Long = DateTime.DateParse ("2018-05-14 11:38:01.6258711")
    DateTime.DateFormat  = "MM/dd/yyyy"
    Dim MyDate As String =DateTime.Date(SomeTime)
    Log("My date is: " & MyDate)  'displays: My date is: 05/14/2018
    DateTime.DateFormat =myformat

If you have spaces between the hrs, min and sec, then you need to use this:
B4X:
Dim myformat As String =DateTime.DateFormat
    DateTime.DateFormat  = "yyyy-MM-dd HH: mm: ss."
    Dim  SomeTime As Long = DateTime.DateParse ("2018-05-14 11: 38: 01.6258711")
    DateTime.DateFormat  = "MM/dd/yyyy"
    Dim MyDate As String =DateTime.Date(SomeTime)
    Log("My date is: " & MyDate)  'displays: My date is: 05/14/2018
    DateTime.DateFormat =myformat
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…