Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Code Samples & Tips > Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Tutorials Basic4ppc tutorials

Date and Time

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-24-2007, 10:05 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 13,162
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Date and Time

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):
Code:
<font color="#010101"><font color="#0000ff">Msgbox</font><font color="#000000">(</font><font color="#0000ff">Date</font><font color="#000000">(</font><font color="#0000ff">Now</font><font color="#000000">) & </font><font color="#800000">" "</font><font color="#000000"> & </font><font color="#0000ff">Time</font><font color="#000000">(</font><font color="#0000ff">Now</font><font color="#000000">))</font>
</font>
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:
Code:
d = <font color="#0000ff">DateParse</font>(<font color="#800000">"02/03/2004"</font>)
<font color=
"#0000ff">Msgbox</font>(<font color="#800000">"String: "</font> & <font color="#0000ff">Date</font>(d) & crlf & <font color="#800000">"Ticks: "</font> & d)


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:
Code:
d = <font color="#0000ff">TimeParse</font>(<font color="#800000">"12:30"</font>)
<font color=
"#0000ff">Msgbox</font>(<font color="#800000">"Date: "</font> & <font color="#0000ff">Date</font>(d) & crlf & <font color="#800000">"Time: "</font> & <font color="#0000ff">Time</font>(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:
Code:
d = <font color="#0000ff">DateAdd</font>(<font color="black">d</font>,<font color="#800080">0</font>,<font color="#800080">0</font>,<font color="#800080">7</font>)
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:
Code:
d1 = <font color="#0000ff">DateParse</font>(<font color="#800000">"04/30/2006"</font>)
d2 = <font color=
"#0000ff">DateParse</font>(<font color="#800000">"04/30/2007"</font>)
<font color=
"#0000ff">Msgbox</font>(<font color="#0000ff">Int</font>((d2-d1)/cTicksPerDay)) <font color="#008000">'will show 365</font>
To get the value of a specific date and time:
Code:
d = <font color="#0000ff">DateParse</font>(<font color="#800000">"04/30/2006"</font>) + (<font color="#0000ff">TimeParse</font>(<font color="#800000">"13:35"</font>) <font color="#0000ff">Mod</font> cTicksPerDay)
<font color=
"#0000ff">Msgbox</font>(<font color="#0000ff">Date</font>(d) & <font color="#800000">" "</font> & <font color="#0000ff">Time</font>(d)) <font color="#008000">'will show 04/30/2006 13:35</font>
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.
Reply With Quote
  #2 (permalink)  
Old 01-27-2010, 03:55 AM
Junior Member
 
Join Date: Nov 2009
Posts: 16
Default 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:
Code:
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.
Reply With Quote
  #3 (permalink)  
Old 01-27-2010, 05:24 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 13,162
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

The following code splits seconds to hours, minutes and seconds:
Code:
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.
__________________
Basic4android documentation
Reply With Quote
  #4 (permalink)  
Old 01-27-2010, 06:01 AM
Junior Member
 
Join Date: Nov 2009
Posts: 16
Default

...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.
Reply With Quote
  #5 (permalink)  
Old 01-27-2010, 06:26 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 13,162
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You can use a code similar to the one I've posted to accept any time required.
__________________
Basic4android documentation
Reply With Quote
  #6 (permalink)  
Old 01-27-2010, 07:36 PM
Senior Member
 
Join Date: Apr 2007
Location: Arlington, Washington USA
Posts: 176
Default

SonicSue caused me to review my old CountDown program and in doing so I found and corrected a bug - thanks SonicSue!

Perhaps my old program addresses SonicSue's question (http://www.basic4ppc.com/forum/share...own-timer.html). It does allow the user to enter dates/times well into the future. Perhaps the code could be easily adapted to meet SonicSue's needs.
Reply With Quote
  #7 (permalink)  
Old 01-29-2010, 12:01 AM
Junior Member
 
Join Date: Nov 2009
Posts: 16
Default

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!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
working with date gjoisa Questions (Windows Mobile) 3 03-29-2008 10:40 AM
How to get the value of date substraction ? gjoisa Questions (Windows Mobile) 1 02-11-2008 10:15 AM
How I have compare date? epatrik Questions (Windows Mobile) 4 09-20-2007 10:54 AM
How to get saved date and time value or other properties of a file? Rioven Questions (Windows Mobile) 1 08-30-2007 07:10 AM
filter for date manu Questions (Windows Mobile) 2 06-09-2007 02:13 PM


All times are GMT. The time now is 07:47 PM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0