Solved

DELPHI - DATEDIFF FROM STRING

Posted on 2010-11-30
6
1,241 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
[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
  • 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
Technology Partners: 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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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…
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

724 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