Click here to Skip to main content
13,052,558 members (61,404 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Help How to get server date in c#
Posted 23-Jan-13 18:29pm
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.
   
And also because server is not a palm :-)
PIEBALDconsult 24-Jan-13 0:42am
   
Dang, we can't vote on these?
   
Thank you, but I rarely see sense of humor here. As you know, we can vote if this is an "answer", but an humorous answer, even a good one, more typically heavily down-voted :-)

However, you are the one who probably knows: if I strongly like the idea to post something as an answer, I'll never hesitate, even at the expense of any down-votes. :-)

—SA
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..
   
And?... even 16, or 133... :-)
Member 8653959 24-Jan-13 1:14am
   
through ip address or domain name can we get server date time?
PIEBALDconsult 24-Jan-13 1:42am
   
No.
   
I commented on it below my answer. Actually, this is just misunderstanding of what is IP and domain and how they work...
Everything is done via some programming agents. For example, DNS...
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
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..
   
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?
   
??? 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.
   
Of course. That's why I mentioned UtcNow instead of Now. It's the best to use UTC in all cases. This way, the client part needs to know only its local time zone (always known unless the system is administered by some idiot :-) and server's time zone is totally irrelevant. Logical, isn't it?
—SA
abel83 26-Apr-15 23:54pm
   
Hi Sir, based on your explanation, could I assume that the datetime we get using System.DateTime.Now or System.DateTime.UtcNow is generated from where the application is hosted on the server?
   
The time stamp will be generated according to the time set up of the service's host, yes. If two services in different time zones are setup correctly, its UtcNow result taken at the same time should be the same...
—SA
abel83 27-Apr-15 2:16am
   
Can I assume that if I have 2 servers, then I have 2 transaction running at the same time. Each transaction would only ping a server to get the datetime at one time. If first transaction ping the server 1 and get the datetime follow by second transaction ping the server 2 to get the datetime. If both servers is sync. then by right datetime for second transaction should be slower than first trasaction. If the servers are out of sync., let say server 2 is slower than server 1 by 5 minutes, then would the datetime returned from server 2(for second transaction) is slower than first transaction(which datetime is get from server 1)?
   
There is no such thing as "same time". For setting system time over the network, Network Time Protocol (NTP) is used, please read on it. Ping and ICMP protocol are not designed for this purpose and are not suitable.
—SA
abel83 27-Apr-15 2:20am
   
Both transaction will use the System.DateTime.UtcNow
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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).
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

String currDateTime = System.DateTime.Now.ToString();
  Permalink  
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?
   
ToString? What a shame. How can all of you get to the idea of using strings representing data instead of data?
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Try the below code.Write the code in Load method,
textBox1.Text = DateTime.Now.ToShortDateString();
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 23 Dec 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100