Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-06

Date conversion question

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

Hello,
I have a little project in which I need to convert various string encoded in various date format to TDateTime.
For the same date, I'll have:

'2008-05-10 21:49:21'
'2008:05:10 21:49:21'
'2008-05-10T19:49:21Z'

I am no expert with date manipulation so I am asking:

For the first type of string;
 DateSeparator:= '-';
 ShortDateFormat:= 'yyyy:MM:dd';
 StrToDateTime(str_date); //gets converted.
For the second type of string;
 ShortDateFormat:= 'yyyy:MM:dd';
 DateSeparator:= ':';
 StrToDateTime(str_date); // will trigger an exception
Is that intented or normal? Is there a method available except replacing
the ':' by '-' in the date part?

For the third type of string;
Should I convert the third string and remove the T and Z chars or are there any others methods available?

Thanks in advance.

Reply by email

Marco van de Voort
to Unknown author
I have a little project in which I need to convert various string encoded in various date format to TDateTime. For the same date, I'll have:

In the upcoming 2.2.2, there will be a scandatetime routine in datetuils. One could copy this code also to 2.2.0 projects though.

Search for scandatetime in

http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/rtl/objpas/dateutil.inc?view=markup

Have a look at the documentation for formatdatetime and the comments in the source to get an idea how to use it.

Reply by email

Graeme Geldenhuys
to Unknown author: I have a little project in which I need to convert various string encoded in various date…

I would say the easiest would be to extract the different parts: Year, Month, Day, Hour, Min, Sec and then use EncodeDateTime() to return a TDateTime type.

I have implemented (with unit tests) the last format (ISO 8601) in the tiOPF project. I use that for storing my dates in a database. See the tiUtils.pas unit in the tiOPF project on SF.net:
http://tinyurl.com/69a5nf

See the following functions:
tiDateTimeAsIntlDateStor
tiDateTimeAsIntlDateDisp
tiIntlDateStorAsDateTime
tiIntlDateDispAsDateTime

Hope this helps...

Regard,
- Graeme -

2008/6/6 :

Show quoted text 29 lines
Hello, I have a little project in which I need to convert various string encoded in various date format to TDateTime. For the same date, I'll have: '2008-05-10 21:49:21' '2008:05:10 21:49:21' '2008-05-10T19:49:21Z' I am no expert with date manipulation so I am asking: For the first type of string; DateSeparator:= '-'; ShortDateFormat:= 'yyyy:MM:dd'; StrToDateTime(str_date); //gets converted. For the second type of string; ShortDateFormat:= 'yyyy:MM:dd'; DateSeparator:= ':'; StrToDateTime(str_date); // will trigger an exception Is that intented or normal? Is there a method available except replacing the ':' by '-' in the date part? For the third type of string; Should I convert the third string and remove the T and Z chars or are there any others methods available? Thanks in advance.

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email