Recent

Author Topic: [Solved] A different date format!  (Read 783 times)

loaded

  • Hero Member
  • *****
  • Posts: 838
[Solved] A different date format!
« on: April 25, 2024, 12:55:32 pm »
Hi All,
I have a recipe format like this:
Code: Pascal  [Select][+][-]
  1. 2002-10-13T21:00:00.000Z

I want to convert it to this:
Code: Pascal  [Select][+][-]
  1. 14/10/2002
Is it possible?
« Last Edit: April 25, 2024, 01:38:10 pm by loaded »
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

cdbc

  • Hero Member
  • *****
  • Posts: 1157
    • http://www.cdbc.dk
Re: A different date format!
« Reply #1 on: April 25, 2024, 01:06:44 pm »
Hi
Yes ofc, but that ISO datetime-stamp equals => UTC 13/10/2002 @ 9:00 PM
So what exactly are you trying to achieve?!?
The ISO format is dead-easy to parse  8-)
edit: Have a 'Sniff' around in the RTL there are some ISO routines lying around
Regards Benny
« Last Edit: April 25, 2024, 01:09:36 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

alpine

  • Hero Member
  • *****
  • Posts: 1107
Re: A different date format!
« Reply #2 on: April 25, 2024, 01:11:25 pm »
DateUtils have ISO8601ToDate function
Then use FormatDateTime to convert the returned timestamp.
Your result is one day ahead, do you mean it will be according to a different time zone?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

loaded

  • Hero Member
  • *****
  • Posts: 838
Re: A different date format!
« Reply #3 on: April 25, 2024, 01:11:38 pm »
Thank you very much for your answer, cdbc.
The information you provided is very useful to me, I didn't know this before. Because I haven't worked with dates before.

So what exactly are you trying to achieve?!?
I am trying to display the date 2002-10-13T21:00:00.000Z as 14/10/2002 and insert it into a json tree we have.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

cdbc

  • Hero Member
  • *****
  • Posts: 1157
    • http://www.cdbc.dk
Re: A different date format!
« Reply #4 on: April 25, 2024, 01:21:04 pm »
Hi
@alpine remembered the function name in the RTL...
Quote
Your result is one day ahead, do you mean it will be according to a different time zone?
I'll re-ask his question?!?
Regards Benny
« Last Edit: April 25, 2024, 01:44:45 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

cdbc

  • Hero Member
  • *****
  • Posts: 1157
    • http://www.cdbc.dk
Re: A different date format!
« Reply #5 on: April 25, 2024, 01:24:21 pm »
Hi
A little inspiration on working with string-dates:
Code: Pascal  [Select][+][-]
  1. const
  2.   { time formats, used in bcstrtotime }
  3.   STRSHORT = 0; { StrShort }
  4.   STRLONG  = 1; { StrLong }
  5.   STRISO   = 2; { StrIso }
  6.   { date formats, used in bcdetectdateformat }
  7.   DATE_DA  = 0; { danish dd.mm.yyyy }
  8.   DATE_US  = 1; { us std. mm/dd/yyyy }
  9.   DATE_ISO = 2; { iso spec. yyyy-mm-dd }
  10.   DATE_STR = 3; { danish dd-mm-yyyy }
  11. ...
  12. { bcStrToDate #2 takes 3 params, the string to convert, the  format ~ DATE_DA,
  13.   DATE_US & DATE_ISO and the type ~ STRSHORT, STRLONG & STRISO, returns a TDateTime }
  14. function bcStrToDate(const aDateStr: string; aFormat, aType: word): TDateTime;
  15. var
  16.   P,PE: pchar;
  17.   L,Fc,Y,M,D: word;
  18.   Buf: string;
  19.   C: char;
  20. begin
  21.   if aDateStr = '' then exit;
  22.   case aFormat of
  23.     DATE_DA : C:= '.';
  24.     DATE_US : C:= '/';
  25.     DATE_ISO: C:= '-';
  26.     DATE_STR: C:= '-'; { extra for numbnuts }
  27.   end;
  28.   L:= Length(aDateStr);
  29.   P:= pchar(aDateStr); PE:= P + L; Buf:= ''; Fc:= 0;
  30.   while P <= PE do begin
  31.     if ((P^ = C) or (P = PE)) then begin
  32.       case Fc of
  33.         0: begin
  34.              case aFormat of
  35.                DATE_DA,DATE_STR : D:= Buf.ToInteger;
  36.                DATE_US : M:= Buf.ToInteger;
  37.                DATE_ISO: Y:= Buf.ToInteger;
  38.              end;
  39.              Buf:= '';
  40.              inc(Fc); // 1
  41.            end; { Fc = 0 }
  42.         1: begin
  43.              case aFormat of
  44.                DATE_DA,DATE_STR : M:= Buf.ToInteger;
  45.                DATE_US : D:= Buf.ToInteger;
  46.                DATE_ISO: M:= Buf.ToInteger;
  47.              end;
  48.              Buf:= '';
  49.              inc(Fc); // 2
  50.            end; { Fc = 1 }
  51.         2: begin
  52.              case aFormat of
  53.                DATE_DA,DATE_STR : Y:= Buf.ToInteger;
  54.                DATE_US : Y:= Buf.ToInteger;
  55.                DATE_ISO: D:= Buf.ToInteger;
  56.              end;
  57.              Buf:= '';
  58.              inc(Fc); // 3
  59.            end; { Fc = 2 }
  60.       end; { case fc of }
  61.     end; { P^=C }
  62.     if P^ in ['0'..'9'] then Buf+= P^;
  63.     inc(P);
  64.   end; { while }
  65.   { cater for DATE_STR ~ NumbNuts using '-' in DATE_DA }
  66.   if aFormat = DATE_ISO then begin { iso or idiots at play }
  67.     if D > 31 then begin { idiots at play }
  68.       L:= Y;
  69.       Y:= D;  { switch'a'roo }
  70.       D:= L;
  71.     end;
  72.   end;
  73.   { cater for short dates }
  74.   if aType = STRSHORT then if Y < ((YearOf(now)-2000)+5) then Y+= 2000 else Y+= 1900; { lookahead = 5 years }
  75.   Result:= EncodeDate(Y,M,D);
  76. end; { bcStrToDate #2 }
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

loaded

  • Hero Member
  • *****
  • Posts: 838
Re: A different date format!
« Reply #6 on: April 25, 2024, 01:37:54 pm »
alpine, thank you very much for your answer.
I never thought the history event would be this detailed.
cdbc and alpine, I did some work with the information you gave me and got the result I wanted.

Code: Pascal  [Select][+][-]
  1. showmessage(DateToStr(ISO8601ToDate('2002-10-13T21:00:00.000Z',false)));

I'm glad to have you. Respects.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

alpine

  • Hero Member
  • *****
  • Posts: 1107
Re: [Solved] A different date format!
« Reply #7 on: April 25, 2024, 01:49:12 pm »
@loaded
Aware of time-zones! Your input is UTC, ISO8601ToDate will do a local TZ adjustment and may trip over midnight.
And  DateToStr will work with your local format settings, may not be DD/MM/YY on a client computer.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

loaded

  • Hero Member
  • *****
  • Posts: 838
Re: [Solved] A different date format!
« Reply #8 on: April 26, 2024, 06:37:07 am »
Thank you for the additional information, brother alpine.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

 

TinyPortal © 2005-2018