Click to See Complete Forum and Search --> : Client date/time
DaddyGweedo
May 4th, 2004, 12:33 PM
I think this is something I should know, but I'll ask anyway.
I'm developing a web app that has a calendar function.
I need to know how to get the local user's date and time instead of the Server date and time.
How do I go about doing this?
Thanks!
criszepp
May 11th, 2004, 03:55 AM
The only way i see to get the client date and time is using the client side script ( JavaScript or VB Script ).
a short JavaScript example is using the Date object. This should be supported by all IE ( 3.0 or higher ). A short sample:
var date = new Date();
alert( date.toString() );
The Date object has a lot of methods u can use... Check the JavaScript documentation.
Have fun!
Cristi
DaddyGweedo
May 13th, 2004, 02:12 PM
I can get the local time to display, but I need it to do a database query with the code that sits server side.
It seems like it should be that hard. :-(
criszepp
May 17th, 2004, 03:22 AM
Hi !
In order to get the clinet date / time in the server side code u must do a post from the aspx ( html ) page. A simple way to do it is to load the client date into a hidden input field ( HTML ) and then post it. Then u can get this string in the server side as Request["hidClientDate"] ( where hidClientDate is the name of that hidden input tag ), parse it and use it in your queries ( u can send it to a webservice or whatever u want to do with it ).
Regards,
Cristi
PS: sorry for the delayed answear....
DaddyGweedo
June 9th, 2004, 05:15 PM
I don't have much experience with client side java scripting. Could you please post the sample asp code to do this?
Thanks,
criszepp
June 10th, 2004, 03:15 AM
Hi!
Ok, u said u have the date for display. Then you can have it in an hidden input, like this:
<form id="myForm">
...
<input type=hidden id="hidDate" name="hidDate" >
<script>
var date = new Date();
myForm.hidDate.value = date.toString()
</script>
...
<input type=submit value="send to server" >
</form>
Then on server side, in PageLoad:
string sClientDate = "";
if( Request["hidDate"] != null )
sClientDate = Request["hidDate"];
there it is. if u don't want the date to be sent on server side after a button is clicked, put a myForm.submit(); in the script on client side.
regards,
Cristi
DaddyGweedo
June 10th, 2004, 11:03 AM
Thanks!
After much hunting last night I found out how to do do it in a different, but similar fashion:
<script>
var right_now=new Date();
window.document.all["TextBox1"].value = right_now
</script>
where TextBox1 can be a HTML control or a Webform Control.
Now I just have to deal with my organization issues. I don't use a a button, but instead I post with a query string to a different page. No buttons and I don't think that MyForm.Submit() will work with an image map.
I come from the Windows programming world. I need to take a class in basic ASP/Javascript.
Thanks again for all of your help!
criszepp
June 10th, 2004, 11:08 AM
Nice to help you. I don't know if it works with image map, but u didn't say u had one :) . Or maybe it was me not reading carefully. Good luck with your work, and post any further questions here.
Regards,
Cristi
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.