Doubt with return time formatted Asp.net mvc

Asked

Viewed 415 times

1

In the application I have a query that compares the current server time with a pre-set time - saved in a table:

select  CAST(HORA_FECHAMENTO AS datetime) as HORA_FECHAMENTO, 
 CAST(CONVERT(VARCHAR(11),GETDATE(),8) as datetime) as HORA_ATUAL 
 from TB_ESTRACAO  
 where IDEXTRACAO = 1 

When I send this information to the screen I am formatting as follows:

Session["hora_atual_servidor"] = String.Format("{0:T}", retornoHoraAtual.HORA_ATUAL);
Session["hora_final_valida"] = String.Format("{0:T}", retornoHoraAtual.HORA_FECHAMENTO);

The application running on the local machine shows me the information:

inserir a descrição da imagem aqui

On the server, the information and view in this way:

inserir a descrição da imagem aqui

To complete the server returns me the wrong time with a difference of nearly 40 minutes unless the current time is when it is 22:00 shows how 10:00 PM, how I could solve this problem?

  • The hosting is vbmahospedagem, the best price is the worst support of the internet, in all frankness.

  • Well, since you can’t switch servers... you already tried to get the CURRENT TIME of the ISS SERVER and not SQL?

  • @Paulohdsousa, I will change server, because this one is complicated to work, more and as the formatting of the date, know why?

  • It may be that your machine is configured PT-BR and the server you are using is American (American is more common)... Has a place to change in SQL.

  • yes, on the server this in English, as I have the closing date registered, I use the time so 20:00 is no 08:00 PM, I would do this formatting?

  • Yes, Voce can do on the server side... on C#

Show 1 more comment

1 answer

2


This problem is because of Globalization, you have two options to solve this, change the Cultureinfo that your application is running, then it will change to all calls, ie you should realize that the thousand separator is ',' and the decimal separator is '.', because the culture must be running in English.

To change for the entire application, you can change on web.config as link https://msdn.microsoft.com/en-us/library/ydkak5b9(v=vs.71). aspx

Or change only in the formatting of this return, as row below:

 string.Format(System.Globalization.CultureInfo.GetCultureInfo("pt-BR"), "{0:T}", DateTime.Now)

About the difference of 40 minutes is given why the server where your database is must be in another Timezone.

You can recover the local time based in Brasilia, this way:

Session["hora_atual_servidor"] = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("E. South America Standard Time")); //"E. South America Standard Time" é Brasília

I hope I’ve helped.

  • Very grateful for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.