| | |
Display client's PC Date & Time
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
Use a Javascript,
Cuz this runs in the client browser.
Check out this code..
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.
Cuz this runs in the client browser.
Check out this code..
ASP.NET Syntax (Toggle Plain Text)
var Days = new Array('Sunday','Monday','Tuesday','Wednesday', 'Thursday','Friday','Saturday'); var today = new Date(); var Year = takeYear(today); var Month = leadingZero(today.getMonth()+1); var DayName = Days[today.getDay()]; var Day = leadingZero(today.getDate()); var Hours = leadingZero(today.getHours()); var Minutes = leadingZero(today.getMinutes()); var Seconds = leadingZero(today.getSeconds()); function takeYear(theDate) { x = theDate.getYear(); var y = x % 100; y += (y < 38) ? 2000 : 1900; return y; } function leadingZero(nr) { if (nr < 10) nr = "0" + nr; return nr; }
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
•
•
Join Date: Sep 2005
Posts: 1
Reputation:
Solved Threads: 0
<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>
<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>
•
•
Join Date: Jul 2008
Posts: 1
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Access Date & Time Validation Question (MS Access and FileMaker Pro)
- Program to display System date and Time in VC++ 6.0 (C++)
- DNS ERROR>>>CANNOT CONNECT TO SECURE SITES (Viruses, Spyware and other Nasties)
- arrange date&time column (MySQL)
Other Threads in the ASP.NET Forum
- Previous Thread: store files in database
- Next Thread: update the whole table
| Thread Tools | Search this Thread |
.net 2.0 activexcontrol advice ajax alltypeofvideos asp asp.net bc30451 bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn checkbox child click commonfunctions compatible confirmationcodegeneration content contenttype courier css dataaccesslayer database datagrid datagridview datagridviewcheckbox datalist deadlock development dgv dropdownlist dropdownmenu edit expose feedback flash flv form formatdecimal forms formview gridview homeedition iframe iis javascript jquery listbox login menu microsoft mono mouse mssql multistepregistration news numerical objects order panelmasterpagebuttoncontrols parent radio ratings reportemail rotatepage save schoolproject search security serializesmo.table silverlight smartcard software sql-server sqlserver2005 suse textbox tracking typeof unauthorized validation vb.net video videos virtualdirectory vista visual-studio visualstudio web webapplications webarchitecture webdevelopemnt webprogramming webservice xml youareanotmemberofthedebuggerusers





