Date and Time

Erel

B4X founder
Staff member
Licensed User
Longtime User
Basic4ppc stores date and time values as ticks.
Each tick represents 1 / 10,000,000 of a second and counting starts at January 1, AD 0001.
This post was written on September 23, 2007 which is 633261705083378192 ticks.
It is more convenient to store the values as a number, because date and time formats could vary.
There are two pairs of keywords that transfer a ticks value to a formatted string (Date and Time) and vice versa (DateParse and TimeParse).
Now returns the ticks value of the current time.
So if we want to show the current date and time (string formatted):
B4X:
[COLOR=#010101][COLOR=#0000ff]Msgbox[/COLOR][COLOR=#000000]([/COLOR][COLOR=#0000ff]Date[/COLOR][COLOR=#000000]([/COLOR][COLOR=#0000ff]Now[/COLOR][COLOR=#000000]) & [/COLOR][COLOR=#800000]" "[/COLOR][COLOR=#000000] & [/COLOR][COLOR=#0000ff]Time[/COLOR][COLOR=#000000]([/COLOR][COLOR=#0000ff]Now[/COLOR][COLOR=#000000]))[/COLOR]
[/COLOR]
DateFormat and TimeFormat keywords set the date and time string format.
The default formats are:
DateFormat("mm/dd/yyyy")
TimeFormat("HH:mm") - 24 hours format.
See the help manual for more information about the formats.
DateParse and TimeParse convert a string formatted date or time to a ticks value.
The string format must exactly match the DateFormat and TimeFormat values (or the default values).
So if we want to store a specific date or we want to use a date given by the user we will use:
B4X:
d = [COLOR=#0000ff]DateParse[/COLOR]([COLOR=#800000]"02/03/2004"[/COLOR])
[COLOR=#0000ff]Msgbox[/COLOR]([COLOR=#800000]"String: "[/COLOR] & [COLOR=#0000ff]Date[/COLOR](d) & crlf & [COLOR=#800000]"Ticks: "[/COLOR] & d)
Picture1.jpg


The actual time that this value represents is February 03, 2004 00:00 AM.
If we use TimeParse we will get a value that represents the time specified and the date will be today.
For example:
B4X:
d = [COLOR=#0000ff]TimeParse[/COLOR]([COLOR=#800000]"12:30"[/COLOR])
[COLOR=#0000ff]Msgbox[/COLOR]([COLOR=#800000]"Date: "[/COLOR] & [COLOR=#0000ff]Date[/COLOR](d) & crlf & [COLOR=#800000]"Time: "[/COLOR] & [COLOR=#0000ff]Time[/COLOR](d))
This code will show today's date and 12:30.
Keywords like DateMonth or TimeHour return a specific date or time component from a ticks value.
DateAdd and TimeAdd return a new ticks value after adding the required years, months...
If we want to add 7 days to a specific date:
B4X:
d = [COLOR=#0000ff]DateAdd[/COLOR]([COLOR=black]d[/COLOR],[COLOR=#800080]0[/COLOR],[COLOR=#800080]0[/COLOR],[COLOR=#800080]7[/COLOR])
As ticks are just numbers, they could be used inside all kinds of calculations.
There are four date and time constants:
  1. cTicksPerDay
  2. cTicksPerHour
  3. cTicksPerMinute
  4. cTicksPerSecond
To calculate the number of days between two dates:
B4X:
d1 = [COLOR=#0000ff]DateParse[/COLOR]([COLOR=#800000]"04/30/2006"[/COLOR])
d2 = [COLOR=#0000ff]DateParse[/COLOR]([COLOR=#800000]"04/30/2007"[/COLOR])
[COLOR=#0000ff]Msgbox[/COLOR]([COLOR=#0000ff]Int[/COLOR]((d2-d1)/cTicksPerDay)) [COLOR=#008000]'will show 365[/COLOR]
To get the value of a specific date and time:
B4X:
d = [COLOR=#0000ff]DateParse[/COLOR]([COLOR=#800000]"04/30/2006"[/COLOR]) + ([COLOR=#0000ff]TimeParse[/COLOR]([COLOR=#800000]"13:35"[/COLOR]) [COLOR=#0000ff]Mod[/COLOR] cTicksPerDay)
[COLOR=#0000ff]Msgbox[/COLOR]([COLOR=#0000ff]Date[/COLOR](d) & [COLOR=#800000]" "[/COLOR] & [COLOR=#0000ff]Time[/COLOR](d)) [COLOR=#008000]'will show 04/30/2006 13:35[/COLOR]
You can use the Calendar control to allow the user to choose a specific date.
The Calendar.Value property gets or sets the chosen date (as ticks).
Note that the Calendar has a Format property of its own.
 

SonicSue

Member
Licensed User
TimeAdd

Apologies in advance if this question is addressed somewhere...I have not found it if it is. Anywhoo...

In using TimeAdd within a Countdown Timer project, I accept numbers as input from the user and insert those values into the TimeAdd(x, H, M, S). Then I use that info to give me the time to count down. Here is the problem: If I try to enter greater than 24 hours as the user, and display the time in hours, minutes and seconds as it counts down, the hours turns to '0' for 25, and adds 1 for each value above 24 hours. Just curious why this is happening.
Here's part of the code:
B4X:
Sub Start_Click
   H = H.Text
   M = M.Text
   S = S.Text
   HMS.Visible=True
   
Timer1.Enabled = True
Timer1.Interval = 1000
Timer2.Interval = 30000 '30 seconds
Timer2.Enabled = True

   
If IsNumber(H)=True AND IsNumber(M)=True AND IsNumber(S)=True Then
         x=Now
         StartTime = TimeAdd(x, H, M, S)
         
         Diff = (StartTime-x)/cTicksPerSecond
         Start.Visible = False
         Pause.Visible = True

      Else 
         Message_Box
         Timer1.Enabled=False
         Timer2.Enabled=False
         Display.Text=""
         M.Focus
         Start.Visible = True
         Pause.Visible = False
End If

If H=0 AND M=0 AND S=0 Then
   Message_Box
   Timer1.Enabled=False
   Timer2.Enabled=False
   'Display.Text=""
   H.Focus
   Start.Visible = True
   Pause.Visible = False
   TH=0
   TS=0
   TM=0
End If
End Sub

Sub Timer1_Tick
   Diff=Diff-1

If Diff = 0 Then 
   Timer1.Enabled = False
   Timer2.Enabled = False
   Start.Visible = True
   Pause.Visible = False
   Sound("untie.wav")
End If
   Display.Text=Diff
If H=0 AND M=0 AND S=0 Then
   Reset_Click
Else
   TM=TimeMinute(Diff*cTicksPerSecond)
   TH=TimeHour(Diff*cTicksPerSecond)
   TS=TimeSecond(Diff*cTicksPerSecond)
End If
   HMS.Text = "Hrs: " & TH & " Mins: " & TM & " Secs: " & TS
End Sub

Thanks in advance. :)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The following code splits seconds to hours, minutes and seconds:
B4X:
Sub App_Start
    seconds = 3876
    TH = Int(seconds / 3600)
    TM = Int ((seconds - TH * 3600)/60)
    TS = seconds - TH * 3600 - TM * 60
    Msgbox(Format(th,"d2") & ":" & Format(tm, "d2") & ":" & Format(ts, "d2"))
End Sub
TimeHour/Minute/Second takes a date value which is stored as ticks and returns the requested time section. The date part is returned by the Date keyword.
The time methods will always return a value between 0 to 23 or 0 to 59.
 

SonicSue

Member
Licensed User
...so if I understand correctly, there isn't a way around it, and the countdown timer will only be able to accept 24 hours or less, right? Thanks Erel.:sign0188:
 

SonicSue

Member
Licensed User
I used Erel's code and it works like a dream! Thanks!

Also, thanks dfallen - I looked at your code as well.

I love this forum, and I love Basic4PPC! :sign0089:
 
Top