?
Solved

DELPHI - DATEDIFF FROM STRING

Posted on 2010-11-30
6
Medium Priority
?
1,264 Views
Last Modified: 2013-11-23
hi,
I used the function that is fine, now I need to use it with a date format string.

now i need...

var y,m,d,h,n,s,ms: Word;
begin
  DATAINIZIO:= Now;
  DATAFINE:= // it's in string format, for example 2011/09/24 ;
  DiffDates(DATAINIZIO, DATAFINE, y,m,d,h,n,s,ms);
end;
0
Comment
Question by:danz67
  • 4
  • 2
6 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 34237377
this depends on how you receive the string

is it from user input ? > here the language settings are of importance
is it from database > the definition probably never changes

DataFine := StrToDate(YourDateInString);

you could write your own conversion routine:


function ConvertToDate(S: String): TDateTime;
var Fmt: TFormatSettings;
begin
  GetLocaleFormatSettings(LOCALE_USER_DEFAULT, fmt);
  try
    fmt.ShortDateFormat := 'DD/MM/YYYY';
    Result := StrToDate(S, Fmt);
  except
    raise Exception.CreateFmt('String could not be converted to a date with short date format = "%s"', [fmt.ShortDateFormat]);
  end;
end;

Open in new window

0
 

Author Comment

by:danz67
ID: 34237427
I have try this, but not work.

var
  DATAINIZIO: Tdate;
  DATAFINE: TDate;
begin
  DATAINIZIO:= Now;
  DATAFINE:= ConvertToDate(Edit1.Text); // it's in string format, for example 2011/09/24 ;
  DiffDates(DATAINIZIO, DATAFINE, y,m,d,h,n,s,ms);
end;
0
 

Author Comment

by:danz67
ID: 34237431
provided the date is so, not language-dependent system
AscoltaTrascrizione fonetica
Dizionario - Visualizza dizionario dettagliatosostantivo0.laabbreviazione0.Apronome0.it0.her0.youarticolo0.the0.a0.an
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 38

Accepted Solution

by:
Geert Gruwez earned 200 total points
ID: 34237433
well your format is the following :

function ConvertToDate(S: String): TDateTime;
var Fmt: TFormatSettings;
begin
  GetLocaleFormatSettings(LOCALE_USER_DEFAULT, fmt);
  try
    fmt.ShortDateFormat := 'YYYY/MM/DD';
    Result := StrToDate(S, Fmt);
  except
    raise Exception.CreateFmt('String could not be converted to a date with short date format = "%s"', [fmt.ShortDateFormat]);
  end;
end;

Open in new window

0
 

Author Comment

by:danz67
ID: 34237435
sorry..google translate :(

 provided the date is so, not language-dependent system
0
 

Author Closing Comment

by:danz67
ID: 34237453
great
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

850 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