客户端机器在asp.net中的日期时间 [英] Client machine date n time in asp.net

查看:85
本文介绍了客户端机器在asp.net中的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想要获取客户的机器日期&时间。



我用javascript这样做,



代码:

Hi guys,

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

I have used javascript to do so,

code:

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

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





但是,遗憾的是= =fulldate,就像字符串一样,我提供变量名称。



Plz建议我做什么...



谢谢



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

Plz suggest me what to do...

thanks

推荐答案

你是什么由于您的 fulldate 变量已初始化为客户端,但会话[CDATE]被分配给服务器端,因此无法正常工作。



创建一个页面,将您的fulldate作为查询字符串或cookie放在URL中。然后将您的页面重定向到另一个页面,从查询字符串或cookie中获取日期值,然后将其分配给会话缓存。
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.


< %%>符号可用于将服务器端代码嵌入到最终呈现的客户端页面中。

它无法在周围工作,因为< %%>的内容仍然在服务器上解析。

如果你想从客户端获得客户端的时间(时区),你只能在回发后才能这样做。阅读本文: http://www.prideparrot.com/blog/archive/2011/9/how_to_display_dates_and_times_in_clients_timezone [ ^ ]

另一种选择是使用某些第三方调查IP并决定什么时区客户端属于...
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...


您可以在客户端读取会话的值但是无法在客户端初始化/写入会话,因为它是服务器对象。请使用 HiddenField 保存客户端系统的日期,如下例所示。



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.

// 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>





//< Body>中的控件声明页面标签..



// Declaration of controls in <Body> tag of page..

<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按钮单击,您可以在隐藏字段的帮助下访问服务器端的客户端日期..





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

protected void btnGetDate_Click(object sender, EventArgs e)
   {
       Response.Write(hdnDate.Value);
   }


这篇关于客户端机器在asp.net中的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