Android Question Convert any date to milliseconds

Mehrzad238

Member
Hi guys,
I have an issue with converting any date to milliseconds I know that this code
B4X:
DateTime.Now
brings now date to milliseconds and if we wanted, we could add 24 hours in milliseconds to that time and we can use it just fine.
but what if I wanted to set a time for next week's Friday or the end of next month or a specific day.
I already searched the forum and all was about how to convert day to milliseconds, what about the date to milliseconds?
 

Mehrzad238

Member
alright I watched the video and it was helpful but still, I have a little issue with days and months.
with this code, I managed to add a day, month, and even year but with this sub ConvertMillisecondsToString I only can use it if it maximum 24 hours can anyone help me add day or month?

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    Dim p As Period
    p.Initialize
    p.Hours=5
     
    Dim t As Long= DateUtils.AddPeriod(DateTime.Now,p)
    Dim s As Long = DateTime.Now
    Log("t="&(t-s))
    Log(ConvertMillisecondsToString(t-s))
End Sub

Sub ConvertMillisecondsToString(t As Long) As String
    Dim hours, minutes, seconds As Int
    hours = t / DateTime.TicksPerHour
    minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
    seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
    Return $"$1.0{hours}:$2.0{minutes}:$2.0{seconds}"$
End Sub

thank you guys very much
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
what about the date to milliseconds?
Here are 3 examples to convert a date to ticks without the time component. If you want the time component, use: DateTime.DateTimeParse
All three give you the same ticks based on different date formats:
B4X:
Dim lngDate As Long    
    Dim MyDate As String ="2020-07-15"
    DateTime.DateFormat="yyyy-MM-dd"
    lngDate = DateTime.DateParse(MyDate)
    Log(lngDate)    'displays   1594785600000
    
    Dim MyDate As String ="15-07-2020"
    DateTime.DateFormat="dd-MM-yyyy"
    lngDate = DateTime.DateParse(MyDate)
    Log(lngDate)
    
    Dim MyDate As String ="07-15-2020"
    DateTime.DateFormat="MM-dd-yyyy"
    lngDate = DateTime.DateParse(MyDate)
    Log(lngDate)
 
Upvote 0
Top