Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Help How to get server date in c#
Posted
Comments
Zoltán Zörgő 24-Jan-13 0:35am    
1) From client side?
2) With a web forms or win forms application?
Member 8653959 24-Jan-13 1:03am    
hi... i have 6 systems (5 client & 1 Server) in network..
i want server date time & i developing application in client system win forms c#
frame work 3.5 vs 2008..
pls help
PIEBALDconsult 24-Jan-13 0:37am    
Servers won't date you; just accept it. It's not that you're not smart enough or cute enough, it's just you are easily distracted by other servers.
Sergey Alexandrovich Kryukov 24-Jan-13 0:38am    
And also because server is not a palm :-)
PIEBALDconsult 24-Jan-13 0:42am    
Dang, we can't vote on these?

You not gonna believe that: System.DateTime.Now, or, better, System.DateTime.UtcNow. :-)

Whatever you do: http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

—SA
 
Share this answer
 
Comments
Member 8653959 24-Jan-13 1:07am    
hi... i have 6 systems (5 client & 1 Server) in network..
i want server date time & i developing application in client system win forms c#
frame work 3.5 vs 2008..
Sergey Alexandrovich Kryukov 24-Jan-13 1:49am    
I already answered. What's unclear?
—SA
Member 8653959 24-Jan-13 1:14am    
through ip address or domain name can we get server date time?
Sergey Alexandrovich Kryukov 24-Jan-13 1:49am    
??? How so? From where? Do you really understand what you are talking about. IP is just a record, it has nothing before you connect and receive any package...

You have everything you need.
Make a service which responds with its date or DateTime. Make a Web page or an element on it doing the same.
How can it be a problem at all?
—SA
PIEBALDconsult 24-Jan-13 1:47am    
.Now relies on the settings for the logged-on user. Each user could set a different time zone.
If the server is a database server, and your clients all access that database, then you should be able to get the time from the database, e.g. GETDATE() in SQL Server. Otherwise you'll want UTC. I don't think anything else will be guaranteed to be what you want.

At my current job I access many servers across the U.S., possibly around the world. For consistency, they are all set to "U.S. Central time" (UTC-6:00). But my settings specify Arizona (UTC-7:00) so I see that. Another user might see yet another time.

So, you need to use UTC or have all your clients ask a common process (the database service for instance).
 
Share this answer
 
These answers are partial and unclear.
One suggestion was to set up a web service that allows the server to send out UTC time. This is the best answer, but you should note that if you have two servers they need to stay in sync. All servers get their time updated/maintained from time servers, typically Microsoft or other NTP servers. Time should always be updated on server to stay in sync, you can not expect your server to be the time standard for the world. So that needs to come from the network or NTP devices that manage that from an atomic clock.

You app could then get the needed time from your server when you write records. There is no easy technique for getting server side time from a code behind page. That is why the service was suggested. The web service would pull the time from the server and send it back to the client in the call to the web service. Then you could use that servers time to update, but note that the time returned is not going to keep ticking. You will need to keep an internal clock if your process runs long before you write your record to your database.

Example, you query your web service for the UTC time and it returns 12:01:44:025, twelve hours, 1 minute, 44 seconds, 25 milliseconds. If your processing takes say :200 milliseconds, you would want to increase your milliseconds to :225 before writing the record to your database. The timestamp value you get back from your web service will not keep ticking up 200 seconds while you are processing.

So if that level of accuracy is important you can manage it. By getting another timestamp from the local PC when you receive the timestamp from the server, then subtracting that from the current time just prior to storing your processed data, but first adding that difference to the server timestamp that was returned from the service.

Sounds like a lot of over head. But its not that difficult to implement, other than the web service if you are not familiar with it.
 
Share this answer
 
v2
Comments
CHill60 23-Dec-15 10:59am    
I actually think that Solutions 1 and 3 are complete and clear. At least they were posted in a timely fashion - 3 years ago.
You mention "would want to increase your milliseconds to :225 before writing the record to your database" ... personally I would have the database on the server and just use GetDate() to be even more accurate.
String currDateTime = System.DateTime.Now.ToString();
 
Share this answer
 
Comments
Member 8653959 24-Jan-13 1:08am    
Above coding is local system date time..
hi... i have 6 systems (5 client & 1 Server) in network..
i want server date time & i developing application in client system win forms c#
frame work 3.5 vs 2008..
Member 8653959 24-Jan-13 1:15am    
through ip address or domain name can we get server date time?
Sergey Alexandrovich Kryukov 27-Apr-15 0:11am    
ToString? What a shame. How can all of you get to the idea of using strings representing data instead of data?
—SA
Try the below code.Write the code in Load method,
textBox1.Text = DateTime.Now.ToShortDateString();
 
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