Javascript 在aspx页面上显示实时时钟(使用服务器时间)
现在,我正在使用updatepanel和timer在aspx页面上显示一个时钟(小时:分钟+日期),它会导致服务器端每30秒发回一次。我不喜欢这个解决方案。我想要一个JavaScript时钟,它从服务器的一个基值开始。有人能想出比下面的代码更好的替代方案吗Javascript 在aspx页面上显示实时时钟(使用服务器时间),javascript,c#,asp.net,datetime,Javascript,C#,Asp.net,Datetime,现在,我正在使用updatepanel和timer在aspx页面上显示一个时钟(小时:分钟+日期),它会导致服务器端每30秒发回一次。我不喜欢这个解决方案。我想要一个JavaScript时钟,它从服务器的一个基值开始。有人能想出比下面的代码更好的替代方案吗 <asp:UpdatePanel ID="UpdatePanelClock" runat="server"> <ContentTemplate> <asp:Lab
<asp:UpdatePanel ID="UpdatePanelClock" runat="server">
<ContentTemplate>
<asp:Label ID="lblTime2" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer2" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer2" runat="server" Interval="30000" OnTick="Timer2_Tick"></asp:Timer>
在我自己的库中,我创建了函数
getCurrentTime
。它是一个多功能的日期和时间获取和转换工具。此函数可以以微秒为单位从时间戳创建日期。它允许您创建时钟、日历、倒计时计时器等
months = "Jan:31;Feb:28;Mar:31;Apr:30;May:31;Jun:30;Jul:31;Aug:31;Sep:30;Oct:31;Nov:30;Dec:31";
function getCurrentTime(unixTimeStamp)
{
if (unixTimeStamp)
{
this.dateObject = new Date(unixTimeStamp);
}
else
{
this.dateObject = new Date();
}
// Time
this.dateObject.h = this.dateObject.getHours();
this.dateObject.m = this.dateObject.getMinutes();
if (this.dateObject.m < 10)
{this.dateObject.m = "0" + this.dateObject.m;}
this.dateObject.s = this.dateObject.getSeconds();
if (this.dateObject.s < 10)
{this.dateObject.s = "0" + this.dateObject.s;}
//Date
this.dateObject.y = parseInt(this.dateObject.getFullYear());
this.dateObject.mo = this.dateObject.getMonth()+1;
if (this.dateObject.mo < 10)
{this.dateObject.mo = "0" + this.dateObject.mo;}
this.dateObject.d = this.dateObject.getDate();
if (this.dateObject.d < 10)
{this.dateObject.d = "0" + this.dateObject.d;}
this.today = this.dateObject.d + "/" + this.dateObject.mo + "/" + this.dateObject.y;
this.now = this.dateObject.d + "/" + this.dateObject.mo + "/" + this.dateObject.y + " " + this.dateObject.h + ":" + this.dateObject.m + ":" + this.dateObject.s;
this.dateStamp = Date.parse(this.dateObject.mo+"/"+this.dateObject.d+"/"+this.dateObject.y);
//these are the objects returned by getCurrentTime() next to hour, minutes, seconds, year, month and date.
this.dateObject.fullDate = this.dateObject.d + " " + months.split(/;/)[this.dateObject.mo-1].split(/:/)[0] + " " + this.dateObject.y;
this.dateObject.timeZoneOffset = dateObject.getTimezoneOffset(); //in minutes.
this.dateObject.today = this.today;
this.dateObject.now = this.now;
this.dateObject.dateStamp = this.dateStamp;
return this.dateObject;
}
timeStamp = <%= ConvertToTimestamp(DateTime.UtcNow).ToString() %>
setInterval(clock, 1000);
function clock()
{
timeStamp += 1000;
var timeObj = getCurrentTime(timeStamp);
var timeString = timeObj.h + ":" + timeObj.m + ":" + timeObj.s;
console.log(timeString);
}
:例如
请注意,
getCurrentTime
返回的对象比显示时间的可能性更大。它可以为您提供多种日期格式。这是显示服务器时间还是客户端时间?我希望它显示服务器时间。两者都显示。要显示服务器时间,您需要获取服务器时间,并将其转换为毫秒内的时间戳
,然后将其插入getCurrentTime
函数。@RonaldinholanCode,因此替换时间戳=1420671199000代码>带有时间戳=
其中后者是检索到的服务器时间,以毫秒为单位,作为unix时间戳。中的时间戳是什么?我试过timeStamp=代码>但它不起作用,显示nothing@RonaldinhoLearnCoding我已经用C代码更新了答案,以获取正确的时间戳。这将为您提供一个基于javascript的带有服务器时间的时钟。享受。
months = "Jan:31;Feb:28;Mar:31;Apr:30;May:31;Jun:30;Jul:31;Aug:31;Sep:30;Oct:31;Nov:30;Dec:31";
function getCurrentTime(unixTimeStamp)
{
if (unixTimeStamp)
{
this.dateObject = new Date(unixTimeStamp);
}
else
{
this.dateObject = new Date();
}
// Time
this.dateObject.h = this.dateObject.getHours();
this.dateObject.m = this.dateObject.getMinutes();
if (this.dateObject.m < 10)
{this.dateObject.m = "0" + this.dateObject.m;}
this.dateObject.s = this.dateObject.getSeconds();
if (this.dateObject.s < 10)
{this.dateObject.s = "0" + this.dateObject.s;}
//Date
this.dateObject.y = parseInt(this.dateObject.getFullYear());
this.dateObject.mo = this.dateObject.getMonth()+1;
if (this.dateObject.mo < 10)
{this.dateObject.mo = "0" + this.dateObject.mo;}
this.dateObject.d = this.dateObject.getDate();
if (this.dateObject.d < 10)
{this.dateObject.d = "0" + this.dateObject.d;}
this.today = this.dateObject.d + "/" + this.dateObject.mo + "/" + this.dateObject.y;
this.now = this.dateObject.d + "/" + this.dateObject.mo + "/" + this.dateObject.y + " " + this.dateObject.h + ":" + this.dateObject.m + ":" + this.dateObject.s;
this.dateStamp = Date.parse(this.dateObject.mo+"/"+this.dateObject.d+"/"+this.dateObject.y);
//these are the objects returned by getCurrentTime() next to hour, minutes, seconds, year, month and date.
this.dateObject.fullDate = this.dateObject.d + " " + months.split(/;/)[this.dateObject.mo-1].split(/:/)[0] + " " + this.dateObject.y;
this.dateObject.timeZoneOffset = dateObject.getTimezoneOffset(); //in minutes.
this.dateObject.today = this.today;
this.dateObject.now = this.now;
this.dateObject.dateStamp = this.dateStamp;
return this.dateObject;
}
timeStamp = <%= ConvertToTimestamp(DateTime.UtcNow).ToString() %>
setInterval(clock, 1000);
function clock()
{
timeStamp += 1000;
var timeObj = getCurrentTime(timeStamp);
var timeString = timeObj.h + ":" + timeObj.m + ":" + timeObj.s;
console.log(timeString);
}
public double ConvertToTimestamp(DateTime value)
{
//create Timespan by subtracting the value provided from
//the Unix Epoch
TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
//return the total milliseconds (which is a UNIX timestamp * 1000)
return (long)span.TotalMilliseconds;
}