Solved

Date / Time conversion

Posted on 2004-10-05
7
431 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending Gmail through Delphi 3 108
Intraweb download file link ? 1 134
URL for downloading Google Chrome for Win XP 2 158
LAN or WAN ? 11 92
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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 …

809 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