Solved

_ Str -> TDateTime ?

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

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

17 Experts available now in Live!

Get 1:1 Help Now