Solved

DELPHI - DATEDIFF FROM STRING

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

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 36

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now