Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#ASP.NET
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 17-Oct-11 7:17am
Comments
SAKryukov at 17-Oct-11 14:07pm
   
Why do you need this time? I see no reason. --SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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?
  Permalink  
Comments
SAKryukov at 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
Mark Nischalke at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
SAKryukov at 17-Oct-11 14:17pm
   
Yes, but why? Normally, local client's time should not be used at all, please see my solution. --SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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

Solution 2

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

Solution 5

I got a good solution in this article and it is working great in my case
http://www.infoa2z.com/asp.net/how-to-get-the-date-time-of-client-pc-in-asp.net[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

<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
  Permalink  
Comments
CHill60 at 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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 450
1 Sergey Alexandrovich Kryukov 370
2 Shai Vashdi 318
3 OriginalGriff 265
4 Peter Leow 171
0 Sergey Alexandrovich Kryukov 9,109
1 OriginalGriff 5,290
2 Peter Leow 4,010
3 Maciej Los 3,535
4 Abhinav S 3,263


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 16 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid