Solved

Format and unformat TDateTime

Posted on 2006-11-20
10
320 Views
Last Modified: 2010-04-04
I have a function:



function DateToXMLStr(aDateTime: TDateTime): string;
var
 d : Char;
begin
  d:= DateSeparator;
  DateSeparator:= XML_DATE_SEPARATOR;

  if aDateTime = 0 then
  begin
    Result:= ''
  end else
  begin
    Result := FormatDateTime(XML_DATE_FORMAT,aDateTime);
  end;
  DateSeparator:= D;  
end;



To get the Date on a proper format in my xml.

How can i do the reverse?  Unformat it back to a TDateTime?

0
Comment
Question by:wqclatre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 17979024
maybe something like (not tested):

function XMLStrToDate(aDateTime: string): TDateTime;
var
 d : Char;
begin
  d:= DateSeparator;
  DateSeparator:= XML_DATE_SEPARATOR;

  if aDateTime = '' then
  begin
    Result:= 0
  end else
  begin
    DateTimeToString( Result, XML_DATE_FORMAT, aDateTime);
  end;
  DateSeparator:= D;  
end;
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 17979037
you will have to do it manually by getting the rigth parst from your string in XML_DATE_FORMAT and using EncodeDateTime()
If you tell me what XML_DATE_FORMAT is I'll make you a start.
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 17979056
too late :)
cool I didn't know of DateTimeToString (strange name btw, how about StringToDateTime)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:wqclatre
ID: 17979251
I think I was to early to Grade this:

DateTomeToString looks like:


IE IT expect a String not a TDateTime as the first parameter:

I solved it by:
var
 FS : TFormatSettings;
 LCID : Integer;
begin
  LCID:= GetUserDefaultLCID;
  GetLocaleFormatSettings(LCID,FS);
  FS.DateSeparator:= XML_DATE_SEPARATOR;
  FS.ShortDateFormat:= XML_DATE_FORMAT;
  Result:= StrToDateTime(aDateTimeString,FS)
0
 
LVL 28

Expert Comment

by:2266180
ID: 17979273
>IE IT expect a String not a TDateTime as the first parameter:

of course it does: you did ask for the reverse, did you not? reverse of
function DateToXMLStr(aDateTime: TDateTime): string;
is
function XMLStrToDate(aDateTime: string): TDateTime;

so I don't see where the problem is. StrToDateTime also takes a string as first parameter.
0
 
LVL 28

Expert Comment

by:2266180
ID: 17979295
oh. my bad. you are correct. I just looked through the help and don't know what in the world I was thinking when copy pasted datetimetostring.
0
 
LVL 2

Author Comment

by:wqclatre
ID: 17979299
Result : TDateTime;

Try sending in result into StringToDateTime ????????

DateTimeToString( Result, XML_DATE_FORMAT, aDateTime)  ??????


Won't work....


0
 
LVL 28

Expert Comment

by:2266180
ID: 17979303
btw, you should post a comment in community support asking for a reopan and paq with points refund.
0
 
LVL 2

Author Comment

by:wqclatre
ID: 17979313
Posted at the same time here.

Well I did the same. Graded without reading so you are not the only one here ;-)

However. Problem solved.....
0
 
LVL 1

Accepted Solution

by:
kodiakbear earned 0 total points
ID: 18003994
Closed, 500 points refunded.
kb
Experts Exchange Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question