Thread: Date and Time
View Single Post
  #1 (permalink)  
Old 09-24-2007, 09:05 AM
Erel's Avatar
Erel Erel is offline
Administrator
 
Join Date: Apr 2007
Posts: 15,688
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