User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 402,049 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,554 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting

Display client's PC Date & Time

Join Date: Jul 2008
Posts: 1
Reputation: senmm1981 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
senmm1981 senmm1981 is offline Offline
Newbie Poster

Re: Display client's PC Date & Time

  #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  
All times are GMT -4. The time now is 11:42 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC