MyChat Scripts: function ISOStrToDateTime for converting ISO string into date and time
"For developers", "Server scripts", "Functions description", "Converting types", "DateTimeToISOStr".
Converting the text string with date/time in ISO format ("yyyy-mm-dd hh:nn:ss") into double value of the date and time.
Syntax
function ISOStrToDateTime(const s: string): double;
Parameters and return values
Parameter |
Type |
Value |
s |
string |
a string that contains the date ad tine in ISO format. |
Function result
A double value of the date and time. If the source string is incorrect, the function returns the value NoDate .
Example
function Check(sISO: string): boolean;
var
dt: double;
begin
dt := ISOStrToDateTime(sISO);
if dt <> NoDate then mLogScript(FormatDateTime('ddd d mmm yyyy hh:nn:ss', dt), 'OK')
else mLogScript(sISO, 'ERROR');
end;
begin
Check('2018-02-02 09:00:00');
Check('01-01-2021 18:00:00');
Check('Hello, world!');
Check(FormatDateTime('yyyy-mm-dd hh:nn:ss:zzz', Now));
Check(FormatDateTime('yyyy-mm-dd hh:nn:ss', IncYear(Now, -100)));
end.
Script work result
[19:57:24] (Log "ISOStrToDateTime"): [OK] Fr 2 Feb. 2018 09:00:00
[19:57:24] (Log "ISOStrToDateTime"): [ERROR] 01-01-2021 18:00:00
[19:57:24] (Log "ISOStrToDateTime"): [ERROR] Hello, world!
[19:57:24] (Log "ISOStrToDateTime"): [OK] Tue 19 Jan. 2021 19:57:24
[19:57:24] (Log "ISOStrToDateTime"): [OK] Wed 19 Jan. 1921 19:57:24
[19:57:24] (Run "ISOStrToDateTime"): Script operation time: 2 ms
[19:57:24] (Run "ISOStrToDateTime"): Script operation time.