Solved

_ Str -> TDateTime ?

Posted on 1998-12-19
7
321 Views
Last Modified: 2010-04-04
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
Comment
Question by:Wein
[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
7 Comments
 

Author Comment

by:Wein
ID: 1352000
Edited text of question
0
 

Author Comment

by:Wein
ID: 1352001
Edited text of question
0
 
LVL 4

Expert Comment

by:saar2
ID: 1352002
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!

 
LVL 5

Expert Comment

by:ronit051397
ID: 1352003
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
 

Author Comment

by:Wein
ID: 1352004
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
 
LVL 10

Accepted Solution

by:
viktornet earned 70 total points
ID: 1352005
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
 

Author Comment

by:Wein
ID: 1352006
Thanks, the code works fine.
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

632 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