Date
A date is a quad: the number of seconds since 1 January 1970, 00:00:00. Dates before 1970 are negative. Dates go from the year 1601 to 9999, to the second, in the Gregorian calendar. As in PureBasic, a date carries no time zone: Date() gives the local time, DateUTC() the time in UTC, and ConvertDate turns one into the other.
Date ✅
Date.q = Date()
Date.q = Date(Year, Month, Day, Hour, Minute, Second)
Without parameters, returns the current local date and time. With them, returns that date, or -1 if it doesn't exist (the 30th of February, the year 1600, minute 60).
OpenConsole()
d = Date(2024, 2, 29, 13, 45, 7)
PrintN(Str(d) + " " + FormatDate("%dd/%mm/%yyyy %hh:%ii:%ss", d))
PrintN(Str(Date(2023, 2, 29, 0, 0, 0)))
Output:
1709214307 29/02/2024 13:45:07
-1
DateUTC ✅
Date.q = DateUTC()
Returns the current date and time in UTC.
ConvertDate ✅
Date.q = ConvertDate(Date.q, #PB_Date_UTC) ; local time -> UTC
Date.q = ConvertDate(Date.q, #PB_Date_LocalTime) ; UTC -> local time
Converts using the time zone's offset at that date, summer time included. On Windows this is the system's time zone. On Linux it's the TZ environment variable (a zone name like Europe/London, or a rule like EST5EDT,M3.2.0,M11.1.0), or /etc/localtime without one. Dates after the zone file's list of changes follow the rule at its end.
Year, Month, Day, Hour, Minute, Second ✅
Result = Year(Date.q) ; also Month, Day, Hour, Minute, Second
Return one part of the date.
DayOfWeek, DayOfYear ✅
Result = DayOfWeek(Date.q) ; 0 = Sunday ... 6 = Saturday
Result = DayOfYear(Date.q) ; 1 ... 366
OpenConsole()
d = Date(2024, 2, 29, 13, 45, 7)
PrintN(Str(Year(d)) + " " + Str(Month(d)) + " " + Str(Day(d)) + " " + Str(Hour(d)) + ":" + Str(Minute(d)) + ":" + Str(Second(d)))
PrintN(Str(DayOfWeek(d)) + " " + Str(DayOfYear(d)))
Output:
2024 2 29 13:45:7
4 60
AddDate ✅
Date.q = AddDate(Date.q, Type, Value)
Adds Value (which may be negative) of Type: #PB_Date_Year, #PB_Date_Month, #PB_Date_Week, #PB_Date_Day, #PB_Date_Hour, #PB_Date_Minute or #PB_Date_Second. Adding months or years keeps the day if the new month has it, and otherwise takes the month's last day.
OpenConsole()
PrintN(FormatDate("%dd/%mm/%yyyy", AddDate(Date(2008, 3, 31, 0, 0, 0), #PB_Date_Month, 1)))
PrintN(FormatDate("%dd/%mm/%yyyy", AddDate(Date(2024, 2, 29, 0, 0, 0), #PB_Date_Year, 1)))
PrintN(FormatDate("%dd/%mm/%yyyy %hh:%ii", AddDate(Date(2024, 12, 31, 23, 30, 0), #PB_Date_Minute, 45)))
Output:
30/04/2008
28/02/2025
01/01/2025 00:15
FormatDate ✅
Text$ = FormatDate(Mask$, Date.q)
Returns Mask$ with these tokens replaced by the date's parts. Everything else stays as it is.
| Token | |
|---|---|
%yyyy |
the year, four digits |
%yy |
the year, two digits |
%mm |
the month, two digits |
%dd |
the day, two digits |
%hh |
the hour (0 to 23), two digits |
%ii |
the minute, two digits |
%ss |
the second, two digits |
ParseDate ✅
Date.q = ParseDate(Mask$, Text$)
Reads a date from Text$ laid out as Mask$, with the same tokens as FormatDate. Returns the date, or -1 if the text doesn't match the mask or isn't a real date. Parts the mask doesn't have are 1970, January, the 1st, 00:00:00. %yyyy reads four digits. The other tokens read one or two, so "6:15" fits "%hh:%ii" (an x addition). %yy reads 70 to 99 as 1970 to 1999, and 00 to 69 as 2000 to 2069.
OpenConsole()
PrintN(FormatDate("%yyyy-%mm-%dd %hh:%ii", ParseDate("%dd.%mm.%yyyy %hh:%ii", "05.11.1955 6:15")))
PrintN(Str(ParseDate("%dd/%mm/%yyyy", "31/04/2024")) + " " + Str(ParseDate("%yyyy-%mm", "2024x03")))
Output:
1955-11-05 06:15
-1 -1
WeekOfYear ✅ (x addition)
Result = WeekOfYear(Date.q)
Returns the ISO 8601 week number (1 to 53). Weeks start on Monday, and week 1 is the one with the year's first Thursday, so the first days of January can be in the previous year's last week.
DaysInMonth, IsLeapYear ✅ (x additions)
Result = DaysInMonth(Year, Month)
Result = IsLeapYear(Year)
DaysInMonth returns 28 to 31. IsLeapYear returns 1 for a leap year (every fourth year, but not whole centuries, unless divisible by 400) and 0 otherwise.
OpenConsole()
PrintN(Str(WeekOfYear(Date(2021, 1, 3, 0, 0, 0))) + " " + Str(WeekOfYear(Date(2026, 6, 15, 0, 0, 0))))
PrintN(Str(DaysInMonth(2024, 2)) + " " + Str(DaysInMonth(2100, 2)) + " " + Str(IsLeapYear(2000)))
Output:
53 25
29 28 1