[Pas2js] DateTimeToIso8601 issue

Michael Van Canneyt michael at freepascal.org
Mon Nov 25 15:19:47 CET 2019



On Mon, 25 Nov 2019, warleyalex via Pas2js wrote:

> mORMot server computes a nounce client and uses the NowToIso8601 method to
> compute the nonce, so
> apparently, this method in pas2js returns this format:
> NowToIso8601();  //-->  2019-11-2509:5809:58:05
>
> but we want this expected result:
> 2019-11-25T09:58:09
>
> I could find a way to get this format, hum I had to edit The SysUtils unit |
> the function FormatDateTime and modify to:
>
>            'T': if Count = 1 then
> 	//	   StoreFormat(ShortTimeFormat, Nesting+1, True)
>               StoreString('T')
>                 else
> 	          StoreFormat(LongTimeFormat, Nesting+1, True);

That's wrong.

> [code]
> function NowToIso8601: string;
> begin
>  result := DateTimeToIso8601(Now);
> end;
>
> function DateTimeToIso8601(Value: TDateTime): string;
> begin // e.g. YYYY-MM-DD Thh:mm:ss or YYYY-MM-DDThh:mm:ss
>  if Value<=0 then
>    result := '' else
>  if frac(Value)=0 then
>    result := FormatDateTime('yyyy-mm-dd',Value{,DateTimeZone.UTC}) else
>  if trunc(Value)=0 then
>    result := FormatDateTime('Thh:nn:ss',Value{,DateTimeZone.UTC}) else
>    result :=
> FormatDateTime('yyyy-mm-ddThh:nn:ss',Value{,DateTimeZone.UTC});

You must use quotes if you want a literal T character.

FormatDateTime('yyyy-mm-dd"T"hh:nn:ss',Value{,DateTimeZone.UTC});

But you don't need to implement this.

The dateutils unit contains DateTimeToRFC3339. 
For all practical purposes, this is the same as ISO8601, so you can use that.
I use it in all my code that needs a 'standard' date time.

Michael.


More information about the Pas2js mailing list