Android Question Date Formatting

Discussion in 'Android Questions' started by FERNANDO SILVEIRA, Jun 4, 2018.

  1. FERNANDO SILVEIRA

    FERNANDO SILVEIRA Active Member Licensed User

    Hello guys,

    I know this been discussed here quite a few times, but I still am unable to format an SQL date field as I desire, it keeps showing me the date as yyyy-mm-dd...

    I tried the following code and the result is always the same: yyyy-mm-dd

    Code:
    DateTime.DateFormat = "dd/MM/yyyy"
    wDataEvento = wRegistro(
    6)       ' <== database DATE field

    DateTime.DateFormat = "EEE, d ' de ' MMMMM ' de ' yyyy"
    wDataEvento = wRegistro(
    6)       ' <== database DATE field
    What am I doing wrong?

    BTW, do I have to explicitly code DateTime.DateFormat = something whenever I want a given format?
    I was expecting a more straightforward thing like mydatefield = formatdate(db_field_date, "my format")... Any ideas?
     
  2. MarkusR

    MarkusR Well-Known Member Licensed User

    after u set the format u can use
    Code:
    Dim lngDate As Long = DateTime.DateParse(sDate)
    then the date is a number
    if u change the format again you can convert it back into text
    Code:
    btnDay1.Text = DateTime.Date(lngDate)
     
    Last edited: Jun 4, 2018
    FERNANDO SILVEIRA likes this.
  3. FERNANDO SILVEIRA

    FERNANDO SILVEIRA Active Member Licensed User

    Hi, Markus. Thanx for the feedback.
    Not sure I got you...

    1) You mean, something like ?
    Code:
    Dim wDataEvento As Long
    DateTime.DateFormat = DateFormato2
    wDataEvento = 
    DateTime.DateParse(wRegistro(6))    ' <=== ERROR Unparseable date: "1961-08-29" (at offset 0)
    2) I still didn't get what's the point in have a field type DATE if I have to change it to LONG in order to format it?
     
  4. MarkusR

    MarkusR Well-Known Member Licensed User

    what type is wRegistro(6) ? did it have " at start and end inside the string?

    B4J
    Code:
    '--------------------------------
        Dim l As Long
        
    DateTime.DateFormat = "yyyy-mm-dd" ' "1961-08-29"
        Dim datestring As String = "1961-08-29"
        l = 
    DateTime.DateParse(datestring)
        
    DateTime.DateFormat = "dd.mm.yyyy"
        
    Log(DateTime.Date(l))
        
    '--------------------------------
    i believe b4a using a long and did not have a date/time type.
    databases have much variants for store a date & time value.
    which library do you use for your database access?
    sql just means Structured Query Language
     
    Last edited: Jun 4, 2018
    FERNANDO SILVEIRA likes this.
  5. FERNANDO SILVEIRA

    FERNANDO SILVEIRA Active Member Licensed User

    wRegistro(6) is a string resulting from a SELECT statement. One of the selected fields (wRegistro(6)) is stored as DATE in SQLITE.

    Thank you again, besides I still don't believe so much code is required to format a date.

    Anyway... :cool::( As a local saying "that's what we have for today!"
     
  6. MarkusR

    MarkusR Well-Known Member Licensed User

    Last edited: Jun 4, 2018
    FERNANDO SILVEIRA likes this.
  7. FERNANDO SILVEIRA

    FERNANDO SILVEIRA Active Member Licensed User

    Thanx, Markus

    I created this code and it is working just fine:
    Code:
    Log(FormatarData("1981-08-11""yyyy-mm-dd""dd/mm/yyyy"))
    Log(FormatarData("1981-08-11""yyyy-mm-dd""dd de MMM de yyyy"))



    'Returns a date converted from format1 to format2
    'Element format accepted: dd, mm, yy, yyyy, MMM
    'example: FormatarData("1981-08-11", "yyyy-mm-dd", "dd/mm/yyyy")
    Sub FormatarData(data As String, formato1 As String, formato2 As StringAs String
        
    Dim result As String = formato2
        
    If formato2.IndexOf("MMM") <> -1 Then
            
    Dim meses() As String = Array As String ("janeiro""fevereiro""março""abril""maio""junho""julho""agosto""setembro""outubro""novembro""dezembro")
            result = result.Replace(
    "MMM", meses((data.SubString2(formato1.IndexOf("mm"), formato1.IndexOf("mm")+2)) - 1))
        
    Else
            result = result.Replace(
    "mm", data.SubString2(formato1.IndexOf("mm"), formato1.IndexOf("mm")+2))
        
    End If
        
    If formato1.IndexOf("yyyy") = -1 Then       
            result = result.Replace(
    "yy", data.SubString2(formato1.IndexOf("yy"), formato1.IndexOf("yy")+2))
        
    Else                   
            result = result.Replace(
    "yyyy", data.SubString2(formato1.IndexOf("yyyy"), formato1.IndexOf("yyyy")+4))
        
    End If
        result = result.Replace(
    "dd", data.SubString2(formato1.IndexOf("dd"), formato1.IndexOf("dd")+2))
        
    Return result
    End Sub
     
  8. Mahares

    Mahares Well Known Member Licensed User

    If you are happy with your code, that is fine, but your code looks verbose and complicated. If your date is: 1981-08-11 and you want to display it as: 11/08/1981, use the Substr function in SQLite. See the example below. It is different from Substring in B4A
    Code:
    Dim MyQuery As String
        MyQuery=
    "SELECT  substr(MyDate,9,2) || '/' || substr(MyDate,6,2) || '/' || substr(MyDate,1,4) AS DT FROM Mytable "
        Cursor1=SQL1.ExecQuery(MyQuery)
        
    For i=0 To Cursor1.RowCount-1
            Cursor1.Position=i
            LogColor (Cursor1.GetString(
    "DT"),Colors.Magenta)  '1981-08-11 will show up as: 11/08/1981
        Next
    You can alternatively use the strfTime function in SQLite, but it is more complicated. Here is a link to it:
    http://www.sqlite.org/lang_datefunc.html
     
    mangojack and FERNANDO SILVEIRA like this.
  9. FERNANDO SILVEIRA

    FERNANDO SILVEIRA Active Member Licensed User

    Thanx, Mahares

    I'll give it a try.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice