Recent

Author Topic: TryStrToDateTime not always working  (Read 794 times)

WimS

  • Newbie
  • Posts: 4
TryStrToDateTime not always working
« on: November 26, 2020, 10:23:23 am »
Code: Pascal  [Select][+][-]
  1. var
  2.         TmpDatumtijd:TDateTime;
  3.         MyFormatSettings:TFormatSettings;  
  4. begin  
  5.         MyFormatSettings := FormatSettings;
  6.         MyFormatSettings.DateSeparator := ':';
  7.         MyFormatSettings.ShortDateFormat:='yyyy:mm:dd';
  8.         MyFormatSettings.TimeSeparator:=':';
  9.         MyFormatSettings.LongTimeFormat:='hh:nn:ss';
  10.  
  11.         if TryStrToDateTime('2018:09:07 12:34:56',TmpDatumtijd, MyFormatSettings)
  12.            then ....
   This doesn't work, result is always no valid date, but the date is valid in my opnion
   
Code: Pascal  [Select][+][-]
  1.         MyFormatSettings := FormatSettings;
  2.         MyFormatSettings.DateSeparator := '-';
  3.         MyFormatSettings.ShortDateFormat:='yyyy-mm-dd';
  4.         MyFormatSettings.TimeSeparator:=':';
  5.         MyFormatSettings.LongTimeFormat:='hh:nn:ss';
  6.  
  7.         if TryStrToDateTime('2018-09-07 12:34:56',TmpDatumtijd, MyFormatSettings)
  8.            then ...
   This works ok!!!
   
   What am I doing wrong?
   Lazarus v2.010 r63526, Windows 10

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: TryStrToDateTime not always working
« Reply #1 on: November 26, 2020, 12:48:10 pm »
Having the same character for DateSeparator and TimeSeparator is not supported AFAIK.
I worked on some parts of that code (some years ago) and found no solution how to solve it.

Bart

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: TryStrToDateTime not always working
« Reply #2 on: November 26, 2020, 12:56:31 pm »
Code: Pascal  [Select][+][-]
  1. TmpDatumtijd:= StrToDateTime('2018:09:07 12:34:56', MyFormatSettings)
However works.
« Last Edit: November 26, 2020, 01:14:36 pm by bytebites »

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: TryStrToDateTime not always working
« Reply #3 on: November 26, 2020, 04:55:57 pm »
Doesn't StrToDateTime call TryStrToDateTime?

Bart

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TryStrToDateTime not always working
« Reply #4 on: November 26, 2020, 05:24:04 pm »
Doesn't StrToDateTime call TryStrToDateTime?

No, they call respectively StrToDate() and StrToTime() or TryStrToDate() and TryStrToTime().
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: TryStrToDateTime not always working
« Reply #5 on: November 26, 2020, 06:30:14 pm »
cough scandatetime() : :D

 

TinyPortal © 2005-2018