Solved

Date / Time conversion

Posted on 2004-10-05
7
433 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 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
Industry Leaders: 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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 358
code issue 8 185
Delphi Seattle: Project Options : Version info for all platforms, all releases? 2 113
Delphi android app hide keyboard 3 45
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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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 …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 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