ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / Date and time functions
  • Validity of the dates
  • Calculating the difference between two dates with the operators
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
Calculates the difference between two dates and times.
Remarks:
  • You can also use the '-' (minus) operator to calculate the difference between two dates (see Remarks).
  • To display the result of this function in a Duration variable, use StringToDuration.
Example
// Time elapsed since 01/01/1998 at 12:15
Diff is string
Diff = DateTimeDifference("199801011215", SysDateTime())
// Display time elapsed
Info("Time elapsed: " + CR + ...
Left(Diff, 8) + "days" + CR + ...
Middle(Diff, 9, 2) + "hours" + CR + ...
Middle(Diff, 11, 2) + "minutes" + CR + ...
Middle(Diff, 13, 2) + "seconds" + CR + ...
Middle(Diff, 15, 2) + "hundredths of a second")
// To fill a duration from the result of DateTimeDifference
Diff is string = DateTimeDifference("199801011215", SysDateTime())
// convert the difference into a duration variable
Duration1 is Duration = StringToDuration(Diff, durationCenti)

// --------------------------------------------
// Other possible solution
// DateTime1 is DateTime = "199801011215"
// DateTime2 is DateTime = DateTimeSys()
// Duration1 is Duration = DateTime2 - DateTime1
// ----------------------------------------------

// Display time elapsed
Info("Time elapsed: " + Duration1.Day + "days" + CR + ...
Duration1.Hour + "hours" + CR + ...
Duration1.Minute + "minutes" + CR + ...
Duration1.Second + "seconds" + CR + ...
Duration1.Millisecond + "thousandths of a second")
Syntax
<Result> = DateTimeDifference(<Start date/time> , <End date/time>)
<Result>: Character string
Numbers of days, hours, minutes and seconds elapsed between two dates in +/-DDDDDDHHMMSSCC format where:
  • DDDDDD is the number of days elapsed between the two specified "Date - Time",
  • HH is the number of hours,
  • MM is the number of minutes,
  • SS is the number of seconds,
  • CC is the number of hundredths of a second.
This result comes from the following operation: <End date/time> - <Start date/time>.
This result contains the '-' sign if the <Start date/time> is later than the <End date/time>.
Caution: To assign this result to a Duration variable, use StringToDuration with the durationCenti constant.
<Start date/time>: Character string or DateTime variable
Start date/time in the following format:
  • YYYYMMDDHHmmSSCC
  • YYYYMMDDHHmmSS
  • YYYYMMDDHHmm
  • YYYYMMDDHH
<End date/time>: Character string or DateTime variable
End date/time in the following format:
  • YYYYMMDDHHmmSSCC
  • YYYYMMDDHHmmSS
  • YYYYMMDDHHmm
  • YYYYMMDDHH
Remarks

Validity of the dates

The validity of the dates and times passed as parameters is checked. A message is displayed if the date or time is invalid. A "Date - Time" is considered invalid if:
  • The date is invalid. You can check the validity of a date using DateValid.
  • The time is invalid. You can check the validity of a time using TimeValid.
This function cannot be used to calculate the difference between two dates before October 14, 1582 (the change from the Julian to the Gregorian calendar will not be taken into account).
The date storage format allows you to store dates from 01/01/0001 to 12/31/9999.
WLanguage functions and WLanguage properties make accurate calculations on dates from January 1st, 1583.

Calculating the difference between two dates with the operators

The DateTimeDifference function can be replaced by the '-' operator. You can also assign the result directly to a variable of type Duration, which allows you to use a negative duration.
Examples:
interval_duration is Duration
IF date_time_1 > date_time_2 THEN
interval_duration = date_time_1 - date_time_2
ELSE
interval_duration = date_time_2 - date_time_1
END
interval_duration is Duration = date_time_1 - date_time_2
IF interval_duration < 0 THEN
interval_duration = - interval_duration
END
Business / UI classification: Neutral code
Component: wd290std.dll
Minimum version required
  • Version 9
This page is also available for…
Comments
Video DateTimeDifference
https://youtu.be/c-Ih6WpX4Ws

https://windevdesenvolvimento.blogspot.com/2019/05/dicas-2106-windev-webdev-mobile-data-29.html

// EDT_BTN_cALCULAR_DIAS

diferencia is string = DateTimeDifference(EDT_DATA_INICIAL_1, EDT_DATA_FINAL_1)
duracao is Duration=StringToDuration(diferencia, durationCenti)
EDT_dias_1=duracao..Day
amarildo
11 May 2019
Display the result of DateTimeDifference

DateTimeLOGIN = Table.MyLOGIN
DateTimeLOGOUT = Table.MyLOGOUT
MyDuration = DateTimeDifference(DateTimeLOGIN,DateTimeLOGOUT)+"0"
Table.MyDuration = MyDuration

Adding the "0" to the result of the calculation is necessary to get a correct display!

Guenter
29 May 2018
Example
// Time passed since 01/01/1998 at 12:15
sTDatahoraAgenda is string = Middle(StringToDate(EDT_DataAgendamento)+StringToTime(Middle(EDT_DataAgendamento,12,5)),1,12) //"199801011215"
Diff is string
Diff = DateTimeDifference(sTDatahoraAgenda, DateSys() + TimeSys())
//Display the time passed
//Info("Time passed: " + CR + ...
//Left(Diff, 8) + "days" + CR + ...
//Middle(Diff, 9, 2) + "hours" + CR + ...
//Middle(Diff, 11, 2) + "minutes" + CR + ...
//Middle(Diff, 13, 2) + "seconds" + CR + ...
//Middle(Diff, 15, 2) + "hundredths of a second")

//Valida data
nSMinutos is int = Val(Middle(Diff, 11, 2))
IF CBOX_Agendamento..Value = True AND Middle(Diff, 11, 2) < 30
Info("A data e hora agendada tem que ser maior do que 30 minutos")
ok1 = False
ELSE
ok1 = True
END
adrianoboller
27 Nov. 2015

Last update: 03/07/2024

Send a report | Local help