Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Function datetime on differents contry like brazil and EUA

Posted on 2007-07-21
18
Medium Priority
?
360 Views
Last Modified: 2010-04-06
hi experts, i have a huge problem trying to make a solution for this one, so Here in EUA the date come month first then the day, but in brazil the day comes first then the month, like this:

brazil :  22/10/1985 20:15:22 pm
eua    : 10/22/1985 20:15:22  pm

so, my problem is when the regional and language option on painel control is Portuguese(brazil) my program give allways a error converting data to english format, here is the command that i'm using:

FormatDateTime('dd/MM/yyyy hh:nn:ss AM/PM',now())

for my understand, when I put now() then comes a brazilian format, when it trys to converto to english format, then the error shows up. Is there anyway to put only english date format on my program????
0
Comment
Question by:rafaelrgl
  • 10
  • 5
  • 3
18 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 19540122
I'm not quite sure if I understand what your problem.

Now() returns the current date/time, independent of regional settings, it just returns current date/time.

If you use DateTimeToString(Now()), you will get a string formated according to the regional settings, so like:
brazil :  22/10/1985 20:15:22 pm
eua    : 10/22/1985 20:15:22  pm

With FormatDateTime you can determine what format will be used yourself, so:
FormatDateTime('dd/MM/yyyy hh:nn:ss AM/PM', Now())
will always return:
22/10/1985 20:15:22 pm
independent of your regional settings.

I hope it is clear now, please let me know.
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 19540203
when i tryed this command if the language is on Portuguese(Brazil) it show a error:

Raised exception class EConvertError with message "07/21/2007 02:42:43 pm is not a valid date and time'. Process Stopped.

 StrtoDateTime(FormatDateTime('MM/dd/yyyy hh:nn:ss AM/PM',now()))
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 19540241
this is because StrToDateTime uses the regional settings to parse the date and you give another format with FormatDateTime.
It's best not to use StrToDateTime.
What is it you try to achieve?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Expert Comment

by:Scay7
ID: 19540250
Why not try use it this way...

StrtoDateTime(FormatDateTime('MM/dd/yyyy hh:nn:ss AM/PM',now);
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 19540735
Function TFVender_Horas.ValorHoras(HORA01,HORA02 : TDateTime): Double;
var
minutos, horas : integer;
total : double;
Begin
   horas   := (strtoint(FormatDateTime('hh',HORA02 - HORA01)));
   minutos := (strtoint(FormatDateTime('nn',HORA02 - HORA01)));
   //Result := RoundTo(StrToFloat(FloatToStrF((((horas * 60) + minutos) * 0.016666666666666666666666666666667),ffFixed,10,2)),-1);
   total := ((horas * 60) + minutos) * 0.016666666666666666666666666666667;
   Result := StrToFloat(FormatFloat('0.00', total));
End;



VALOR :=  FloatToStr(ValorHoras(StrtoDateTime(FormatDateTime('MM/dd/yyyy hh:nn:ss AM/PM',now())),(StrtoDateTime(FormatDateTime('MM/dd/yyyy hh:nn:ss AM/PM',now())) + StrToDateTime(Edit_Tempo.Text))));

0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 19540742
one other question, if the regional settings is Portuguese(Brazil) and I have a date from EUA like: 10/22/1985 20:15:22  pm and i try use this command: FormatDateTime('MM',HORA02 - HORA01))); what will be the result? will be 10 or 22?
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 19540749
forgot my last comment, pls  check the function, and you will se why i'm using this command : StrtoDateTime(FormatDateTime('MM/dd/yyyy hh:nn:ss AM/PM',now()))
0
 
LVL 5

Expert Comment

by:Scay7
ID: 19540953
Not sure if this wil lead you in the right direction but here it goes,

i dont think that you need this whole line:

VALOR :=  FloatToStr(ValorHoras(StrtoDateTime(FormatDateTime('MM/dd/yyyy hh:nn:ss AM/PM',now())),(StrtoDateTime(FormatDateTime('MM/dd/yyyy hh:nn:ss AM/PM',now())) + StrToDateTime(Edit_Tempo.Text))));


rather drop a datetimepicker on the forum, and:

VALOR :=  FloatToStr(ValorHoras(now,now+datetimepicker1.DateTime));


Im not sure this is what you want but where does horas1 and horas2 come from ?
also try pasting in that line that i provided and see its result... ;P
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 19541155
one question:

help me on the var tempo01 and tempo02, something is wrong, is not datetime???

