[SOLVED] Convert date ScanDateTime is wrong imho lazarus / 21-General ---------------------------------------------------------------------- pcurtis · 2021-02-06 14:39 Hi All, is there an easy way to convert April 2nd, 2014, 06:57 PM to 2014/04/02 18:57:00 Thanks in advance. ---------------------------------------------------------------------- lucamar · 2021-02-06 16:25 Not that I know about; all the "easy" conversion functions in SysUtils (and elsewere, AFAIK), require the date elements to be in numeric form. Of course, if you can be sure the date is always in that specific format, with long mont names, etc., it should' be too difficult to use a lookup to get the month as number, strip the day ordinal suffix, etc.and then use StrToDateTime() for the final conversion. It all depends of what you mean by "an easy way" ---------------------------------------------------------------------- pcurtis · 2021-02-06 16:37 I heard about ScanDateTime but as usual little or no docs ---------------------------------------------------------------------- wp · 2021-02-06 16:43 Normally many date/time strings can be converted by using the ScanDateTime function DateUtils. Here, however, i think it will fail due to the 'st', 'nd', 'rd', and 'th' suffix of the day number; and after replacing that by an empty string I still could not get it work. Finally I decided to parse the date/time string myself. The Split string helper is a real "helper" for this task since it splits a string at specified characters, here space, comma and colon. The result is a string array with the parts "April", "2nd", "", "2014", "", "06", "57", "PM". (the empty strings before and after the year are due to the combined separators space and comma). After having done that it is easy to convert the parts to numbers and create the date/time variable: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function LongStrToDateTime(s: String; const MonthNames: array of string): TDateTime;var  sa: TStringArray;  m, d, y, h, min: Integer;begin  // Split the input string using the separators space, comma and colon  sa := s.Split([' ', ',', ':']);   // Find the month number  m := -1;  for m := 0 to 11 do if MonthNames[m] = sa[0] then break;  if m = -1 then  raise Exception.Create('Unknown month name');  inc(m);  // m-loop is zero-based   // Find the day number  SetLength(sa[1], Length(sa[1]) - 2);  // Delete "st", "nd", "rd, "th"  if not TryStrToInt(sa[1], d) then    raise Exception.Create('Unsupported day number.');   // sa[2] is empty due to combination ', '   // Find the year number  if not TryStrToInt(sa[3], y) then    raise Exception.Create('Incorrect year number');   // sa[4] is empty due to combination ', '   // Find the hour number  if not TryStrToInt(sa[5], h) then    raise Exception.Create('Incorrect hour number.');   // Find the minute number  if not TryStrToInt(sa[6], min) then    raise Exception.Create('Incorrect minutes number.');   // correct for "PM"  Result := EncodeTime(h, min, 0, 0);  if sa[7] = 'PM' then Result := Result + 0.5;   // Buld result  Result := EncodeDate(y, m, d) + Result;end;  const  s = 'April 2nd, 2014, 06:57 PM';...  d := LongStrToDateTime(s1, FormatSettings.LongMonthNames);  WriteLn(DateTimeToStr(d)); [/code] ---------------------------------------------------------------------- pcurtis · 2021-02-06 17:11 Awesome. Thanks. ---------------------------------------------------------------------- cdbc · 2021-02-06 18:14 Hi pcurtis Here's an "easy way for you"  Attached convert_date.zip Regards Benny ---------------------------------------------------------------------- bytebites · 2021-02-06 19:07 Hard way Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ScanDateTime('mmmm dd??, yyyy, hh:mm ampm','April 2nd, 2014, 06:57 pm'); ---------------------------------------------------------------------- lucamar · 2021-02-06 20:18 Quote from: lucamar on February 06, 2021, 05:25:08 pm[...] all the "easy" conversion functions in SysUtils (and elsewere, AFAIK), require the date elements to be in numeric form. Quote from: bytebites on February 06, 2021, 08:07:40 pmHard way Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ScanDateTime('mmmm dd??, yyyy, hh:mm ampm','April 2nd, 2014, 06:57 pm'); There goes my infalibility, out on the night towards the west Quote from: pcurtis on February 06, 2021, 05:37:01 pmI heard about ScanDateTime but as usual little or no docs Like this, you mean? ScanDateTime (doc for RTL unit DateUtils). Granted, it might not be much, but there is not much more to say about it. ---------------------------------------------------------------------- wp · 2021-02-06 20:53 Quote from: bytebites on February 06, 2021, 08:07:40 pmHard way Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ScanDateTime('mmmm dd??, yyyy, hh:mm ampm','April 2nd, 2014, 06:57 pm');Wow! How did you find out that there is a '?' mask parameter? ---------------------------------------------------------------------- bytebites · 2021-02-06 20:59 RTFSC ---------------------------------------------------------------------- wp · 2021-02-06 21:51 Since there are probably more peope who RTFM than who RTFSC I submitted a bugreport to add the '?' to the documentation: https://bugs.freepascal.org/view.php?id=38448 ---------------------------------------------------------------------- pcurtis · 2021-02-08 15:34 I needed to modify Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   // Find the hour number  if not TryStrToInt(sa[5], h) then    raise Exception.Create('Incorrect hour number.');  if sa[7] = 'PM' then            <-- added this    if h < 12 then h := h + 12; <-- added this   // Find the minute number  if not TryStrToInt(sa[6], min) then    raise Exception.Create('Incorrect minutes number.');   // correct for "PM"  Result := EncodeTime(h, min, 0, 0);  //if sa[7] = 'PM' then Result := Result + 0.5; <-- removed this  in wp's post ---------------------------------------------------------------------- pcurtis · 2021-02-08 15:54 da := ScanDateTime('mmmm dd??, yyyy, hh:mm ampm','April 2nd, 2014, 12:01 pm');   Caption := FormatDateTime('ddmmyy hh:mm', da); returns 030414 12:01 not 020414 12:01 ?? in my opinion the day is wrong ---------------------------------------------------------------------- sstvmaster · 2021-02-08 16:05 Quote from: bytebites on February 06, 2021, 08:07:40 pmHard way Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ScanDateTime('mmmm dd??, yyyy, hh:mm ampm','April 2nd, 2014, 06:57 pm'); I got an EConversion error in 2.0.10 with this Pattern above. This is works, note the / between am and pm. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ScanDateTime('mmmm dd??, yyyy, hh:mm am/pm','April 2nd, 2014, 06:57 pm'); ---------------------------------------------------------------------- sstvmaster · 2021-02-08 16:10 Quote from: pcurtis on February 08, 2021, 04:54:17 pm  da := ScanDateTime('mmmm dd??, yyyy, hh:mm ampm','April 2nd, 2014, 12:01 pm');   Caption := FormatDateTime('ddmmyy hh:mm', da); returns 030414 12:01 not 020414 12:01 ?? in my opinion the day is wrong Because 12:01pm is the next day? 00:01 03.04.14? 12:01am is high noon. Look: https://en.wikipedia.org/wiki/12-hour_clock ---------------------------------------------------------------------- pcurtis · 2021-02-08 16:14 why is it the next day? days change at midnight not midday https://en.wikipedia.org/wiki/12-hour_clock anyway it should  only convert not change ---------------------------------------------------------------------- sstvmaster · 2021-02-08 16:29 Quote from: pcurtis on February 08, 2021, 04:54:17 pm...   da := ScanDateTime('mmmm dd??, yyyy, hh:mm ampm','April 2nd, 2014, 12:01 pm'); ... Because you wrote 'April 2nd, 2014, 12:01 pm' 12:01 pm is the next day!!! It is 1 minute past 24:00. 12:01 am is the same day, midday. ---------------------------------------------------------------------- wp · 2021-02-08 16:33 Quote from: pcurtis on February 08, 2021, 04:34:22 pmI needed to modify [...] in wp's post Ah, I forgot about the mysterious hour 12... (https://www.timeanddate.com/time/am-and-pm.html). For us in Europe (non UK!) the day begins at 0:00, but in the AM/PM countries it begins at 12:00AM. The last minute of the first hour is 0:59 in non-AM/PM, but 12:59AM in AM/PM countries. The next full hour is then 1:00 (AM) in both systems. Hours are equal until 13:00 or 1:00PM Therefore you must modify the PM correction in my code in this way: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   // correct for "PM"  Result := EncodeTime(h, min, 0, 0);  if sa[7] = 'PM' then    if (Result < 0.5) and (Result >= 1/24) then Result := Result + 0.5; ---------------------------------------------------------------------- pcurtis · 2021-02-08 16:37 @sstvmaster so what is 1 minute past midday? @wp thanks ScanDateTime('mmmm dd??, yyyy, hh:mm','April 2nd, 2014, 06:57 pm') works just leave the ampm out of the format string ---------------------------------------------------------------------- sstvmaster · 2021-02-08 17:11 Quote from: pcurtis on February 08, 2021, 05:37:53 pm@sstvmaster so what is 1 minute past midday? You are right, so there is an am/pm bug in ScanDateTime? Referenzed to the wiki entry: https://en.wikipedia.org/wiki/12-hour_clock Or i'm wrong. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; uses  DateUtils, SysUtils;var  dt: TDateTime;begin  dt := ScanDateTime('mmmm dd??, yyyy, hh:mm am/pm', 'April 2nd, 2014, 12:00 am');  WriteLn('12:00 am: ', DateTimeToStr(dt), '  -> should 02.04.14 00:00');   dt := ScanDateTime('mmmm dd??, yyyy, hh:mm am/pm', 'April 2nd, 2014, 12:01 am');  WriteLn('12:01 am: ', DateTimeToStr(dt), '  -> should 02.04.2014 00:01:00');   dt := ScanDateTime('mmmm dd??, yyyy, hh:mm am/pm', 'April 2nd, 2014, 1:00 am');  WriteLn(' 1:00 am: ', DateTimeToStr(dt), '  -> should 02.04.2014 01:00:00');   dt := ScanDateTime('mmmm dd??, yyyy, hh:mm am/pm', 'April 2nd, 2014, 11:00 am');  WriteLn('11:00 am: ', DateTimeToStr(dt), '  -> should 02.04.2014 11:00:00');   dt := ScanDateTime('mmmm dd??, yyyy, hh:mm am/pm', 'April 2nd, 2014, 11:59 am');  WriteLn('11:59 am: ', DateTimeToStr(dt), '  -> should 02.04.2014 11:59:00');   dt := ScanDateTime('mmmm dd??, yyyy, hh:mm am/pm', 'April 2nd, 2014, 12:00 pm');  WriteLn('12:00 pm: ', DateTimeToStr(dt), '  -> should 02.04.2014 12:00:00');   dt := ScanDateTime('mmmm dd??, yyyy, hh:mm am/pm', 'April 2nd, 2014, 12:01 pm');  WriteLn('12:01 pm: ', DateTimeToStr(dt), '  -> should 02.04.2014 12:01:00');   dt := ScanDateTime('mmmm dd??, yyyy, hh:mm am/pm', 'April 2nd, 2014, 1:00 pm');  WriteLn(' 1:00 pm: ', DateTimeToStr(dt), '  -> should 02.04.2014 13:00:00');   dt := ScanDateTime('mmmm dd??, yyyy, hh:mm am/pm', 'April 2nd, 2014, 11:00 pm');  WriteLn('11:00 pm: ', DateTimeToStr(dt), '  -> should 02.04.2014 23:00:00');   dt := ScanDateTime('mmmm dd??, yyyy, hh:mm am/pm', 'April 2nd, 2014, 11:59 pm');  WriteLn('11:59 pm: ', DateTimeToStr(dt), '  -> should 02.04.2014 23:59:00');   dt := ScanDateTime('mmmm dd??, yyyy, hh:mm am/pm', 'April 2nd, 2014, 12:00 pm');  WriteLn('12:00 pm: ', DateTimeToStr(dt), '  -> should 03.04.2014 00:00:00');   ReadLn;end. ---------------------------------------------------------------------- ASBzone · 2021-02-10 00:35 Quote from: sstvmaster on February 08, 2021, 05:29:34 pmQuote from: pcurtis on February 08, 2021, 04:54:17 pm...   da := ScanDateTime('mmmm dd??, yyyy, hh:mm ampm','April 2nd, 2014, 12:01 pm'); ... Because you wrote 'April 2nd, 2014, 12:01 pm' 12:01 pm is the next day!!! It is 1 minute past 24:00. 12:01 am is the same day, midday. Say again? 12:01am is 1 minute past midnight. Noon is 12:00pm and thus 12:01pm is 1 minute past noon. Am I missing something here? ---------------------------------------------------------------------- pcurtis · 2021-02-10 07:42 00:01 is 1 min. past midnight. If you look at a digital clock does it go from 23:59 to 24:00 (11:59 PM to 12:00 PM)? I don't think so. 23:59 > 00:00 (11:59 > 00:00) It goes from 11:59 to 12:00 ONLY IN THE MORNING before MIDDAY, NOT at the change to midnight which signifies a new day hence 00:00 Quite remedial stuff ---------------------------------------------------------------------- sstvmaster · 2021-02-10 08:27 We (wp and i) have fixed the problem, see: https://forum.lazarus.freep ascal.org/index.php/topic,53235.msg393629.html#msg393629 And now we only must create a patch and write a bug report. ---------------------------------------------------------------------- lucamar · 2021-02-10 09:33 Quote from: pcurtis on February 10, 2021, 08:42:41 amIt goes from 11:59 to 12:00 ONLY IN THE MORNING before MIDDAY, NOT at the change to midnight which signifies a new day hence 00:00 That's valid for 24 hours clocks. A 12 hours clock goes from 11:59 PM to 12:00AM and from 11:59AM to 12:00 PM. They mimick the most usual two-handed analog clock. ---------------------------------------------------------------------- winni · 2021-02-10 10:09 Hi! ISO says - no matter if am/pm or 24: 12:00 (am/pm) and 24:00 (24) are allowed for an arrival event. They are forbidden for a start event. So much about the green table. Winni ---------------------------------------------------------------------- sstvmaster · 2021-02-10 19:48 Fixed in fpc rev 48580 for 3.3.1 + 3.2.2 https://bugs.freepascal.org/view.php?id=38462 ---------------------------------------------------------------------- ASBzone · 2021-02-10 20:05 Quote from: lucamar on February 10, 2021, 10:33:13 amQuote from: pcurtis on February 10, 2021, 08:42:41 amIt goes from 11:59 to 12:00 ONLY IN THE MORNING before MIDDAY, NOT at the change to midnight which signifies a new day hence 00:00 That's valid for 24 hours clocks. A 12 hours clock goes from 11:59 PM to 12:00AM and from 11:59AM to 12:00 PM. They mimick the most usual two-handed analog clock. Exactly. ---------------------------------------------------------------------- ASBzone · 2021-02-10 20:06 Quote from: sstvmaster on February 10, 2021, 08:48:52 pmFixed in fpc rev 48580 for 3.3.1 + 3.2.2 https://bugs.freepascal.org/view.php?id=38462 Thanks!