如何在javascript中获取服务器日期时间的代码 [英] how to get code for server date time in javascript
本文介绍了如何在javascript中获取服务器日期时间的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
先生,
如何在Java脚本中获取服务器日期时间的代码
我正在通过访问服务器时间使用客户端系统.
sir,
how to get code for server date time in java script
i am using client system, through access server time.
推荐答案
由于隐藏字段在任何Web应用程序中都可用,因此您可以在页面中嵌入隐藏字段并设置其隐藏字段服务器端的值,并在客户端javascript中使用它.所以这是一个示例ASP.NET解决方案:
Since hidden fields are available in any web application so you can embed a hidden field in your page and set its value in server side and use it in client side javascript. so here is a sample ASP.NET solution :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testdate.aspx.cs" Inherits="testdate" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body onload="writeServerdDT()">
<script>
function writeServerdDT() {
document.write(document.getElementById('serverDateTime').value);
}
</script>
<form id="form1" runat="server" >
<asp:HiddenField ID="serverDateTime" runat="server"/>
<div>
</div>
</form>
</body>
</html>
和CodeBehind:
and the CodeBehind :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class testdate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
serverDateTime.Value = DateTime.Today.ToString();
}
}
希望它能对您有所帮助.
Hope it helps.
嗯...这可能并不完全可靠,但是您可以使用AJAX向服务器请求某些内容,然后从响应头中获取服务器时间:
http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders-method [ ^ ]
Well... this might not be totally reliable, but you could use AJAX to request something from the server, then get the server time from the response header:
http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders-method[^]
这篇关于如何在javascript中获取服务器日期时间的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文