Solved

Format and unformat TDateTime

Posted on 2006-11-20
10
308 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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 28

Expert Comment

by:ciuly
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
 
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:ciuly
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 28

Expert Comment

by:ciuly
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:ciuly
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now