Display client's PC Date & Time

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 26
Reputation: aish is an unknown quantity at this point 
Solved Threads: 0
aish's Avatar
aish aish is offline Offline
Light Poster

Display client's PC Date & Time

 
0
  #1
Sep 17th, 2005
I have a asp.net c# web form, when the page load I want to display client computer date and time.I used DateTime.Now.Date() for this. it works well in the localhost, but after I deployed in the web server it displays web server date and time.how can I display client's computer date and time? :o
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 175
Reputation: Letscode is an unknown quantity at this point 
Solved Threads: 6
Letscode's Avatar
Letscode Letscode is offline Offline
Junior Poster

Re: Display client's PC Date & Time

 
0
  #2
Sep 21st, 2005
Use a Javascript,
Cuz this runs in the client browser.
Check out this code..

  1. var Days = new Array('Sunday','Monday','Tuesday','Wednesday',
  2. 'Thursday','Friday','Saturday');
  3.  
  4. var today = new Date();
  5. var Year = takeYear(today);
  6. var Month = leadingZero(today.getMonth()+1);
  7. var DayName = Days[today.getDay()];
  8. var Day = leadingZero(today.getDate());
  9. var Hours = leadingZero(today.getHours());
  10. var Minutes = leadingZero(today.getMinutes());
  11. var Seconds = leadingZero(today.getSeconds());
  12.  
  13. function takeYear(theDate)
  14. {
  15. x = theDate.getYear();
  16. var y = x % 100;
  17. y += (y < 38) ? 2000 : 1900;
  18. return y;
  19. }
  20.  
  21. function leadingZero(nr)
  22. {
  23. if (nr < 10) nr = "0" + nr;
  24. return nr;
  25. }

I got it from this URL

Your code is working on the server side ,so thats the reason Y you are getting date on the server.

For you code to work on the client side,You gotta code that in Javascript that runs in the client browser.
Save White Tiger
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 26
Reputation: aish is an unknown quantity at this point 
Solved Threads: 0
aish's Avatar
aish aish is offline Offline
Light Poster

Re: Display client's PC Date & Time

 
0
  #3
Sep 22nd, 2005
Thanks!.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1
Reputation: amitkr is an unknown quantity at this point 
Solved Threads: 0
amitkr amitkr is offline Offline
Newbie Poster

Re: Display client's PC Date & Time

 
0
  #4
Sep 28th, 2005
<html>
<head>
<script language="JavaScript">
function showDate()
{
alert("Welcome to Java Script","");
var dt=new Date();
var dd=dt.getDate();
var mm=dt.getMonth()+1;
var yyyy=dt.getYear();
txtDate.value=dd+"/"+mm+"/"+yyyy
txtTime.value=dt.getHours() +":"+ dt.getMinutes()+":"+dt.getSeconds();
}
function close()
{
alert("Good Bye, Waiting to see you again","");
}
</script>
</head>
<body bgcolor="pink" onLoad="showDate();" onUnload="close();">
<center>
<font face="Arial" size=5 color="red">
<b> Current Date and Time </b>
</font>
<br> <br> <br> <br> <br> <br>
Current Date:
<input type=text name=txtDate value="">
<br>
Current Time:
<input type=text name=txtTime value="">
</center>
</body>
</html>
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1
Reputation: senmm1981 is an unknown quantity at this point 
Solved Threads: 0
senmm1981 senmm1981 is offline Offline
Newbie Poster

Re: Display client's PC Date & Time

 
0
  #5
Jul 20th, 2008
In order to provide relevant content to a site visitor, I wanted to find out what time of day it was wherever the visitor was located. This would be useful to serve different content based on if its morning ("Buy Coffee!!"), lunch ("Eat lunch near you!!"), or evening ("Come rent a movie from us!!")

Using a similar approach to how viewstate is maintained during postbacks, I added a couple of methods to my base page implementation and a read-only property which returns the value of hidden input. When the base page's load method fires, it registers a hidden input and a small script that will set the input's value equal to the date of the client browser during a postback.

public class ClientTimeBasePage : Page

{



private const string CLIENTTIME_SCRIPT_ID = "__CLIENTTIMEJS";

private const string CLIENTTIME_FIELD = "__CLIENTTIME";



public string ClientTime

{

get { return this.Request.Form[CLIENTTIME_FIELD]; }

}



protected override void OnLoad(EventArgs e)

{



ClientScript.RegisterHiddenField(CLIENTTIME_FIELD, "");

if (!ClientScript.IsOnSubmitStatementRegistered(typeof(string), CLIENTTIME_SCRIPT_ID))

{

ClientScript.RegisterOnSubmitStatement(typeof(string), CLIENTTIME_SCRIPT_ID, "document.getElementById('" + CLIENTTIME_FIELD + "').value=new Date();");



}

base.OnLoad(e);



}

}

At this point, I should mention I have not tested this thoroughly but I'm pretty sure I'll run into problems when we get into different culture and locale settings across browsers. A few other considerations that come to mind is using AJAX for a similar implementation and possibly attaching the client date / time to the Request context. This seems like a more appropriate place for it - perhaps using extension methods in C# 3.0. Another option is to write some more intricate javascript to perform a little more parsing on the client-side. We should always be careful when we rely on the client's browser for input data.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC