Android Question Basic date save and display

Mike Olmsted

Member
Licensed User
Longtime User
I have almost finished my checkbook app but I am stuck on a very simple thing.
how do I save a date and then get it back from SQL and display it.
I save it this way

lngDate = DateTime.Now
DateTime.DateFormat = "yyyy-MM-dd"
sqltxt= "INSERT INTO tblTransaction Values (null,'Check','','','" & lngDate & "'," _
&"'" & Main.strBankAccount & "','','0','0','0','','Check','0','','','0','0','')"
Main.SQL1.ExecNonQuery(sqltxt)

That stores 1379736978488 which is apparently a valid date.
then I have bunch of code that doesn't work....I need to print it on a check and display
the date in a register.

Sub dateStuff
'Dim TheDate As Long
'Dim strdate As String
DateTime.DateFormat = "yyyy-MM-dd"
lngDate= DateTime.DateParse(curMaster.GetString("Date")) <---the one I stored above
DateTime.DateFormat = "dd/MM/yyyy"
strdate = lngDate
edtDate.Text= DateTime.Date(strdate) ( or lngDate?? )

End Sub

Help, Mike - Lake Tahoe
 

mangojack

Expert
Licensed User
Longtime User
Please use [ Code ] [ /Code ] (without spaces) when posting code .. much easier to read.

You can format your date and store in the database as string .. then just retrieve and display.

In your above code ,because you are storing the date as Long there is no need to Parse the result, Just format and display.

B4X:
Sub dateStuff

   Dim TheDate As Long
   TheDate= curMaster.GetLong("Date")
   DateTime.DateFormat = "dd/MM/yyyy"
   edtDate.Text= DateTime.Date(TheDate)

End Sub

Cheers mj
 
Last edited:
Upvote 0

Mike Olmsted

Member
Licensed User
Longtime User
That worked...thanks. then I changed it and wrote it back
B4X:
 DateTime.DateFormat = "MM/dd/yyyy"  
  lngDate=DateTime.DateParse(edtDate.text)

Now I need to do a little work as if I enter 1/1/80, that is not the same as 1/1/1980
 
Upvote 0

Mike Olmsted

Member
Licensed User
Longtime User
To finish the discussion I submit 3 subs that will work in the US. The date is stored as long so it can be reported upon sorted by date. ( Hint..use a SQL view.) One will read the date, one will write it and one can be used in reports.
B4X:
Sub dateRead
'Dim lngDate As Long, strdate As String   
    lngDate= curMaster.GetLong("Date")      
    DateTime.DateFormat = "MM/dd/yy"
    edtDate.Text= DateTime.date(lngDate  
End Sub

Sub dateWrite
'Dim lngDate As Long, strdate As String
   DateTime.DateFormat = "MM/dd/yy" 
   lngDate=DateTime.DateParse(edtDate.text)
End Sub

Sub dateReport
'Dim cursor1 As Cursor
     lngDate= cursor1.GetLong("Date")
     DateTime.DateFormat = "MM/dd/yy"
     strdate= DateTime.Date(lngDate)
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I found the best way to store a date in a SQLite database is the following format: yyyymmdd (preferred) or yyyy-mm-dd. This way you can sort data ascending or descending very easily with that format, instead of storing long numbers that do not make any sense when you look at them. You can also query for data within a date range, you can group data and run all kind of aggregate functions, etc.
 
Upvote 0

Mike Olmsted

Member
Licensed User
Longtime User
I will try that as my method messes up the webview. Thanks

(Sometimes the most simple things are the most difficult.)
 
Upvote 0
Top