//+------------------------------------------------------------------+ //| advanced.mq5 | //| Copyright © 2026, Amr Ali | //| https://www.mql5.com/en/users/amrali | //+------------------------------------------------------------------+ // First day of the week used by all week-related functions. // Optionally, override the default before including this header: // // #define TIMEUTILS_FIRST_DAY_OF_WEEK SUNDAY // #include "TimeUtils.mqh" // // Valid values: SUNDAY or MONDAY (default: MONDAY). #include "TimeUtils.mqh" int counter = 0; #define PRINT(A) Print((string)++counter + ". " + #A + " = ", (A)) void OnStart() { // The time to process const datetime t = TimeLocal(); PRINT( TimeToString(t, TIME_DATE|TIME_SECONDS) ); // MQL's native TimeToString() PRINT( t2s( t, TIME_DATE|TIME_SECONDS) ); // Formats time with the weekday name //+==================================================================+ //| Break datetime To Components | //+==================================================================+ MqlDateTime dt[1] = {}; PRINT( TimeToStructFast(t, dt[0]) ); ArrayPrint(dt); //#################################################################### //# # //# Construction # //# # //#################################################################### PRINT( DateFrom(2022, 03, 25) ); PRINT( StructToTimeFast(dt[0]) ); //#################################################################### //# # //# Calendar Components # //# # //#################################################################### //+==================================================================+ //| Extract Components of datetime: Sunday, yyyy.mm.dd hh:mm:ss | //| Get() Units | //+==================================================================+ PRINT( Second(t) ); PRINT( Minute(t) ); PRINT( Hour(t) ); PRINT( Day(t) ); PRINT( Month(t) ); PRINT( Year(t) ); PRINT( DateOnly(t) ); PRINT( TimeOnly(t) ); //+==================================================================+ //| Day() Number | //+==================================================================+ PRINT( DayOfWeek(t) ); PRINT( DayOfYear(t) ); //+==================================================================+ //| Week() Number | //+==================================================================+ PRINT( WeekOfMonth(t) ); PRINT( WeekOfYear(t) ); //+==================================================================+ //| Epoch-Based Indices | //+==================================================================+ PRINT( MinuteIndex(t) ); PRINT( HourIndex(t) ); PRINT( DayIndex(t) ); PRINT( WeekIndex(t) ); PRINT( MonthIndex(t) ); PRINT( QuarterIndex(t) ); PRINT( YearIndex(t) ); //+==================================================================+ //| Quarter() Number | //+==================================================================+ PRINT( Quarter(t) ); //+==================================================================+ //| HourOf() Units | //+==================================================================+ PRINT( HourOfWeek(t) ); PRINT( HourOfMonth(t) ); PRINT( HourOfQuarter(t) ); PRINT( HourOfYear(t) ); //+==================================================================+ //| MinuteOf() Units | //+==================================================================+ PRINT( MinuteOfDay(t) ); PRINT( MinuteOfWeek(t) ); PRINT( MinuteOfMonth(t) ); PRINT( MinuteOfQuarter(t) ); PRINT( MinuteOfYear(t) ); //+==================================================================+ //| SecsElapsedOf() Units | //+==================================================================+ PRINT( SecsElapsedOfMinute(t) ); PRINT( SecsElapsedOfHour(t) ); PRINT( SecsElapsedOfDay(t) ); PRINT( SecsElapsedOfWeek(t) ); PRINT( SecsElapsedOfMonth(t) ); PRINT( SecsElapsedOfQuarter(t) ); PRINT( SecsElapsedOfYear(t) ); PRINT( SecsElapsedOfEpoch(t) ); //#################################################################### //# # //# Adjusters / Navigation # //# # //#################################################################### //+==================================================================+ //| StartOf() Units | //+==================================================================+ PRINT( StartOfMinute(t) ); PRINT( StartOfHour(t) ); PRINT( StartOfDay(t) ); PRINT( StartOfWeek(t) ); PRINT( StartOfMonth(t) ); PRINT( StartOfQuarter(t) ); PRINT( StartOfYear(t) ); PRINT( StartOfToday() ); PRINT( StartOfTomorrow() ); PRINT( StartOfYesterday() ); //+==================================================================+ //| EndOf() Units | //+==================================================================+ PRINT( EndOfMinute(t) ); PRINT( EndOfHour(t) ); PRINT( EndOfDay(t) ); PRINT( EndOfWeek(t) ); PRINT( EndOfMonth(t) ); PRINT( EndOfQuarter(t) ); PRINT( EndOfYear(t) ); PRINT( EndOfToday() ); PRINT( EndOfTomorrow() ); PRINT( EndOfYesterday() ); //+==================================================================+ //| LastDayOf() Units | //+==================================================================+ PRINT( LastDayOfWeek(t) ); PRINT( LastDayOfMonth(t) ); PRINT( LastDayOfQuarter(t) ); PRINT( LastDayOfYear(t) ); //+==================================================================+ //| RoundTo() / Nearest() Units | //+==================================================================+ PRINT( RoundToMinute(t) ); PRINT( RoundToHour(t) ); PRINT( RoundToDay(t) ); PRINT( RoundToWeek(t) ); //+==================================================================+ //| CeilTo() / Next() Units | //+==================================================================+ PRINT( CeilToMinute(t) ); PRINT( CeilToHour(t) ); PRINT( CeilToDay(t) ); PRINT( CeilToWeek(t) ); //+==================================================================+ //| Next() Weekday | //+==================================================================+ PRINT( NextSunday(t) ); PRINT( NextMonday(t) ); PRINT( NextTuesday(t) ); PRINT( NextWednesday(t) ); PRINT( NextThursday(t) ); PRINT( NextFriday(t) ); PRINT( NextSaturday(t) ); //+==================================================================+ //| Previous() Weekday | //+==================================================================+ PRINT( PreviousSunday(t) ); PRINT( PreviousMonday(t) ); PRINT( PreviousTuesday(t) ); PRINT( PreviousWednesday(t) ); PRINT( PreviousThursday(t) ); PRINT( PreviousFriday(t) ); PRINT( PreviousSaturday(t) ); //+==================================================================+ //| Nth() Weekday Of The Month | //+==================================================================+ PRINT( FirstWeekdayOfMonth(t, SUNDAY) ); PRINT( LastWeekdayOfMonth(t, SUNDAY) ); //+==================================================================+ //| WithXxx() / Replace() Units | //+==================================================================+ PRINT( WithSecond(t, 05) ); PRINT( WithMinute(t, 30) ); PRINT( WithHour(t, 10) ); PRINT( WithDay(t, 26) ); PRINT( WithMonth(t, 2) ); PRINT( WithYear(t, 2024) ); PRINT( WithDayOfWeek(t, FRIDAY) ); PRINT( WithTime(t, 10, 30, 05) ); PRINT( WithDate(t, 2024, 2, 26) ); //#################################################################### //# # //# Addition and Subtraction # //# # //#################################################################### //+==================================================================+ //| Add() Units | //+==================================================================+ PRINT( AddSeconds(t, 30) ); PRINT( AddMinutes(t, 30) ); PRINT( AddHours(t, 2) ); PRINT( AddDays(t, 10) ); PRINT( AddBusinessDays(t, 10) ); PRINT( AddWeeks(t, 4) ); PRINT( AddMonths(t, 2) ); PRINT( AddQuarters(t, 3) ); PRINT( AddYears(t, 5) ); //+==================================================================+ //| Sub() Units | //+==================================================================+ PRINT( SubSeconds(t, 30) ); PRINT( SubMinutes(t, 30) ); PRINT( SubHours(t, 2) ); PRINT( SubDays(t, 10) ); PRINT( SubBusinessDays(t, 10) ); PRINT( SubWeeks(t, 4) ); PRINT( SubMonths(t, 2) ); PRINT( SubQuarters(t, 3) ); PRINT( SubYears(t, 5) ); //#################################################################### //# # //# Difference # //# # //#################################################################### //+==================================================================+ //| DifferenceIn() Units | //+==================================================================+ PRINT( DifferenceInCalendarDays( StartOfYear(t), EndOfYear(t)) ); PRINT( DifferenceInCalendarWeeks( StartOfYear(t), EndOfYear(t)) ); PRINT( DifferenceInCalendarMonths( StartOfYear(t), EndOfYear(t)) ); PRINT( DifferenceInCalendarQuarters( StartOfYear(t), EndOfYear(t)) ); PRINT( DifferenceInCalendarYears( StartOfYear(t), EndOfYear(t)) ); PRINT( DifferenceInSeconds( t, EndOfDay(t)) ); PRINT( DifferenceInMinutes( t, EndOfDay(t)) ); PRINT( DifferenceInHours( t, EndOfDay(t)) ); PRINT( DifferenceInDays( StartOfYear(t), EndOfYear(t)) ); PRINT( DifferenceInWeeks( StartOfYear(t), EndOfYear(t)) ); PRINT( DifferenceInMonths( StartOfYear(t), EndOfYear(t)) ); PRINT( DifferenceInQuarters( StartOfYear(t), EndOfYear(t)) ); PRINT( DifferenceInYears( StartOfYear(t), EndOfYear(t)) ); PRINT( DifferenceInBusinessDays( StartOfYear(t), EndOfYear(t)) ); PRINT( DifferenceInWeekendDays( StartOfYear(t), EndOfYear(t)) ); MqlDateTime diff; PRINT( CalendarDifference( t, D'2035.10.9', diff ) ); PRINT( CalendarDifferenceToString( t, D'2035.10.9' ) ); PRINT( AddPeriod( t, diff ) ); PRINT( SubPeriod( D'2035.10.9', diff ) ); //#################################################################### //# # //# Comparison and Checks # //# # //#################################################################### //+==================================================================+ //| Is() Checks | //+==================================================================+ PRINT( IsFirstDayOfMonth(t) ); PRINT( IsLastDayOfMonth(t) ); PRINT( IsWeekend(t) ); PRINT( IsWeekday(t) ); PRINT( IsSunday(t) ); PRINT( IsMonday(t) ); PRINT( IsTuesday(t) ); PRINT( IsWednesday(t) ); PRINT( IsThursday(t) ); PRINT( IsFriday(t) ); PRINT( IsSaturday(t) ); //+==================================================================+ //| IsSame() Units | //+==================================================================+ PRINT( IsSameMinute(t, AddHours(t, 25)) ); PRINT( IsSameHour(t, AddHours(t, 25)) ); PRINT( IsSameDay(t, AddHours(t, 25)) ); PRINT( IsSameWeek(t, AddHours(t, 25)) ); PRINT( IsSameMonth(t, AddHours(t, 25)) ); PRINT( IsSameQuarter(t, AddHours(t, 25)) ); PRINT( IsSameYear(t, AddHours(t, 25)) ); //+==================================================================+ //| IsCurrent() Units | //+==================================================================+ PRINT( IsCurrentMinute(t) ); PRINT( IsCurrentHour(t) ); PRINT( IsCurrentWeek(t) ); PRINT( IsCurrentMonth(t) ); PRINT( IsCurrentQuarter(t) ); PRINT( IsCurrentYear(t) ); PRINT( IsToday(t) ); PRINT( IsTomorrow(t) ); PRINT( IsYesterday(t) ); //+==================================================================+ //| Session() Checks | //+==================================================================+ PRINT( IsInSession(8, 0, 17, 0) ); // Uses TimeTradeServer() PRINT( IsInSession("22:00", "06:00") ); //+==================================================================+ //| Misc | //+==================================================================+ PRINT( IsLeapYear(2100) ); PRINT( DaysInYear(2100) ); PRINT( DaysInMonth(2024, 2) ); PRINT( WeeksInYear(2026) ); PRINT( WeeksInMonth(2026, 8) ); //#################################################################### //# # //# Format & Display # //# # //#################################################################### //+==================================================================+ //| Formating Time to String | //+==================================================================+ PRINT( TimeToString(t) ); // MQL's native TimeToString() PRINT( t2s(t) ); // Formats time with the weekday name PRINT( TimeFormat(t) ); PRINT( TimeFormat(t, "DDD, YYYY.MM.DD hh:mm:ss") ); PRINT( TimeFormat(t, "DDDD, D MMM YYYY, HH:mm a") ); PRINT( SecondsToString( SecsElapsedOfDay(t) ) ); } //+------------------------------------------------------------------+ /* example output: 1. TimeToString(t, TIME_DATE|TIME_SECONDS) = 2026.07.18 19:54:28 2. t2s( t, TIME_DATE|TIME_SECONDS) = Sat, 2026.07.18 19:54:28 3. TimeToStructFast(t, dt[0]) = true [year] [mon] [day] [hour] [min] [sec] [day_of_week] [day_of_year] [0] 2026 7 18 19 54 28 6 198 4. DateFrom(2022, 03, 25) = 2022.03.25 00:00:00 5. StructToTimeFast(dt[0]) = 2026.07.18 19:54:28 6. Second(t) = 28 7. Minute(t) = 54 8. Hour(t) = 19 9. Day(t) = 18 10. Month(t) = 7 11. Year(t) = 2026 12. DateOnly(t) = 2026.07.18 00:00:00 13. TimeOnly(t) = 1970.01.01 19:54:28 14. DayOfWeek(t) = 6 15. DayOfYear(t) = 198 16. WeekOfMonth(t) = 3 17. WeekOfYear(t) = 29 18. MinuteIndex(t) = 29740074 19. HourIndex(t) = 495667 20. DayIndex(t) = 20652 21. WeekIndex(t) = 2950 22. MonthIndex(t) = 678 23. QuarterIndex(t) = 226 24. YearIndex(t) = 56 25. Quarter(t) = 3 26. HourOfWeek(t) = 139 27. HourOfMonth(t) = 427 28. HourOfQuarter(t) = 427 29. HourOfYear(t) = 4771 30. MinuteOfDay(t) = 1194 31. MinuteOfWeek(t) = 8394 32. MinuteOfMonth(t) = 25674 33. MinuteOfQuarter(t) = 25674 34. MinuteOfYear(t) = 286314 35. SecsElapsedOfMinute(t) = 28 36. SecsElapsedOfHour(t) = 3268 37. SecsElapsedOfDay(t) = 71668 38. SecsElapsedOfWeek(t) = 503668 39. SecsElapsedOfMonth(t) = 1540468 40. SecsElapsedOfQuarter(t) = 1540468 41. SecsElapsedOfYear(t) = 17178868 42. SecsElapsedOfEpoch(t) = 1784404468 43. StartOfMinute(t) = 2026.07.18 19:54:00 44. StartOfHour(t) = 2026.07.18 19:00:00 45. StartOfDay(t) = 2026.07.18 00:00:00 46. StartOfWeek(t) = 2026.07.13 00:00:00 47. StartOfMonth(t) = 2026.07.01 00:00:00 48. StartOfQuarter(t) = 2026.07.01 00:00:00 49. StartOfYear(t) = 2026.01.01 00:00:00 50. StartOfToday() = 2026.07.18 00:00:00 51. StartOfTomorrow() = 2026.07.19 00:00:00 52. StartOfYesterday() = 2026.07.17 00:00:00 53. EndOfMinute(t) = 2026.07.18 19:54:59 54. EndOfHour(t) = 2026.07.18 19:59:59 55. EndOfDay(t) = 2026.07.18 23:59:59 56. EndOfWeek(t) = 2026.07.19 23:59:59 57. EndOfMonth(t) = 2026.07.31 23:59:59 58. EndOfQuarter(t) = 2026.09.30 23:59:59 59. EndOfYear(t) = 2026.12.31 23:59:59 60. EndOfToday() = 2026.07.18 23:59:59 61. EndOfTomorrow() = 2026.07.19 23:59:59 62. EndOfYesterday() = 2026.07.17 23:59:59 63. LastDayOfWeek(t) = 2026.07.19 00:00:00 64. LastDayOfMonth(t) = 2026.07.31 00:00:00 65. LastDayOfQuarter(t) = 2026.09.30 00:00:00 66. LastDayOfYear(t) = 2026.12.31 00:00:00 67. RoundToMinute(t) = 2026.07.18 19:54:00 68. RoundToHour(t) = 2026.07.18 20:00:00 69. RoundToDay(t) = 2026.07.19 00:00:00 70. RoundToWeek(t) = 2026.07.20 00:00:00 71. CeilToMinute(t) = 2026.07.18 19:55:00 72. CeilToHour(t) = 2026.07.18 20:00:00 73. CeilToDay(t) = 2026.07.19 00:00:00 74. CeilToWeek(t) = 2026.07.20 00:00:00 75. NextSunday(t) = 2026.07.19 00:00:00 76. NextMonday(t) = 2026.07.20 00:00:00 77. NextTuesday(t) = 2026.07.21 00:00:00 78. NextWednesday(t) = 2026.07.22 00:00:00 79. NextThursday(t) = 2026.07.23 00:00:00 80. NextFriday(t) = 2026.07.24 00:00:00 81. NextSaturday(t) = 2026.07.25 00:00:00 82. PreviousSunday(t) = 2026.07.12 00:00:00 83. PreviousMonday(t) = 2026.07.13 00:00:00 84. PreviousTuesday(t) = 2026.07.14 00:00:00 85. PreviousWednesday(t) = 2026.07.15 00:00:00 86. PreviousThursday(t) = 2026.07.16 00:00:00 87. PreviousFriday(t) = 2026.07.17 00:00:00 88. PreviousSaturday(t) = 2026.07.11 00:00:00 89. FirstWeekdayOfMonth(t, SUNDAY) = 2026.07.05 00:00:00 90. LastWeekdayOfMonth(t, SUNDAY) = 2026.07.26 00:00:00 91. WithSecond(t, 05) = 2026.07.18 19:54:05 92. WithMinute(t, 30) = 2026.07.18 19:30:28 93. WithHour(t, 10) = 2026.07.18 10:54:28 94. WithDay(t, 26) = 2026.07.26 19:54:28 95. WithMonth(t, 2) = 2026.02.18 19:54:28 96. WithYear(t, 2024) = 2024.07.18 19:54:28 97. WithDayOfWeek(t, FRIDAY) = 2026.07.17 19:54:28 98. WithTime(t, 10, 30, 05) = 2026.07.18 10:30:05 99. WithDate(t, 2024, 2, 26) = 2024.02.26 19:54:28 100. AddSeconds(t, 30) = 2026.07.18 19:54:58 101. AddMinutes(t, 30) = 2026.07.18 20:24:28 102. AddHours(t, 2) = 2026.07.18 21:54:28 103. AddDays(t, 10) = 2026.07.28 19:54:28 104. AddBusinessDays(t, 10) = 2026.07.31 19:54:28 105. AddWeeks(t, 4) = 2026.08.15 19:54:28 106. AddMonths(t, 2) = 2026.09.18 19:54:28 107. AddQuarters(t, 3) = 2027.04.18 19:54:28 108. AddYears(t, 5) = 2031.07.18 19:54:28 109. SubSeconds(t, 30) = 2026.07.18 19:53:58 110. SubMinutes(t, 30) = 2026.07.18 19:24:28 111. SubHours(t, 2) = 2026.07.18 17:54:28 112. SubDays(t, 10) = 2026.07.08 19:54:28 113. SubBusinessDays(t, 10) = 2026.07.06 19:54:28 114. SubWeeks(t, 4) = 2026.06.20 19:54:28 115. SubMonths(t, 2) = 2026.05.18 19:54:28 116. SubQuarters(t, 3) = 2025.10.18 19:54:28 117. SubYears(t, 5) = 2021.07.18 19:54:28 118. DifferenceInCalendarDays( StartOfYear(t), EndOfYear(t)) = 364 119. DifferenceInCalendarWeeks( StartOfYear(t), EndOfYear(t)) = 52 120. DifferenceInCalendarMonths( StartOfYear(t), EndOfYear(t)) = 11 121. DifferenceInCalendarQuarters( StartOfYear(t), EndOfYear(t)) = 3 122. DifferenceInCalendarYears( StartOfYear(t), EndOfYear(t)) = 0 123. DifferenceInSeconds( t, EndOfDay(t)) = 14731 124. DifferenceInMinutes( t, EndOfDay(t)) = 245 125. DifferenceInHours( t, EndOfDay(t)) = 4 126. DifferenceInDays( StartOfYear(t), EndOfYear(t)) = 364 127. DifferenceInWeeks( StartOfYear(t), EndOfYear(t)) = 52 128. DifferenceInMonths( StartOfYear(t), EndOfYear(t)) = 11 129. DifferenceInQuarters( StartOfYear(t), EndOfYear(t)) = 3 130. DifferenceInYears( StartOfYear(t), EndOfYear(t)) = 0 131. DifferenceInBusinessDays( StartOfYear(t), EndOfYear(t)) = 260 132. DifferenceInWeekendDays( StartOfYear(t), EndOfYear(t)) = 104 133. CalendarDifference( t, D'2035.10.9', diff ) = true 134. CalendarDifferenceToString( t, D'2035.10.9' ) = 9y 2m 20d 04:05:32 135. AddPeriod( t, diff ) = 2035.10.09 00:00:00 136. SubPeriod( D'2035.10.9', diff ) = 2026.07.18 19:54:28 137. IsFirstDayOfMonth(t) = false 138. IsLastDayOfMonth(t) = false 139. IsWeekend(t) = true 140. IsWeekday(t) = false 141. IsSunday(t) = false 142. IsMonday(t) = false 143. IsTuesday(t) = false 144. IsWednesday(t) = false 145. IsThursday(t) = false 146. IsFriday(t) = false 147. IsSaturday(t) = true 148. IsSameMinute(t, AddHours(t, 25)) = false 149. IsSameHour(t, AddHours(t, 25)) = false 150. IsSameDay(t, AddHours(t, 25)) = false 151. IsSameWeek(t, AddHours(t, 25)) = true 152. IsSameMonth(t, AddHours(t, 25)) = true 153. IsSameQuarter(t, AddHours(t, 25)) = true 154. IsSameYear(t, AddHours(t, 25)) = true 155. IsCurrentMinute(t) = true 156. IsCurrentHour(t) = true 157. IsCurrentWeek(t) = true 158. IsCurrentMonth(t) = true 159. IsCurrentQuarter(t) = true 160. IsCurrentYear(t) = true 161. IsToday(t) = true 162. IsTomorrow(t) = false 163. IsYesterday(t) = false 164. IsInSession(8, 0, 17, 0) = false 165. IsInSession(22:00, 06:00) = false 166. IsLeapYear(2100) = false 167. DaysInYear(2100) = 365 168. DaysInMonth(2024, 2) = 29 169. WeeksInYear(2026) = 53 170. WeeksInMonth(2026, 8) = 6 171. TimeToString(t) = 2026.07.18 19:54 172. t2s(t) = Sat, 2026.07.18 19:54 173. TimeFormat(t) = 2026.07.18 19:54 174. TimeFormat(t, DDD, YYYY.MM.DD hh:mm:ss) = Sat, 2026.07.18 19:54:28 175. TimeFormat(t, DDDD, D MMM YYYY, HH:mm a) = Saturday, 18 Jul 2026, 07:54 pm 176. SecondsToString( SecsElapsedOfDay(t) ) = 19:54:28 */