JavaScript客户端日期时间和时区 [英] JavaScript client Date time and timezones
问题描述
大家好,
我发现这种出色的JavaScript可以显示当前时间,时区以及夏令时的更改时间.我的问题是,它似乎仅适用于document.write.但是我需要将其显示在asp.net标签中,以便我可以利用C#后面代码中的信息.我尝试在页面上放置标签和/或隐藏字段,并没有运气地使用document.getElementById.任何想法将不胜感激.
预先感谢.这是带有asp隐藏字段的原始代码
Hello all,
I have found this great JavaScript that display the current time, time zones and when the daylight saving will change. My problem is that it seem to work only with document.write. But I need it to be displayed in a asp.net label so that I can take advantage of the information from the code behind in C#. I tried placing a label and or hidden field on the page and using the document.getElementById with no luck. Any Ideas would be grateful.
Thank in advance. Here is the original code with the asp hidden field
<!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>
<title>DST Calculator</title>
<script type="text/javascript">
function DisplayDstSwitchDates()
{
var year = new Date().getYear();
if (year < 1000)
year += 1900;
var firstSwitch = 0;
var secondSwitch = 0;
var lastOffset = 99;
// Loop through every month of the current year
for (i = 0; i < 12; i++)
{
// Fetch the timezone value for the month
var newDate = new Date(Date.UTC(year, i, 0, 0, 0, 0, 0));
var tz = -1 * newDate.getTimezoneOffset() / 60;
// Capture when a timzezone change occurs
if (tz > lastOffset)
firstSwitch = i-1;
else if (tz < lastOffset)
secondSwitch = i-1;
lastOffset = tz;
}
// Go figure out date/time occurences a minute before
// a DST adjustment occurs
var secondDstDate = FindDstSwitchDate(year, secondSwitch);
var firstDstDate = FindDstSwitchDate(year, firstSwitch);
if (firstDstDate == null && secondDstDate == null)
return 'Daylight Savings is not observed in your timezone.';
else
return 'Last minute before DST change occurs in ' +
year + ': ' + firstDstDate + ' and ' + secondDstDate;
}//
function FindDstSwitchDate(year, month)
{
// Set the starting date
var baseDate = new Date(Date.UTC(year, month, 0, 0, 0, 0, 0));
var changeDay = 0;
var changeMinute = -1;
var baseOffset = -1 * baseDate.getTimezoneOffset() / 60;
var dstDate;
// Loop to find the exact day a timezone adjust occurs
for (day = 0; day < 50; day++)
{
var tmpDate = new Date(Date.UTC(year, month, day, 0, 0, 0, 0));
var tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;
// Check if the timezone changed from one day to the next
if (tmpOffset != baseOffset)
{
var minutes = 0;
changeDay = day;
// Back-up one day and grap the offset
tmpDate = new Date(Date.UTC(year, month, day-1, 0, 0, 0, 0));
tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;
// Count the minutes until a timezone chnage occurs
while (changeMinute == -1)
{
tmpDate = new Date(Date.UTC(year, month, day-1, 0, minutes, 0, 0));
tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;
// Determine the exact minute a timezone change
// occurs
if (tmpOffset != baseOffset)
{
// Back-up a minute to get the date/time just
// before a timezone change occurs
tmpOffset = new Date(Date.UTC(year, month,
day-1, 0, minutes-1, 0, 0));
changeMinute = minutes;
break;
}
else
minutes++;
}
// Add a month (for display) since JavaScript counts
// months from 0 to 11
dstDate = tmpOffset.getMonth() + 1;
// Pad the month as needed
if (dstDate < 10) dstDate = "0" + dstDate;
// Add the day and year
dstDate += '/' + tmpOffset.getDate() + '/' + year + ' ';
// Capture the time stamp
tmpDate = new Date(Date.UTC(year, month,
day-1, 0, minutes-1, 0, 0));
dstDate += tmpDate.toTimeString().split(' ')[0];
return dstDate;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="HiddenFieldClientTime" runat="server" />
<script type="text/javascript">
document.write("Current date/time: " + new Date() + "<br />");
document.write(DisplayDstSwitchDates());
</script>
</div>
</form>
</body>
</html>
推荐答案
you should use the following code to access the hidden control
you should use the following code to access the hidden control
var hdnControl=document.getElementById("<%=HiddenFieldClientTime.ClientID%>");
then to set the desired value in the hiddenfield control , follow the code below:
then to set the desired value in the hiddenfield control , follow the code below:
var currentDate="15/10/2010";
hdnControl.value=currentDate;
now,its done.
now,its done.
it also works well with textbox
it also works well with textbox
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="HiddenFieldClientTime" Value="hdn" runat="server" />
</div>
</form>
<script type="text/javascript">
var hdnControl=document.getElementById("HiddenFieldClientTime");
var currentDate="15/10/2010";
hdnControl.value=currentDate;
</script>
</body>
another method is also posted below
another method is also posted below
这篇关于JavaScript客户端日期时间和时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!