Solved

Date / Time conversion

Posted on 2004-10-05
7
432 Views
Last Modified: 2010-04-05
I have 2 strings: one representing a date, and one representing a time. I Need to convert this to a TDateTime variable.

The Date is always in the format of : Oct 05 2004
The Time is always in the format of : 07:35PM

Is there an easy way to accomplish this?
0
Comment
Question by:rbohac
7 Comments
 
LVL 11

Accepted Solution

by:
calinutz earned 75 total points
ID: 12226268
no.
There is only the hard way.
:)
0
 
LVL 12

Assisted Solution

by:Ivanov_G
Ivanov_G earned 50 total points
ID: 12226337
there is a way, but have to create a parser from this. which is really time consuming ...

it would be better to change the regional settings the same as above and use DateToStr, TimeToStr, StrToDate, StrToTime, ReplaceDate, ReplaceTime methods...
0
 
LVL 11

Assisted Solution

by:calinutz
calinutz earned 75 total points
ID: 12226463
You must split your string considering your format
var
 s,t:string;
 mym,myd,myy:word;
begin
...
d:='Oct 05 2004';
t:='07:35PM';

mymonth:=s[1]+s[2]+s[3];
myday:=s[4]+s[5];
myyear:=s[7]+s[8]+s[9]+s[10];

If mymonth='Oct' then mym:=10;
If mymonth='Nov' then mym:=11;
If mymonth='Dec' then mym:=12;
If mymonth='Ian' then mym:=1;
If mymonth='Feb' then mym:=2;
If mymonth='Mar' then mym:=3;
If mymonth='Apr' then mym:=4;
If mymonth='May' then mym:=5;
If mymonth='Jun' then mym:=6;
If mymonth='Jul' then mym:=7;
If mymonth='Aug' then mym:=8;
If mymonth='Sep' then mym:=9;

myy:=StrToInt(myyear);
myd:=StrToInt(myday);

// So this is the hard way I mentioned. Now you have the year, the month  and the day. You can now convert it to date format.
//Do the same with the time and combine them to get the format you want
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:calinutz
ID: 12226588
But your program may run on different machines with differnt timeformats just as Ivanov G said. So first you should do a change in the regional settings and set a format that you will use as long as your pprogram runs and onclose change settings back.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12226615
you could try to set the application-wide shortDateFormat/shortTimeFormat variables to your scheme like

ShortDateFormat := 'mmm dd yyyy';
DateSeparator := ' ';
ShortTimeFormat := 'hh:mm'
TimeAMString := 'AM';
TimePMString := 'PM';
TimeSeparator:= ':';
//now a conversion should work
ADateTimeVar := StrToDateTime(DateS+' '+TimeS);

if you have d7, then you could use the overloaded function
with the TFormatSettings-Type

just from head

meikl ;-)
0
 
LVL 6

Author Comment

by:rbohac
ID: 12226670
I ended up just writing a parser after the first two posts to this question. It ended up being quite similar to the post by calinutz

function CustomDateTimeToDateTime(dt,tm:String):TDateTime;
var
  m : String;
  d : String;
  y : String;
  Temp : String;
begin
  {dates are in the format of "mmm dd yyyy"}
  Temp := UpperCase(Copy(dt,1,3));
  dt := Copy(dt,4,99);
  if Temp = 'JAN' then M := '01';
  if Temp = 'FEB' then M := '02';
  if Temp = 'MAR' then M := '03';
  if Temp = 'APR' then M := '04';
  if Temp = 'MAY' then M := '05';
  if Temp = 'JUN' then M := '06';
  if Temp = 'JUL' then M := '07';
  if Temp = 'AUG' then M := '08';
  if Temp = 'SEP' then M := '09';
  if Temp = 'OCT' then M := '10';
  if Temp = 'NOV' then M := '11';
  if Temp = 'DEC' then M := '12';
  dt := M + Dt;
  dt := Copy(StringReplace(dt,' ','/',[rfReplaceAll]),1,10);
  try
    Result := DateOf(StrToDate(dt))+TimeOf(StrToTime(tm));
    except
    Result := 0;
    end;
end;
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12227775
What's wrong with using ShortMonthNames[] btw?

uses SysUtils;

function MonthStrToMonth( const Value: string ): Integer;
begin
  Result := High( ShortMonthNames );
  while ( Result >= Low( ShortMonthNames ) ) and not AnsiSameText( Value, ShortMonthNames[ Result ] ) do
    Dec( Result );
end;

One nice gem of code, free of charge. :-)
Now, all you have to do is use StrToInt to convert the day and year to proper numbers to, and use some encode function to convert it to a TDateTime type. :-P
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 317
Delphi XE10 Round Image 2 158
Delphi IDE crash without error message ... 7 99
Press three keys together and trigger a function 3 60
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 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