Function datetime on differents contry like brazil and EUA

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????
LVL 1
rafaelrglAsked:
Who is Participating?
 
MerijnBSr. Software EngineerCommented:
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
 
MerijnBSr. Software EngineerCommented:
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
 
rafaelrglAuthor Commented:
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
MerijnBSr. Software EngineerCommented:
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
 
Scay7Commented:
Why not try use it this way...

StrtoDateTime(FormatDateTime('MM/dd/yyyy hh:nn:ss AM/PM',now);
0
 
rafaelrglAuthor Commented:
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
 
rafaelrglAuthor Commented:
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
 
rafaelrglAuthor Commented:
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
 
Scay7Commented:
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
 
rafaelrglAuthor Commented:
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
 
rafaelrglAuthor Commented:
don't worry, i got it this, wait until i tested.
0
 
rafaelrglAuthor Commented:
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
 
MerijnBSr. Software EngineerCommented:
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
 
rafaelrglAuthor Commented:
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
 
Scay7Commented:
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
 
rafaelrglAuthor Commented:
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
 
rafaelrglAuthor Commented:
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
 
MerijnBSr. Software EngineerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.