procedure TFVender_Horas.BitBtn2Click(Sender: TObject);
Var
  TEMPO01, TEMPO02 : datetime // this here is not working ?????
  VALOR, TEMPREST : STRING;
  LVCod : integer;
begin
              TEMPO01 := FormatDateTime('MM/dd/yyyy hh:nn:ss AM/PM',now());
              VALOR :=  FloatToStr(ValorHoras(now(),(now() + StrToDateTime(Edit_Tempo.Text)));
              TEMPO02 := FormatDateTime('MM/dd/yyyy hh:nn:ss AM/PM',(now() + StrToDateTime(Edit_Tempo.Text)));
                  temprest:= FormatDateTime('hh:nn:ss',(TEMPO02 - now()));
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 19541159
don't worry, i got it this, wait until i tested.
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 19541196
ok here is the final solution, becouse all my datatime on my database is formated to EUA format, so when i retreve the datatime from my database, i have to conver to the right regional settings, i mean:

If my program is running on EUA setting, then leave like it is, no need format

if my program is running on Portuguese setting, then format data to portuguese

if my program is running on anysetting that is not EUA, so change to the right format.

How to do this, change the data that i reatreve from my database to the right regional settings?

0
 
LVL 19

Expert Comment

by:MerijnB
ID: 19541926
before I (or anybody else) comes up with a solution.
What is really the issue here is that you store all your datetime's in a regional specific format (EUA in this case), is there any way you can change that?
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 19542489
like you sad above i change all my program and i'm not using strtodatetime anymore, so all my functions that i have on my program will work, but my database all datatime is save like here on EUA, month comes first than day, so that's we can't change, becouse this server is for a lot clients, i mean, clients from brazil and eua, so we can't change the format on the sql server, we when we will get the data from the database we need to check wich regional config is active and transform the data to the right format.

so?
0
 
LVL 19

Accepted Solution

by:
MerijnB earned 1200 total points
ID: 19542526
as an advice, don't store a datetime in yoru database as a string containing a regional formated datetime.
Store it either as a datetime format provided by the database, or as a float.
If you really have to store it as a string, store it as something easy parsable, like yyyymmddhhnnss (so 22/10/1985 20:15:22 pm becomes 19852210201522).

For now I think it's best to parse the string containing the datetime yourself, and try to store it as one of the other formats asap.
You can do this by changing the locale and using StringToDateTime but I really think you don't want to go there.

Try something like:

function DecodeEUADateTime(s: string): TDateTime;
// 10/22/1985 20:15:22  pm
begin
    Result := EncodeDate(StrToInt(copy(s, 7,4)),
                         StrToInt(copy(s, 1,2)),
                         StrToInt(copy(s, 4,2)));

    result := result + EncodeTime(StrToInt(copy(s, 12,2)),
                                  StrToInt(copy(s, 15,2)),
                                  StrToInt(copy(s, 18,2)),
                                  0);
end;
0
 
LVL 5

Assisted Solution

by:Scay7
Scay7 earned 800 total points
ID: 19542707
TEMPO01, TEMPO02 : datetime // this here is not working ?????

try

TEMPO01, TEMPO02 : Tdatetime; // <-- you forgot the T

http://www.delphibasics.co.uk/RTL.asp?Name=Now
Some stupid info on the now command.
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 19543447
ok, i'm going to try the function that you gave me above, but, by the way, i'm store my datatime on a field datatime from sql server 2000, so i don't have to worry about wrong formatad dates, it only accept datatime on format EUA.
0
 
LVL 1

Author Comment

by:rafaelrgl
ID: 19543497
i can't believe that i did not see this before, when i get the datatime from my database, i'm using the object query, so i was using like this Query_MaqTIMER01.text, so it comes the same value that is on the database like a string, but when i use the command like this :

DateTimeToStr(Query_MaqTIMER01.AsDateTime))

then do not matter if the regional config is brazilian or EUA, always will convert to the right regional config.

the only problem that i have it is, i get this right value from mydatabase and i pass to the listbox only when the program start, so after this the datatime is load on a listbox. then if the user change the regional config when the program is running, then it shows the error that datatime is invalid.  what i can do about this littow problem?

here is where the problem is showing now:

tempuso:= FormatDateTime('hh:nn:ss',(now() - StrToDateTime(LVMaq.Items[i].SubItems.Strings[4])));
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 19543512
I'd save the datetime as a TDateTime as well, not just as a string representation, you can't depend on that as you say yourself.
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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

564 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