Solved

_ Str -> TDateTime ?

Posted on 1998-12-19
7
315 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
7 Comments
 

Author Comment

by:Wein
Comment Utility
Edited text of question
0
 

Author Comment

by:Wein
Comment Utility
Edited text of question
0
 
LVL 4

Expert Comment

by:saar2
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 5

Expert Comment

by:ronit051397
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks, the code works fine.
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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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