User Tools

Site Tools


k9:k9.3:start

9.3 Date and time functions

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).

9.3.1 Overview of date and time functions

FunctionData typeDescription
Date (expression)DateReturns only the date of an expression that can be interpreted as a date.
Time (expression)DateReturns only the time of an expression that can be interpreted as a date.
DateAdd (Date as Date, Period AS Integer, Count AS Integer)DateAdds 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)IntegerReturns the number of the specified period between two date values.
Year (Date AS Date)IntegerReturns the year of a date as an integer number.
Month (Date AS Date)IntegerReturns the month of a date as an integer number between 1 and 12.
Day (Date AS Date) IntegerReturns the day of a date as an integer number between 1 and 31.
Hour (Date AS Date)IntegerReturns the hour of a date as an integer number between 0 and 23.
Minute (Date AS Date)IntegerReturns the minutes of a date as an integer number between 0 and 59.
Second (Date AS Date) AS IntegerIntegerReturns the seconds of a date as an integer number between 0 and 59.
Week (Date AS Date[, FirstDayOfWeek AS Integer, FullWeek AS Boolean]IntegerReturns 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) IntegerReturns the day of the week as an integer number between 0 and 6.
Now[ () ]DateReturns the current date and time.
Timer[ () ]FloatReturns 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:

  • IsDate (String) to check whether a string can be safely interpreted as a date,
  • CDate (Expression AS Variant) to convert a number (float) or string into a date string (without taking into account the localization setting),
  • Val (String), with which you can convert a string to a date, taking into account the localization setting, or
  • Str (expression) to convert a date into a string. This function also takes the localization settings into account!

Detailed information on these 4 functions and other conversion functions can be found in? chapter 9.10 Conversion functions.

9.3.2 Notes

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
  • Date is the start date,
  • Period is the type of time interval: year, month, day, day,….,
  • Count = c is the number of time intervals to be added. The default is c = 1.

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
  • It applies: Date_1 ≤ Date_2
  • The date 1 is before the date 2,
  • Period is the type of time interval.

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 aDaysListe As String[]
Dim dDate As Date

aDaysListe = Split("Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday", ",")
dDate = Now()
Print "Today is the "; aDaysListe[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!

9.3.3.3 Leap years

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 ' btnLeapYear_Click()

The console of the IDE shows up:

2008: True
2014: False
1500: False
2000: True

9.3.4 Timestamp - Unix-TimeStamp

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); " Seconds"

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”.

9.3.5 Valid date and time values

You can use various controls to generate a date. The next overview shows the control and the chapter describing the control.

  • DateChooser → Chapter 17.13
  • ValueBox → Chapter 16.9
  • MaskBox → Chapter 16.7
  • ButtonBox → Chapter 16.8.2 (with mask)
  • DateBox → Chapter 16.6.2
  • Input field → Chapter 19.6.5.2 Checking the syntax of character strings

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.

9.3.6 Examples

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 aDaysListe As String[]
Dim dDate As Date

aDaysListe = Split("Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday", ",")
dDate = Now()
Print "Today is "; aDaysListe[WeekDay(dDate)]; "."
Print "Today is "; Format$(Now(), "dddd.")
dDate = DateAdd(Now, gb.day, 7 - WeekDay(Now))
Print "Next Sunday falls on "; Format$(dDate, "dd.mm.yyyy"); "!"
Print "Until Christmas there are still "; DateDiff(Now(), Date(Year(Now()), 12, 24), gb.Day); " Days."

Print "UNIX-Timestamp (current):     "; DateDiff(CDate("1/1/1970"), Now(), gb.Second); " Seconds"
Print "UNIX-TimeStampToDate (current): "; DateAdd(CDate("1/1/1970"), gb.Second, DateDiff(CDate("1/1/1970"), Now(), gb.Second))
Print "UNIX-Timestamp from 5.6.2014:  "; DateDiff(CDate("1/1/1970"), Date(2014, 6, 5), gb.Second); " Seconds"

Print "Today is "; Format$(Now(), "dddd"); ", the "; Format$(Now(), "d. mmmm yyyy.")
Print "It was "; Format$(Now, "hh:nn"); " o'clock!"
Print "It was exactly "; Format$(Now, "hh:nn:ss.uuu"); " o'clock!"
Print "It was "; Format$(Now, "hh:nn"); " o'clock"; Format$(Now, " (t)")
Print "Time = "; Format(Now(), "hh:nn:ss"); " o'clock!"  ' Current kitchen time
Print "UTC  = "; Format(Time(DateAdd(Now(), gb.second, System.TimeZone)), "hh:nn:ss"); " o'clock"
Print "Current calendar week: "; Week(Now(), gb.Monday, True)
Print "You are currently "; DateDiff(Date(1949, 5, 2), Now(), gb.Year); " Years old."
Today is Thursday.
Today is Thursday.
The next Sunday falls on 08.06.2014!
There are 202 days left until Christmas.
UNIX timestamp (current): 1401964266 seconds
UNIX TimeStampToDate (current): 05.06.2014 10:31:06
UNIX timestamp from 5.6.2014: 1401926400 seconds
Today is Thursday, 5 June 2014.
It was 10:31!
It was exactly 10:31:06.349!
It was 10:31 (CET)
Time = 10:31:06!
UTC = 09:31:06
Current calendar week: 22
You are currently 65 years old.
The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k9/k9.3/start.txt · Last modified: 11.02.2022 (external edit)

Page Tools