前端代码:
//获取UTC即时日期时间并转换为js日期类型
function GetUTCDateTime()
{
var ajaxPage = "GetServerDateTime.ashx?Ticikes=" + new Date().getMilliseconds();
var options = {
type: "POST",
url: ajaxPage,
data: "",
contentType: "application/json;charset=utf-8",
dataType: "string",
async: false,
success: function(response) {
},
error: function(msg) { return false; }
}
var responseText = $.ajax(options).responseText;
var serverDateTime = new Date(Date.parse(responseText.replace(/-/g, "/")));
return serverDateTime;
}
通过handler异步取日期:
/// <summary>
/// Summary description for $codebehindclassname$
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class GetServerDateTime : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.Write(UTCDateTime.Now.ToString());
}
public bool IsReusable
{
get
{
return false;
}
}
}