Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a ASP.NET web application. My hosting server is in USA. Database is present there.
Anyone can browse the site. Now I want to get the date time of client PC. I will store it in DB and will show it in browser.
I have a label. I will show it in that label
I have searched it in Google. But did not found proper code.
Please give me the C# code.
[I am using asp.net, C#]
Thanks in advance
Rashed
Posted
Comments
Sergey Alexandrovich Kryukov 17-Oct-11 14:07pm    
Why do you need this time? I see no reason.
--SA

You can't get the date/time form the client PC using C# because the code-behind is executed on the SERVER, not the client. You can, however, use JavaScript to get the current date/time from the browser. What would be the point of storing this in a database just to display it on the page?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Oct-11 14:16pm    
Correct, there is no point. My 5. For all practical purposes, it should be server's local time translated in local time zone, that's it -- please see my solution.
--SA
[no name] 17-Oct-11 14:43pm    
Is it really necessary to post "please see my solution" on each solution presented? Can't you just say, Good answer. +5? It certainly gives the impression you are saying, Nice answer, but mine is so much better than yours you need to read it now.
royalarjun001 4-Nov-12 9:37am    
this link have the solution this not true that we can't get the Local machine following blog has the solution link to the blog
You can use JavaScript for getting the date time of client. try this:

Date time using javascript[^]

After collecting the date & time from javascript, hold this date time value in a asp:HiddenField control so this(date & time) can be read on the server. On the server, call Convert.ToDateTime() on the Text value of your HiddenField.

hope it helps :)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 17-Oct-11 14:15pm    
This is good link, my 5, but I seriously doubt local time per se is really needed. Normally, it should be server's local time translated in local time zone, that's it -- please see my solution.
--SA
Well if you dont want to use Javascript you can have a look at this link. There it will get the client's web browser date and time.

http://weblogs.asp.net/mikebosch/archive/2007/11/15/getting-the-client-browsers-date-and-time-with-asp-net.aspx[^]

Just to warn you ahead, this link will not be helpful in all conditions though. It is explained why.

To be more safe I would prefer using the Javascript to accomplish that. For an ex look at this link.

http://stackoverflow.com/questions/274826/how-to-get-client-date-and-time-in-asp-net[^]

Good luck
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Oct-11 14:17pm    
Yes, but why? Normally, local client's time should not be used at all, please see my solution.
--SA
I question if you really need local client time, ever. Well, you usually need this time just to present the time of some events translated into the client's local time zone, this is the only reasonable task. But this is time zone information, not really time itself. The server does not need anything about local time. Think about it in this way: you need to show time of some events. All events which happen happen in interaction with the server; and the server knows its local time, which should be got using System.DateTime.UtcNow, see http://msdn.microsoft.com/en-us/library/system.datetime.utcnow.aspx[^]. The time of anything else simply does not exist for the application.

On client side, you can use JavaScript method Date.getTimezoneOffset to translate UTC time into local time, see http://www.w3schools.com/jsref/jsref_gettimezoneoffset.asp[^].

Normally, that's all you really need.

—SA
 
Share this answer
 
<html>
<body>
<h4>It is now  
<script type="text/javascript">
<!--
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10){
minutes = "0" + minutes
}
document.write(hours + ":" + minutes + " ")
if(hours > 11){
document.write("PM")
} else {
document.write("AM")
}
//-->
</script>
</h4>
</body></html>


Try this code
 
Share this answer
 
Comments
CHill60 16-Jul-13 13:36pm    
This is an exact copy of the code available on the link provided in Solution 2 nearly 2 years ago. Answering old questions like this usually only attracts downvotes and/or flaming (I see someone has already downvoted your solution)

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