Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

_ Str -> TDateTime ?

In Delphi, when I use FormatDateTime, I can convert a TDateTime to short string.
eg. 12/12/98 - > Dec 12 98

But how can I do the opposite?

If I have strings that vary
eg.:
2, Nov, 98;
Dec, 23, 1998;
1998, 2, 12;
November, 12, 1998;
(sometimes without the comma)

how can I convert any of these patterns to a valid TDateTime? (a command of all of them)

Thanks.

Wein
0
Wein
Asked:
Wein
1 Solution
 
WeinAuthor Commented:
Edited text of question
0
 
WeinAuthor Commented:
Edited text of question
0
 
saar2Commented:
Try this one:

======Copied from Delphi's help=======
StrToDateTime Function
   
 Unit
 
 SysUtils
 
 Declaration

 
 function  StrToDateTime( const  S:  string ):  TDateTime ;
 
 Description
 
 The StrToDateTime function converts a string into a date and time format. The string specified as the S parameter must be in the  MM/DD/YY HH:MM:SS format unless the value of the value of the  date and time typed constants  has changed. Specifying AM or  PM as part of the time is optional, as are the seconds. You should use 24-hour time (7:45 PM is entered as 19:45, for example) if  you don't specify AM or PM.  
 
 Note:         You must use another format to specify a date and time string if you change the value of the some of the  date and time typed  constants .  
 
 ======Copied from Delphi's help=======

Saar
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!

 
ronit051397Commented:
Use EncodeDate. From Delphi Help File:
"...EncodeDate returns a TDateTime type for a specified Year, Month, and Day.

Unit

SysUtils

Category

date/time routines

function EncodeDate(Year, Month, Day: Word): TDateTime;

Description

The EncodeDate function returns a TDateTime type from the values specified as the Year, Month, and Day parameters.
The year must be between 1 and 9999.
Valid Month values are 1 through 12.
Valid Day values are 1 through 28, 29, 30, or 31, depending on the Month value. For example, the possible Day values for month 2 (February) are 1 through 28 or 1 through 29, depending on whether or not the Year value specifies a leap year.
If the specified values are not within range, an EConvertError exception is raised. The resulting value is one plus the number of days between 12/30/1899 and the given date..."
Example:
This example uses a button and a label on a form. When the user clicks the button, a specified date is encoded as a MyDate variable of type TDateTime. MyDate is then displayed as a string in the caption of the label.

procedure TForm1.Button1Click(Sender: TObject);

var
  MyDate: TDateTime;
begin
  MyDate := EncodeDate(83, 12, 31);
  Label1.Caption := DateToStr(MyDate);
end;
0
 
WeinAuthor Commented:
Thanks for both answers but this is not exactly where the problem is located.

I've wanted to try these commands before.

The problem is to convert any of:
2, Nov, 98;
Dec, 23, 1998;
1998, 2, 12;
November, 12, 1998;

to a valid, and uniform string or integer, anything that has the same pattern. Then I can use StrToDateTime or EncodeDate.

Thankx. Can you provide the complete source?
Wein
0
 
viktornetCommented:
Hello Wein

Here is an example...

var
  D1, D2, D3 : TDateTime;
begin
  D1 := VarToDateTime('December 6, 1969');
  D2 := VarToDateTime('6-Apr-1998');
  D3 := VarToDateTime('1998-Apr-6');
  ShowMessage(DateToStr(D1)+' '+DateToStr(D2)+' '+DateToStr(D3));
end;

Hope this helps...

-Viktor
--Ivanov
0
 
WeinAuthor Commented:
Thanks, the code works fine.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now