Click here to Skip to main content
16,018,818 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi guys,

I want to fetch client's machine date & time.

I have used javascript to do so,

code:
XML
var currentTime = new Date();
var fulldate = currentTime.getFullYear()
     + '-' + currentTime.getMonth() + 1
     + '-' + currentTime.getDate()
     + ' ' + currentTime.getHours()
     + ':' + currentTime.getMinutes()
     + ':' + currentTime.getSeconds()
     + ':' + currentTime.getMilliseconds();

    <% Session["CDATE"] = "'" + fulldate + "'" %>


But, unfortunately sessions = "fulldate", like as string, where as i'm providing the variable name.

Plz suggest me what to do...

thanks
Posted

What you are doing won't work because your fulldate variable is initialized client side but Session ["CDATE"] is assigned server side.

Create a page that places your fulldate either in the url as a query string or in a cookie. Then redirect your page to another that gets the date value from the query string or cookie and then assign it to the session cache.
 
Share this answer
 
The <% %> notation can be used to embed server side code into the final rendered client side page.
It can not be work on the way around, as the content of <% %> parsed while still on the server.
If you want to get the client's time (time-zone) from the client you can do it only after postback. Read this article: http://www.prideparrot.com/blog/archive/2011/9/how_to_display_dates_and_times_in_clients_timezone[^]
An other option is to use some 3rd party to investigate the IP and decide to what timezone client belongs...
 
Share this answer
 
You can read the value of session at client side but can't initialized/write session at client side as it is a server object. Please use HiddenField to save the date of client system as shown below in example.

JavaScript
// Write this javascript code on <Head> tag of page and call it on window's load. 

 <script type="text/javascript">
        window.onload = function () {
            var currentTime = new Date();
            var fulldate = currentTime.getFullYear()
// here converting the month number into integer and then adding 1 to it. 
     + '-' + (parseInt(currentTime.getMonth()) + 1)
     + '-' + currentTime.getDate()
     + ' ' + currentTime.getHours()
     + ':' + currentTime.getMinutes()
     + ':' + currentTime.getSeconds()
     + ':' + currentTime.getMilliseconds();

// Save date in hidden field..
            document.getElementById('<%=hdnDate.ClientID%>').value = fulldate;
        }

I hope this solution will help you.. :)
    </script>


// Declaration of controls in <Body> tag of page..
ASP.NET
<asp:hiddenfield id="hdnDate" runat="server" xmlns:asp="#unknown" />
        <br />
        <br />
<asp:button id="btnGetDate" runat="server" text="Button" onclick="btnGetDate_Click" xmlns:asp="#unknown" />


// On Button Click, You can access client date at server side with help of hidden field..

C#
protected void btnGetDate_Click(object sender, EventArgs e)
   {
       Response.Write(hdnDate.Value);
   }
 
Share this answer
 
Comments
abdul subhan mohammed 18-May-14 9:03am    
i did d same as u have said: but still i didn't get it...
just i called this hdn value in the page load

string s = HiddenField1.Value;
lbltime.Text = s;

Javascript remains same, as urs.

plzzz suggest me
abdul subhan mohammed 18-May-14 9:06am    
i'm getting d null value in d hnd field.
Mandip Grewal 18-May-14 11:29am    
As per this code, you wouldn't be able to get the value of HiddenField in page if it is freshly loaded (property of page Page.IsPostBack returns false). You can get the client date only when same page got postback either by button or any other event.
i have redirected to some other page n i got it...

How??

C#
<script>

            function myFunction() {
                var CDate = new Date();
                var y = CDate.getFullYear();
                var m = CDate.getMonth() + 1;
                var d = CDate.getDate();
                var h = CDate.getHours();
                var mn = CDate.getMinutes();
                var s = CDate.getSeconds();
                var ms = CDate.getMilliseconds();
                var gmt = CDate.getTime();
                var fulldate = y + "-" + m + "-" + d + " " + h + ":" + mn + ":" + s + ":" + ms;
                var clientdate = Date();
                window.location = "Dashboard.aspx?CDate=" + fulldate + "&ClientDate=" + clientdate ;
            }
        </script>


Thanks to all...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900