B4R Question Date components to string

Mostez

Well-Known Member
Licensed User
Longtime User
Hello,
I have Date time components (year-month-day) and I want to compose a string to make a file name i.e. 2016\10\18 >> 20161018.DAT then use SD object to save data. I used joinstrings but it makes program to crash and start over, i tried to put date components into array but i could not pass it to SD object!! any ideas?

Thanks

B4X:
Dim currenttime As DSTime
    currenttime = DS1302.CurrentTime
     Years = currenttime.years
     Months = currenttime.Months
     DayOfMonth = currenttime.DayOfMonth
B4X:
Dim filename As String
    Dim DateTimeString As String = JoinStrings(Array As String(NumberFormat(Years,4,0),NumberFormat(Months,2,0),NumberFormat(DayOfMonth,2,0)))
    filename =JoinStrings(Array As String("dbase/",DateTimeString,".db"))

B4X:
FileNameArr =(Array As String("dbase/",NumberFormat(Years,4,0),NumberFormat(Months,2,0),NumberFormat(DayOfMonth,2,0),".db"))
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Which board are you using?

This is a more efficient solution:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private name(17) As Byte
   Private bc As ByteConverter
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   Dim years = 2016, months = 10, DayOfMonth = 19 As UInt
   SetFileName(years, months, DayOfMonth)
   Dim filename As String = bc.StringFromBytes(name)
End Sub

Private Sub SetFileName(years As UInt, months As UInt, days As UInt)
   Private raf As RandomAccessFile
   raf.Initialize(name, True)
   raf.WriteBytes("dbase/", 0, 6, raf.CurrentPosition)
   raf.WriteBytes(NumberFormat(years,4,0), 0, 4, raf.CurrentPosition)
   raf.WriteBytes(NumberFormat(months,2,0), 0, 2, raf.CurrentPosition)
   raf.WriteBytes(NumberFormat(days,2,0), 0, 2, raf.CurrentPosition)
   raf.WriteBytes(".db", 0, 3, raf.CurrentPosition)
End Sub
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
Your code worked perfectly, it returns the filename string as required. At first I got system crash again, I resized array length to 16(folder name is 'dlog/' now), so the full file name (including path) must not exceed 16 characters, it worked perfectly without any errors (when save data to SD), am I correct? or is it the Stack issue?
 
Upvote 0
Top