Solved

Format and unformat TDateTime

Posted on 2006-11-20
10
313 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: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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

864 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

19 Experts available now in Live!

Get 1:1 Help Now