Solved

Format and unformat TDateTime

Posted on 2006-11-20
10
315 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

832 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