SQLite query date format

mc73

Well-Known Member
Licensed User
Longtime User
It's a matter of taste, but also depends on what your aim is. For example, I always use the YYYYMMDD format. It's just a number, making sorting clear, and after all whenever I want I format the output as needed.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
B4X:
DateTime.DateFormat = "yyyy-MM-dd"
After manipulating your queries in the above date format, you should return your date format to its original. For example, If you use month, day, year, then, you need to issue:
B4X:
DateTime.DateFormat = "M/d/yyyy" 'example: 7/19/2012 starting format

Like mc73, I like to store a date as YYYYMMDD. It can be stored as a TEXT field or INTEGER in a SQLite table. You can do any WHERE clauses with it like between, >, < and also ORDER BY either DESC or ASC, etc.
 
Last edited:
Upvote 0

TyneBridges

Member
Licensed User
Longtime User
A related newbie question - how do we then display the number as a date after reading it? The DateFormat command doesn't work there because the compiler doesn't know that the value it has read is a date, so it just displays as a long number...

Thanks.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Here is an example:
B4X:
Dim MyDate, strDate As String 
strDate= "20120922"
MyDate=strDate.SubString2(4,6) &"/" & strDate.SubString(6) &"/" & strDate.SubString2(0,4)
Msgbox(MyDate,"")   'returns 09/22/2012
MyDate=strDate.SubString(6) &"/" & strDate.SubString2(4,6) &"/" &  strDate.SubString2(0,4)
Msgbox(MyDate,"")   'returns 22/09/2012
 
Upvote 0

TyneBridges

Member
Licensed User
Longtime User
Thanks. I guess it's the meaning of "lite" in SQLite that it handles dates in a way that is flexible, but also quite clumsy and error-prone because of the lack of variable typing.
 
Upvote 0

TyneBridges

Member
Licensed User
Longtime User
I'm still having lots of trouble with DateFormat and date arithmetic. The documentation suggests that the DateFormat command should only affect the way a date is displayed, not the way that the system stores it - or have I misunderstood this? I'm storing strings in my database in UK date order, but struggling with date manipulation.

The following code does not work as intended. CDateStr contains the expected value but I thought I would be able to add a week to this (regardless of DateFormat) using DateTime.Add(DateTime.Date(DateTime.Now), 0, 0, 7). However, the value of Main.CExpDate as it appears in my message box is completely garbled. Can anyone see what I'm doing wrong? Both CDateStr and CExpDate are defined as strings.

DateTime.DateFormat = "ddMMyyhhmm"
CDateStr = DateTime.Date(DateTime.Now)
Main.CExpDate = DateTime.Add(DateTime.Date(DateTime.Now), 0, 0, 7)
Msgbox("Date now stored as " & CDateStr & ". Expiry date: " & Main.CExpDate, Main.AppName)

:sign0013:
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
To add 7 days to today see below:
B4X:
Dim CExpDate As String
      DateTime.DateFormat = "dd/MM/yy hh:mm a" 
      CExpDate =DateTime.Date(DateTime.Add(DateTime.now,0,0,7))  'add 7 days to today
      Msgbox(CExpDate,"") 'returns 11/10/12  04:19 PM
If you set the: DateTime.DateFormat = "dd/MM/yy" it returns: 11/10/12
 
Upvote 0

TyneBridges

Member
Licensed User
Longtime User
Thanks. Following your example, I spotted that I had my syntax/bracketing the wrong way round.
 
Upvote 0
Top