Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I have a winform which is created in vs2010.
Here I want to display server date and time.

But Time in running Mode just like clock.

I have window Dedicated Server with mssql install.

I get the date and time when form is load but i want to continus display time in running just like a system time.

But If i use timer and add 1 seconds on every tick its time we will be increase from current server time approx 2-3 min after some time.

I want to real update time from server. But I do not want to exqute sql query per second to get time. its create a big problem for me..

Please suggest me that I want to make a real clock on client machine
means machine and server time are little bit same.

thanks
Ram Kumar
Posted
Comments
ZurdoDev 17-Nov-14 12:34pm    
You want the server time but don't want to execute sql to get it?
BillWoodruff 17-Nov-14 12:35pm    
Why don't you synchronize your local machine's time-setting with the server every some-number-of-minutes ? They are not likely to get out of synch in a time-frame of minutes.
Maciej Los 17-Nov-14 13:31pm    
Why? Why? and Why?

You can't get any information from SQL server unless you ask for it: and a SELECT query is pretty much the only way to do that. So try:
SQL
SELECT GETDATE()


But please, don't do it per second!
Why not just read it once when you app starts, and store it as:
C#
DateTime serverTime = GetDateAndTimeFromSQL();
TimeSpan serverDiff = DateTime.Now - serverTime;

You can then use the TimeSpan value to work out what the server time is by adding the difference onto the current date and time for your machine. A re-check every couple of hours should keep them nicely in sync.
 
Share this answer
 
Try these..

Get current date time from server and convert it into local time in c#
C#
DateTime serverTime = DateTime.Now; // gives you current Time in server timeZone
DateTime utcTime = serverTime.ToUniversalTime; // convert it to Utc using timezone setting of server computer

TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, tzi); // convert from utc to local

to check it locally , change your computer timezone to match your server. then run the code. I check and it's working fine.
the first two lines can be mixed into one line as below . that has a better performance :
C#
DateTime utcTime = DateTime.UtcNow;


Or See this links.

Link1[^]

Link2[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900