Solved

Date / Time conversion

Posted on 2004-10-05
7
429 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

758 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

23 Experts available now in Live!

Get 1:1 Help Now