?
Solved

Date / Time conversion

Posted on 2004-10-05
7
Medium Priority
?
435 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
[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
 
LVL 11

Accepted Solution

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

Assisted Solution

by:Ivanov_G
Ivanov_G earned 200 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 300 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
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 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

Technology Partners: 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!

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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