Solved

DELPHI - DATEDIFF FROM STRING

Posted on 2010-11-30
6
1,230 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 37

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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 50 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Debugging a PHP Web Application 2 116
Object Data Source 4 135
Visual Studio - Accept Button gets border 3 77
Resharper CTRL+N: Differentiate between files with same name 2 71
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. …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

828 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