For working with date and time values, Gambas not only provides you with the current time stamp, but also functions for generating a date or isolating individual date components such as month or year or weekday or calendar week from a given date or format functions for outputting date and time in freely selectable formats.
Some components for the input or output of date or time values work with valid values for date and time (DateChooser) - others require you to check for a valid date or time (ValueBox).
Function | Data type | Description |
---|---|---|
Date (expression) | Date | Returns only the date of an expression that can be interpreted as a date. |
Time (expression) | Date | Returns only the time of an expression that can be interpreted as a date. |
DateAdd (Date as Date, Period AS Integer, Count AS Integer) | Date | Adds the multiple of a specified period to a given date and returns the new date |
DateDiff (Date1 AS Date, Date2 AS Date, Period AS Integer) | Integer | Returns the number of the specified period between two date values. |
Year (Date AS Date) | Integer | Returns the year of a date as an integer number. |
Month (Date AS Date) | Integer | Returns the month of a date as an integer number between 1 and 12. |
Day (Date AS Date) | Integer | Returns the day of a date as an integer number between 1 and 31. |
Hour (Date AS Date) | Integer | Returns the hour of a date as an integer number between 0 and 23. |
Minute (Date AS Date) | Integer | Returns the minutes of a date as an integer number between 0 and 59. |
Second (Date AS Date) AS Integer | Integer | Returns the seconds of a date as an integer number between 0 and 59. |
Week (Date AS Date[, FirstDayOfWeek AS Integer, FullWeek AS Boolean] | Integer | Returns the calendar week as integer number between 0 and 52. The function value is influenced by the parameters for specifying the first day of the week and the full week. |
WeekDay (Date AS Date) | Integer | Returns the day of the week as an integer number between 0 and 6. |
Now[ () ] | Date | Returns the current date and time. |
Timer[ () ] | Float | Returns the number of seconds after program start as a real number. |
Table 9.3.1.1: Overview of date and time functions
Of particular interest will be the Date () function, which you can use to generate a valid date from the year, month and day. If you also specify the optional arguments hours, minutes, seconds and milliseconds, then the time values in the date are also set, otherwise they are set to 00:
Datum = Date ( Year , Month , Day [ , hours, minutes, seconds, milliseconds ] ) AS Date
In connection with the Date () function, you will also appreciate these functions:
Detailed information on these 4 functions and other conversion functions can be found in? chapter 9.10 Conversion functions.
You can use this function to calculate a new date from a start date and the added time interval:
DateAdd ( Date as Date , Period AS Integer , Count AS Integer ) AS Date
List of constants for time intervals (period):
Constant effect ---------------------------------------------------------------------------------------------------------------------------- gb.Second Added c seconds (seconds). gb.Minute Added c minutes (minutes). gb.Hour Added c hours (hours). gb.Day Added c days (days). gb.Week Added c weeks (weeks). gb.WeekDay Added c Weekdays (Saturday and Sunday are ignored). gb.Month Added c months (months). gb.Quarter Added c Quarterly (quarters). gb.Year Added c years (years).
You can use this function to calculate differences between date values:
DateDiff ( Date_1 AS Date , Date_2 AS Date , Period AS Integer ) AS Integer
The function value of DateDiff(…) for the time difference is the number of time intervals between date1 and date2. only complete intervals are counted; internally, it is rounded off. The time interval constants are the same as for the DateAdd () function.
With the WeekDay (..)function for determining the weekday from a date
WeekDay ( Date AS Date ) AS Integer
Constant Weekday Return value ---------------------------------------------------------------------------------------------------------------------------- gb.Sunday Sunday 0 gb.Monday Monday 1 cb.Tuesday Tuesday 2 gb.Wednesday Wednesday 3 gb.Thursday Thursday 4 gb.Friday Friday 5 gb.Saturday Saturday 6
Examples:
Dim aTagesListe As String[] Dim dDate As Date aTagesListe = Split("Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday", ",") dDate = Now() Print "Today is the "; aTagesListe[WeekDay(dDate)]; " ("; Format$(dDate, "dd.mm.yyyy"); ")." dDate = DateAdd(Now, gb.day, 7 - WeekDay(Now)) Print "The next Sunday falls on the "; Format$(dDate, "dd.mm.yyyy"); "!"
Output in the console of the IDE:
Today is the Sunday (01.06.2014). The next Sunday falls on the 08.06.2014!
Whether a year is a leap year can be easily checked with the following source code excerpts:
Public Function IsLeapYear(iYear As Integer) As Boolean Return (iYear >= 1583 AND ((iYear Mod 4 = 0 And iYear Mod 100 <> 0) Or (iYear Mod 400 = 0))) End ' IsLeapYear(..) Public Sub btnSchaltjahr_Click() Print "2008: "; IsLeapYear(2008) Print Year(Now()); ": "; IsLeapYear(Year(Now())) Print "1500: "; IsLeapYear(1500) Print "2000: "; IsLeapYear(2000) End ' btnSchaltjahr_Click()
The console of the IDE shows up:
2008: True 2014: False 1500: False 2000: True
A Unix timestamp (Unix-TimeStamp) is a number representing the number of seconds since January 1,1970.
To convert the current (gambas) date to a Unix timestamp:
Print DateDiff(CDate("1/1/1970"), Now(), gb.Second) Print "UNIX-TimeStamp : "; DateDiff(CDate("1/1/1970"), Now(), gb.Second); " Sekunden"
This is how to convert a Unix timestamp into a (gambas) date:
Print DateAdd(CDate("1/1/1970"), UnixTimeStamp, gb.Second)
On the website http://www.timeanddate.com/worldclock/germany/berlin you will find interesting information about date, time and time zones. You can also find the time stamp in constants in connection with MySQL and its data types: TimeStamp As String = “TIMESTAMP”.
You can use various controls to generate a date. The next overview shows the control and the chapter describing the control.
There is a favorite of all controls: It is the DateBox, because the DateBox as a specialized button box with a date/time mask sets besides the input and display of a date on the date/time selection dialog in the DateChooser.
If you want to use date and time values directly in the source text, you should look at the examples in the next section.
You should use the following examples in a small project and vary the arguments and constants many times. Many issues are additionally formatted in different ways. The description of the format function Format (…) can be found in? chapter 9.1 and an overview of format definitions for date and time values in? chapter 9.2.
Dim aTagesListe As String[] Dim dDate As Date aTagesListe = Split("Sonntag,Montag,Dienstag,Mittwoch,Donnerstag,Freitag,Samstag", ",") dDate = Now() Print "Heute ist "; aTagesListe[WeekDay(dDate)]; "." Print "Heute ist "; Format$(Now(), "dddd.") dDate = DateAdd(Now, gb.day, 7 - WeekDay(Now)) Print "Der nächste Sonntag fällt auf den "; Format$(dDate, "dd.mm.yyyy"); "!" Print "Bis Weihnachten sind noch "; DateDiff(Now(), Date(Year(Now()), 12, 24), gb.Day); " Tage." Print "UNIX-Zeitstempel (aktuell): "; DateDiff(CDate("1/1/1970"), Now(), gb.Second); " Sekunden" Print "UNIX-TimeStampToDate (aktuell): "; DateAdd(CDate("1/1/1970"), gb.Second, DateDiff(CDate("1/1/1970"), Now(), gb.Second)) Print "UNIX-Zeitstempel vom 5.6.2014: "; DateDiff(CDate("1/1/1970"), Date(2014, 6, 5), gb.Second); " Sekunden" Print "Heute ist "; Format$(Now(), "dddd"); ", der "; Format$(Now(), "d. mmmm yyyy.") Print "Es war "; Format$(Now, "hh:nn"); " Uhr!" Print "Es war genau "; Format$(Now, "hh:nn:ss.uuu"); " Uhr!" Print "Es war "; Format$(Now, "hh:nn"); " Uhr"; Format$(Now, " (t)") Print "Zeit = "; Format(Now(), "hh:nn:ss"); " Uhr!" ' Aktuelle Küchen-Zeit Print "UTC = "; Format(Time(DateAdd(Now(), gb.second, System.TimeZone)), "hh:nn:ss"); " Uhr" Print "Aktuelle Kalenderwoche: "; Week(Now(), gb.Monday, True) Print "Sie sind gegenwärtig "; DateDiff(Date(1949, 5, 2), Now(), gb.Year); " Jahre alt."
Heute ist Donnerstag. Heute ist Donnerstag. Der nächste Sonntag fällt auf den 08.06.2014! Bis Weihnachten sind noch 202 Tage. UNIX-Zeitstempel (aktuell): 1401964266 Sekunden UNIX-TimeStampToDate (aktuell): 05.06.2014 10:31:06 UNIX-Zeitstempel vom 5.6.2014: 1401926400 Sekunden Heute ist Donnerstag, der 5. Juni 2014. Es war 10:31 Uhr! Es war genau 10:31:06.349 Uhr! Es war 10:31 Uhr (CET) Zeit = 10:31:06 Uhr! UTC = 09:31:06 Uhr Aktuelle Kalenderwoche: 22 Sie sind gegenwärtig 65 Jahre alt.
Gambas stellt Ihnen für die Arbeit mit Datums- und Zeitwerten nicht nur den aktuellen Zeitstempel zur Verfügung sondern auch Funktionen u.a. zum Generieren eines Datums oder zur Isolation einzelner Datumskomponenten wie zum Beispiel Monat oder Jahr oder Wochentag oder Kalenderwoche aus einem gegebenen Datum oder Format-Funktionen zur Ausgabe von Datum und Zeit in frei wählbaren Formaten.
Einige Komponenten zur Ein- oder Ausgabe von Datum oder Zeit-Werten arbeiten mit validen Werten für Datum und Zeit (DateChooser) – bei anderen müssen Sie selbst prüfen, ob ein valides Datum oder eine valide Zeit vorliegt (ValueBox).
Funktion | Datentyp | Beschreibung |
---|---|---|
Date ( Expression ) | Date | Gibt nur das Datum eines Ausdrucks zurück, der als Datum interpretiert werden kann. |
Time ( Expression ) | Date | Gibt nur die Zeit eines Ausdrucks zurück, der als Datum interpretiert werden kann. |
DateAdd ( Date as Date , Period AS Integer , Count AS Integer ) | Date | Addiert das Vielfache einer spezifizierten Periode zu einem gegebenen Datum und gibt das neue Datum zurück |
DateDiff ( Date1 AS Date , Date2 AS Date , Period AS Integer ) | Integer | Gibt die Anzahl der spezifizierten Periode zwischen zwei Datumswerten zurück. |
Year ( Date AS Date ) | Integer | Gibt das Jahr eines Datums als Integer-Zahl zurück. |
Month ( Date AS Date ) | Integer | Gibt den Monat eines Datums als Integer-Zahl zwischen 1 und 12 zurück. |
Day ( Date AS Date ) | Integer | Gibt den Tag eines Datums als Integer-Zahl zwischen 1 und 31 zurück. |
Hour ( Date AS Date ) | Integer | Gibt die Stunde eines Datums als Integer-Zahl zwischen 0 und 23 zurück. |
Minute ( Date AS Date ) | Integer | Gibt die Minuten eines Datums als Integer-Zahl zwischen 0 und 59 zurück. |
Second ( Date AS Date ) AS Integer | Integer | Gibt die Sekunden eines Datums als Integer-Zahl zwischen 0 und 59 zurück. |
Week ( Date AS Date [ , FirstDayOfWeek AS Integer , FullWeek AS Boolean ] ) | Integer | Gibt die Kalender-Woche als Integer-Zahl zwischen 0 und 52 zurück. Der Funktionswert wird durch die Parameter zur Angabe des ersten Wochentages und zur vollen Woche beeinflusst. |
WeekDay ( Date AS Date ) | Integer | Gibt den Wochentag als Integer-Zahl zwischen 0 und 6 zurück. |
Now [ ( ) ] | Date | Gibt das aktuelle Datum und die aktuelle Zeit zurück. |
Timer [ ( ) ] | Float | Gibt die Anzahl der Sekunden nach dem Programmstart als reelle Zahl zurück. |
Tabelle 9.3.1.1: Übersicht Datums- und Zeitfunktionen
Von besonderem Interesse wird die Date()-Funktion sein, mit der Sie aus der Angabe des Jahres, des Monats und des Tages ein valides Datum generieren können. Wenn Sie auch die optionalen Argumente Stunden, Minuten, Sekunden und Millisekunden angeben, dann werden auch die Zeitwerte im Datum gesetzt, sonst werden diese auf 00 gesetzt:
Datum = Date ( Jahr , Monat , Tag [ , Stunden , Minuten , Sekunden, Millisekunden ] ) AS Date
Im Zusammenhang mit der Date()-Funktion werden Sie auch diese Funktionen schätzen:
Ausführliche Informationen zu diesen 4 Funktionen und weiteren Konvertierungsfunktionen können Sie im → Kapitel 9.10 Konvertierungsfunktionen nachlesen.
Für die Berechnung eines neuen Datums aus einem Start-Datum und dem addierten Zeit-Intervall können Sie diese Funktion einsetzen:
DateAdd ( Date as Date , Period AS Integer , Count AS Integer ) AS Date
Liste der Konstanten für Zeitintervalle (Period):
Konstante Effekt ---------------------------------------------------------------------------------------------------------------------------- gb.Second Addiert c Sekunden (seconds). gb.Minute Addiert c Minuten (minutes). gb.Hour Addiert c Stunden (hours). gb.Day Addiert c Tage (days). gb.Week Addiert c Wochen (weeks). gb.WeekDay Addiert c Wochentage (Samstag und Sonntag werden ignoriert). gb.Month Addiert c Monate (months). gb.Quarter Addiert c Vierteljahre (quarters). gb.Year Addiert c Jahre (years).
Für die Berechnung von Differenzen zwischen Datumswerten ist diese Funktion einsetzbar:
DateDiff ( Date_1 AS Date , Date_2 AS Date , Period AS Integer ) AS Integer
Der Funktionswert von DateDiff(..) für die Zeit-Differenz ist die Anzahl der Zeitintervalle zwischen Datum1 und Datum2. Es werden nur vollständige Intervalle gezählt; intern wird abgerundet. Die Konstanten für das Zeitintervall sind die gleichen wie für die DateAdd()-Funktion.
Bei der Funktion WeekDay(..) zur Ermittlung des Wochentages aus einem Datum
WeekDay ( Date AS Date ) AS Integer
ist zu beachten, dass eine Zahl zwischen 0 (Sonntag) und 6 (Samstag) zurückgegeben wird. Hier eine Liste der vordefinierten Konstanten, die den Wochentagen zugeordnet sind:
Konstante Wochentag Rückgabe-Wert ---------------------------------------------------------------------------------------------------------------------------- gb.Sunday Sonntag 0 gb.Monday Montag 1 gb.Tuesday Dienstag 2 gb.Wednesday Mittwoch 3 gb.Thursday Donnerstag 4 gb.Friday Freitag 5 gb.Saturday Samstag 6
Beispiele:
Dim aTagesListe As String[] Dim dDate As Date aTagesListe = Split("Sonntag, Montag, Dienstag, Mittwoch, Donnerstag, Freitag, Samstag", ",") dDate = Now() Print "Heute ist "; aTagesListe[WeekDay(dDate)]; " ("; Format$(dDate, "dd.mm.yyyy"); ")." dDate = DateAdd(Now, gb.day, 7 - WeekDay(Now)) Print "Der nächste Sonntag fällt auf den "; Format$(dDate, "dd.mm.yyyy"); "!"
Ausgabe in der Konsole der IDE:
Heute ist Sonntag (01.06.2014). Der nächste Sonntag fällt auf den 08.06.2014!
Ob ein Jahr ein Schaltjahr ist, kann leicht mit den folgenden Quelltext-Ausschnitten überprüft werden:
Public Function IsLeapYear(iYear As Integer) As Boolean Return (iYear >= 1583 AND ((iYear Mod 4 = 0 And iYear Mod 100 <> 0) Or (iYear Mod 400 = 0))) End ' IsLeapYear(..) Public Sub btnSchaltjahr_Click() Print "2008: "; IsLeapYear(2008) Print Year(Now()); ": "; IsLeapYear(Year(Now())) Print "1500: "; IsLeapYear(1500) Print "2000: "; IsLeapYear(2000) End ' btnSchaltjahr_Click()
In der Konsole der IDE zeigt sich:
2008: True 2014: False 1500: False 2000: True
Ein Unix-Zeitstempel (Unix-TimeStamp) ist eine Zahl, welche die Anzahl der Sekunden seit dem 1. Januar 1970 repräsentiert.
So konvertieren Sie das aktuelle (Gambas-)Datum in einen Unix-Zeitstempel:
Print DateDiff(CDate("1/1/1970"), Now(), gb.Second) Print "UNIX-TimeStamp : "; DateDiff(CDate("1/1/1970"), Now(), gb.Second); " Sekunden"
So funktioniert die Umwandlung eines Unix-Zeitstempels in ein (Gambas-)Datum:
Print DateAdd(CDate("1/1/1970"), UnixTimeStamp, gb.Second)
Auf der Website http://www.timeanddate.com/worldclock/germany/berlin finden Sie Interessantes zu den Themen Datum, Zeit und Zeitzonen. Der Zeitstempel begegnet Ihnen auch im Zusammenhang mit MySQL und deren Daten-Typen in der Konstanten: TimeStamp As String = “TIMESTAMP”.
Um ein Datum zu generieren, können Sie verschiedene Steuerelemente einsetzen. In der nächsten Übersicht finden Sie das Steuerelement und das Kapitel, in dem das Steuerelement beschrieben wird.
Von allen Steuerelementen gibt es einen Favoriten: Es ist die DateBox, denn die DateBox als spezialisierte ButtonBox mit einer Datum/Zeit-Maske setzt neben der Eingabe und Anzeige eines Datums auf den Datum/Zeit-Auswahl-Dialog im DateChooser.
Wenn Sie Datums- und Zeit-Werte im Quelltext direkt verwenden wollen, dann sollten Sie sich die Beispiele im nächsten Abschnitt ansehen.
Die folgenden Beispiele sollten Sie in ein kleines Projekt übernehmen und die Argumente sowie Konstanten vielfach variieren. Viele Ausgaben werden zusätzlich in unterschiedlicher Weise formatiert. Die Beschreibung der Format-Funktion Format(..) finden Sie im → Kapitel 9.1 und eine Übersicht von Format-Festlegungen für Datums- und Zeit-Werte im → Kapitel 9.2.
Dim aTagesListe As String[] Dim dDate As Date aTagesListe = Split("Sonntag,Montag,Dienstag,Mittwoch,Donnerstag,Freitag,Samstag", ",") dDate = Now() Print "Heute ist "; aTagesListe[WeekDay(dDate)]; "." Print "Heute ist "; Format$(Now(), "dddd.") dDate = DateAdd(Now, gb.day, 7 - WeekDay(Now)) Print "Der nächste Sonntag fällt auf den "; Format$(dDate, "dd.mm.yyyy"); "!" Print "Bis Weihnachten sind noch "; DateDiff(Now(), Date(Year(Now()), 12, 24), gb.Day); " Tage." Print "UNIX-Zeitstempel (aktuell): "; DateDiff(CDate("1/1/1970"), Now(), gb.Second); " Sekunden" Print "UNIX-TimeStampToDate (aktuell): "; DateAdd(CDate("1/1/1970"), gb.Second, DateDiff(CDate("1/1/1970"), Now(), gb.Second)) Print "UNIX-Zeitstempel vom 5.6.2014: "; DateDiff(CDate("1/1/1970"), Date(2014, 6, 5), gb.Second); " Sekunden" Print "Heute ist "; Format$(Now(), "dddd"); ", der "; Format$(Now(), "d. mmmm yyyy.") Print "Es war "; Format$(Now, "hh:nn"); " Uhr!" Print "Es war genau "; Format$(Now, "hh:nn:ss.uuu"); " Uhr!" Print "Es war "; Format$(Now, "hh:nn"); " Uhr"; Format$(Now, " (t)") Print "Zeit = "; Format(Now(), "hh:nn:ss"); " Uhr!" ' Aktuelle Küchen-Zeit Print "UTC = "; Format(Time(DateAdd(Now(), gb.second, System.TimeZone)), "hh:nn:ss"); " Uhr" Print "Aktuelle Kalenderwoche: "; Week(Now(), gb.Monday, True) Print "Sie sind gegenwärtig "; DateDiff(Date(1949, 5, 2), Now(), gb.Year); " Jahre alt."
Heute ist Donnerstag. Heute ist Donnerstag. Der nächste Sonntag fällt auf den 08.06.2014! Bis Weihnachten sind noch 202 Tage. UNIX-Zeitstempel (aktuell): 1401964266 Sekunden UNIX-TimeStampToDate (aktuell): 05.06.2014 10:31:06 UNIX-Zeitstempel vom 5.6.2014: 1401926400 Sekunden Heute ist Donnerstag, der 5. Juni 2014. Es war 10:31 Uhr! Es war genau 10:31:06.349 Uhr! Es war 10:31 Uhr (CET) Zeit = 10:31:06 Uhr! UTC = 09:31:06 Uhr Aktuelle Kalenderwoche: 22 Sie sind gegenwärtig 65 Jahre alt.