Difference between revisions of "Documentation/BASIC Guide/Date and Time (Runtime Library)"

From Apache OpenOffice Wiki
Jump to: navigation, search
m
 
(16 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 +
{{DISPLAYTITLE:Date and Time ({{AOo}} Runtime Library)}}
 
{{Documentation/BASICGuideTOC/v2
 
{{Documentation/BASICGuideTOC/v2
 
|ShowPrevNext=block
 
|ShowPrevNext=block
Line 5: Line 6:
 
|NextPage=Documentation/BASIC Guide/Files and Directories (Runtime Library)
 
|NextPage=Documentation/BASIC Guide/Files and Directories (Runtime Library)
 
|runtime=block
 
|runtime=block
}}<!-- {{DISPLAYTITLE:Date and Time ({{OOo}} Runtime Library) -->
+
}}
 
+
= Date and Time =
+
{{AOo}} Basic provides the <tt>Date</tt> data type, which saves the date and time details in binary format.
 
+
{{OOo}} Basic provides the <tt>Date</tt> data type, which saves the date and time details in binary format.
+
  
 
== Specification of Date and Time Details within the Program Code ==
 
== Specification of Date and Time Details within the Program Code ==
Line 15: Line 14:
 
You can assign a date to a date variable through the assignment of a simple string:
 
You can assign a date to a date variable through the assignment of a simple string:
  
Dim MyDate As Date
+
<syntaxhighlight lang="oobas">
MyDate = "1.1.2002"
+
Dim MyDate As Date
 +
MyDate = "24.1.2002"
 +
</syntaxhighlight>
  
This assignment can function properly because {{OOo}} Basic automatically converts the date value defined as a string into a date variable. This type of assignment, however, can cause errors, date and time values are defined and displayed differently in different countries.
+
This assignment can function properly because {{AOo}} Basic automatically converts the date value defined as a string into a date variable. This type of assignment, however, can cause errors, date and time values are defined and displayed differently in different countries.
  
Since {{OOo}} Basic uses the country-specific settings of the operating system when converting a string into a date value, the expression shown previously only functions correctly if the country-specific settings match the string expression.
+
Since {{AOo}} Basic uses the country-specific settings of the operating system when converting a string into a date value, the expression shown previously only functions correctly if the country-specific settings match the string expression.
  
 
To avoid this problem, the <tt>DateSerial</tt> function should be used to assign a fixed value to a date variable:
 
To avoid this problem, the <tt>DateSerial</tt> function should be used to assign a fixed value to a date variable:
  
Dim MyVar As Date
+
<syntaxhighlight lang="oobas">
MyDate = DateSerial (2001, 1, 1)
+
Dim MyVar As Date
 +
MyDate = DateSerial (2001, 1, 24)
 +
</syntaxhighlight>
  
 
The function parameter must be in the sequence: year, month, day. The function ensures that the variable is actually assigned the correct value regardless of the country-specific settings  
 
The function parameter must be in the sequence: year, month, day. The function ensures that the variable is actually assigned the correct value regardless of the country-specific settings  
Line 31: Line 34:
 
The <tt>TimeSerial</tt> function formats time details in the same way that the <tt>DateSerial</tt> function formats dates:
 
The <tt>TimeSerial</tt> function formats time details in the same way that the <tt>DateSerial</tt> function formats dates:
  
Dim MyVar As Date
+
<syntaxhighlight lang="oobas">
MyDate =  TimeSerial(11, 23, 45)
+
Dim MyVar As Date
 +
MyDate =  TimeSerial(11, 23, 45)
 +
</syntaxhighlight>
  
 
Their parameters should be specified in the sequence: hours, minutes, seconds.
 
Their parameters should be specified in the sequence: hours, minutes, seconds.
Line 40: Line 45:
 
The following functions form the counterpart to the <tt>DateSerial</tt> and <tt>TimeSerial</tt> functions:
 
The following functions form the counterpart to the <tt>DateSerial</tt> and <tt>TimeSerial</tt> functions:
  
;<tt>Day(MyDate)</tt>:returns the day of the month from <tt>MyDate</tt>
+
;<tt>Day(MyDate)</tt>:returns the day of the month from <tt>MyDate</tt>.
;<tt>Month(MyDate)</tt>:returns the month from <tt>MyDate</tt>
+
;<tt>Month(MyDate)</tt>:returns the month from <tt>MyDate</tt>.
;<tt>Year(MyDate)</tt>:returns the year from <tt>MyDate</tt>
+
;<tt>Year(MyDate)</tt>:returns the year from <tt>MyDate</tt>.
;<tt>Weekday(MyDate)</tt>:returns the number of the weekday from <tt>MyDate</tt>
+
;<tt>Weekday(MyDate)</tt>:returns the number of the weekday from <tt>MyDate</tt>.
;<tt>Hour(MyTime)</tt>:returns the hours from <tt>MyTime</tt>
+
;<tt>Hour(MyTime)</tt>:returns the hours from <tt>MyTime</tt>.
;<tt>Minute(MyTime)</tt>:returns the minutes from <tt>MyTime</tt>
+
;<tt>Minute(MyTime)</tt>:returns the minutes from <tt>MyTime</tt>.
;<tt>Second(MyTime)</tt>:returns the seconds from <tt>MyTime</tt>
+
;<tt>Second(MyTime)</tt>:returns the seconds from <tt>MyTime</tt>.
  
These functions extract the date or time sections from a specified <tt>Date</tt> variable. The following example checks whether the date saved in <tt>MyDate</tt> is in the year 2003.  
+
These functions extract the date or time sections from a specified <tt>Date</tt> variable. The following example checks whether the date saved in <tt>MyDate</tt> is in the year 2023.  
  
Dim MyDate As Date
+
<syntaxhighlight lang="oobas">
' ... Initialization of MyDate
+
Dim MyDate As Date
+
' ... Initialization of MyDate
If Year(MyDate) = 2003 Then
+
 
  ' ... Specified date is in the year 2003
+
If Year(MyDate) = 2023 Then
End If
+
  ' ... Specified date is in the year 2023
 +
End If
 +
</syntaxhighlight>
  
 
In the same way, the following example checks whether <tt>MyTime</tt> is between 12 and 14 hours.
 
In the same way, the following example checks whether <tt>MyTime</tt> is between 12 and 14 hours.
  
Dim MyTime As Date
+
<syntaxhighlight lang="oobas">
' ... Initialization of MyTime
+
Dim MyTime As Date
+
' ... Initialization of MyTime
If Hour(MyTime) >= 12 And Hour(MyTime) < 14 Then
+
 
  ' ... Specified time is between 12 and 14 hours
+
If Hour(MyTime) >= 12 And Hour(MyTime) < 14 Then
End If
+
  ' ... Specified time is between 12 and 14 hours
 +
End If
 +
</syntaxhighlight>
  
 
The <tt>Weekday</tt> function returns the number of the weekday for the transferred date:  
 
The <tt>Weekday</tt> function returns the number of the weekday for the transferred date:  
  
Dim MyDate As Date
+
<syntaxhighlight lang="oobas">
Dim MyWeekday As String
+
Dim MyDate As Date
' ... initialize MyDate
+
Dim MyWeekday As String
+
' ... initialize MyDate
Select Case WeekDay(MyDate)
+
 
  case 1
+
Select Case WeekDay(MyDate)
    MyWeekday = "Sunday"
+
  case 1
  case 2
+
    MyWeekday = "Sunday"
    MyWeekday = "Monday"
+
  case 2
  case 3
+
    MyWeekday = "Monday"
    MyWeekday = "Tuesday"
+
  case 3
  case 4
+
    MyWeekday = "Tuesday"
    MyWeekday = "Wednesday"
+
  case 4
  case 5
+
    MyWeekday = "Wednesday"
    MyWeekday = "Thursday"
+
  case 5
  case 6
+
    MyWeekday = "Thursday"
    MyWeekday = "Friday"
+
  case 6
  case 7
+
    MyWeekday = "Friday"
    MyWeekday = "Saturday"
+
  case 7
End Select
+
    MyWeekday = "Saturday"
 +
End Select
 +
</syntaxhighlight>
  
{{Documentation/Note|Sunday is considered the first day of the week.}}
+
{{Note|Sunday is considered the first day of the week.}}
  
 
== Retrieving System Date and Time ==
 
== Retrieving System Date and Time ==
  
The following functions are available in {{OOo}} Basic to retrieve the system time and system date:
+
The following functions are available in {{AOo}} Basic to retrieve the system time and system date:
 
+
;<tt>Date</tt>:returns the present date
+
;<tt>Time</tt>:returns the present time
+
;<tt>Now</tt>:returns the present point in time (date and time as combined value)
+
  
 +
;<tt>Date</tt>:returns the present date as a string. The format depends on localization settings.
 +
;<tt>Time</tt>:returns the present time as a string.
 +
;<tt>Now</tt>:returns the present point in time (date and time) as a combined value of type <tt>Date</tt>.
  
 +
 +
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Date and Time (Runtime Library)}}
 
{{PDL1}}
 
{{PDL1}}

Latest revision as of 12:10, 30 January 2021


Apache OpenOffice Basic provides the Date data type, which saves the date and time details in binary format.

Specification of Date and Time Details within the Program Code

You can assign a date to a date variable through the assignment of a simple string:

Dim MyDate As Date
MyDate = "24.1.2002"

This assignment can function properly because Apache OpenOffice Basic automatically converts the date value defined as a string into a date variable. This type of assignment, however, can cause errors, date and time values are defined and displayed differently in different countries.

Since Apache OpenOffice Basic uses the country-specific settings of the operating system when converting a string into a date value, the expression shown previously only functions correctly if the country-specific settings match the string expression.

To avoid this problem, the DateSerial function should be used to assign a fixed value to a date variable:

Dim MyVar As Date
MyDate = DateSerial (2001, 1, 24)

The function parameter must be in the sequence: year, month, day. The function ensures that the variable is actually assigned the correct value regardless of the country-specific settings

The TimeSerial function formats time details in the same way that the DateSerial function formats dates:

Dim MyVar As Date
MyDate =  TimeSerial(11, 23, 45)

Their parameters should be specified in the sequence: hours, minutes, seconds.

Extracting Date and Time Details

The following functions form the counterpart to the DateSerial and TimeSerial functions:

Day(MyDate)
returns the day of the month from MyDate.
Month(MyDate)
returns the month from MyDate.
Year(MyDate)
returns the year from MyDate.
Weekday(MyDate)
returns the number of the weekday from MyDate.
Hour(MyTime)
returns the hours from MyTime.
Minute(MyTime)
returns the minutes from MyTime.
Second(MyTime)
returns the seconds from MyTime.

These functions extract the date or time sections from a specified Date variable. The following example checks whether the date saved in MyDate is in the year 2023.

Dim MyDate As Date
' ... Initialization of MyDate
 
If Year(MyDate) = 2023 Then
  ' ... Specified date is in the year 2023
End If

In the same way, the following example checks whether MyTime is between 12 and 14 hours.

Dim MyTime As Date
' ... Initialization of MyTime
 
If Hour(MyTime) >= 12 And Hour(MyTime) < 14 Then
  ' ... Specified time is between 12 and 14 hours
End If

The Weekday function returns the number of the weekday for the transferred date:

Dim MyDate As Date
Dim MyWeekday As String
' ... initialize MyDate
 
Select Case WeekDay(MyDate)
  case 1
    MyWeekday = "Sunday"
  case 2
    MyWeekday = "Monday"
  case 3
    MyWeekday = "Tuesday"
  case 4
    MyWeekday = "Wednesday"
  case 5
    MyWeekday = "Thursday"
  case 6
    MyWeekday = "Friday"
  case 7
    MyWeekday = "Saturday"
End Select
Documentation note.png Sunday is considered the first day of the week.

Retrieving System Date and Time

The following functions are available in Apache OpenOffice Basic to retrieve the system time and system date:

Date
returns the present date as a string. The format depends on localization settings.
Time
returns the present time as a string.
Now
returns the present point in time (date and time) as a combined value of type Date.


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools