We help IT Professionals succeed at work.

Show client's date/time on webform?

284 Views
Last Modified: 2008-08-12
Hello Experts

How do I show a client's actual date/time (based on their pc time) on a webform?  Using something like:

Label1.Text = Now()

That's based on my server time right?  How do I display the client's date & time inside a label instead of the server time?  Javascript? VBScript?
Comment
Watch Question

Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
this is from codeproject.

I present to you a simple set of functions to display the client's date and time on your web pages.



<script language="JavaScript" type=text/JavaScript>
 
 
var dayarray=new Array ("Sunday","Monday","Tuesday","Wednesday",
               "Thursday","Friday","Saturday")
 
var montharray=new Array("January","February","March","April","May","June",
               "July","August","September","October","November","December")
 
function getthedate(){
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
var dn=""
if (hours>=12)
dn=""
if (hours>12){
hours=hours-12
}
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
//Hire change font size
var cdate="< small> < font color='ffffff' face='Arial'>"
  +dayarray[day]
  +", "+daym+" "+montharray[month]+", "+year
  +" "+hours+":"+minutes+":"+seconds+" "+dn
  +"  < /small>"
 
if (document.all)
document.all.clock.innerHTML=cdate
else if (document.getElementById)
document.getElementById("clock").innerHTML=cdate
else
document.write(cdate)
}
if (!document.all&&!document.getElementById)
getthedate()
function goforit(){
if (document.all||document.getElementById)
setInterval("getthedate()",1000)
}
 
 
 
</script>
<SPAN id=clock></SPAN>

Open in new window

Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
lambdabunker,
That's already on the link I provided :)
jpaulino: sorry but i wasnt aware of what there is in the link. i had used this solution in my development form codeproject so therefore i referred it here. Sorry.
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
No problem with that! It was just a comment.
All the inputs are valid

Author

Commented:
I'm trying to get that code to work on the link jpaulino but I'm not sure I'm using it properly.  I put the javascript in my asp page.  Then in the code-behind I put in the rest of the code?  When I do that and set my Label = clientDateTime I get nothing.
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
In the code there are a few diferent solutions. What solution are you trying ?

Author

Commented:
The accepted solution is the code I'm trying first.
IT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hmm can I do multiple "onload"'s?  I already have one on this form.

Author

Commented:
Here's what I have so far, gives me an end of statement error when the page loads.  I have another javascript above the date/time one.


<script language="JavaScript" type="text/JavaScript">
 
var dayarray=new Array ("Sunday","Monday","Tuesday","Wednesday",
               "Thursday","Friday","Saturday")
 
var montharray=new Array("January","February","March","April","May","June",
               "July","August","September","October","November","December")
 
function getthedate(){
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
var dn=""
if (hours>=12)
dn=""
if (hours>12){
hours=hours-12
}
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
//Hire change font size
var cdate=dayarray[day]
  +", "+daym+" "+montharray[month]+", "+year
  +" "+hours+":"+minutes+":"+seconds+" "+dn
 
if (document.all)
document.all.clock.innerHTML=cdate
else if (document.getElementById)
document.getElementById("clock").innerHTML=cdate
else
document.write(cdate)
}
if (!document.all&&!document.getElementById)
getthedate()
 
function goforit(){
if (document.all||document.getElementById)
setInterval("getthedate()",1000)
}
 
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
    <title>Print Voucher</title>
</head>
<body onload="goforit();go()" style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; overflow: hidden; clip: rect(0px 0px 0px 0px); padding-top: 0px; clear: none; table-layout: fixed; display: block; left: 0px; background-image: none; visibility: visible; width: 0px; cursor: crosshair; position: absolute; top: 0px; height: 0px" >
<object id="WebBrowser1" style="display:none" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>
    <form id="form1" runat="server" >
        <img src="https://www.mysite.com/FuelTracker/client logos/gwpflogo.gif" style="Z-INDEX: 112; LEFT: 0px; TOP: 0px; margin: inherit; padding-top: 0px; position:absolute" width="298px" alt="" />
        <asp:label runat="server" id="Label2" style="Z-INDEX: 112; LEFT: 2px; POSITION: absolute; TOP: 44px" font-size="12px" Height="55px" Width="295px" BorderStyle="Solid" BorderWidth="1px" Font-Names="Arial" ></asp:label>
        <asp:Label ID="clock" runat="server" style="Z-INDEX: 130; LEFT: 1px; POSITION: absolute; TOP: 99px" font-size="12px" Height="10px" Width="295px" BorderStyle="None" Font-Names="Arial" text=""></asp:Label>
    </form>
</body>
</html>

Open in new window

You are calling the non-existant function go() in the body onload event.
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
I don't see any function go() in your code !
If you have you can use it to call the goforit()

Author

Commented:
Actually I didn't show the top of my code which includes a function go().  So, how do I call two functions in the onload event?
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
You don't call it. You can call it (the goforit) from the go() function that you already have.

Author

Commented:
Yep, I had to remove the time pause but it works!

Thanks for the help.
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
Glad I could help and thanks for the grade
jpaulino
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.