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):
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:
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:
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:
As ticks are just numbers, they could be used inside all kinds of calculations.
There are four date and time constants:
To get the value of a specific date and time:
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.
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]
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)
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))
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])
There are four date and time constants:
- cTicksPerDay
- cTicksPerHour
- cTicksPerMinute
- cTicksPerSecond
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]
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]
The Calendar.Value property gets or sets the chosen date (as ticks).
Note that the Calendar has a Format property of its own.