且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用JavaScript获取客户端时间时出现问题.

更新时间:2022-06-18 04:57:07

Ok I got your problem,

Make sure that you Have lable control on the page with ID lblDateTime ( I think this will be. )

But The Actual Problem Here is First Your Java script event fires and you are getting Right values ( you can varify it by putting debugger; to the first line of your JS Function ) to set to your label, but then page Fires server Side event and Page is Getting rendered again and Displays by default Value Applied to Lable.

[Addition to Solution]

Ok As per you Told me that you only want to fire client Side Javascript.

Ok I got your problem,

Make sure that you Have lable control on the page with ID lblDateTime ( I think this will be. )

But The Actual Problem Here is First Your Java script event fires and you are getting Right values ( you can varify it by putting debugger; to the first line of your JS Function ) to set to your label, but then page Fires server Side event and Page is Getting rendered again and Displays by default Value Applied to Lable.

[Addition to Solution]

Ok As per you Told me that you only want to fire client Side Javascript.

Swapnil 2009 wrote:
Swapnil 2009 wrote:

OnClientClick="DisplayDateTime()"

OnClientClick="DisplayDateTime()"



Instead Write



Instead Write

OnClientClick = "return DisplayDateTime()"





Swapnil 2009 wrote:
Swapnil 2009 wrote:

function DisplayDateTime()
{
var DateTime = new Date();
var strYear= DateTime.getYear();
var strMonth= DateTime.getMonth() +1;
var strDay = DateTime.getDate();
var strHours = DateTime.getHours();
var strMinutes = DateTime.getMinutes();
var strSeconds = DateTime.getSeconds();
var dateTime=document.getElementById("lblDateTime");
dateTime.innerText = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;
}

function DisplayDateTime()
{
var DateTime = new Date();
var strYear= DateTime.getYear();
var strMonth= DateTime.getMonth() +1;
var strDay = DateTime.getDate();
var strHours = DateTime.getHours();
var strMinutes = DateTime.getMinutes();
var strSeconds = DateTime.getSeconds();
var dateTime=document.getElementById("lblDateTime");
dateTime.innerText = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;
}




Instead Write ( Just Appended return false to the end of function )




Instead Write ( Just Appended return false to the end of function )

function DisplayDateTime() {
            var DateTime = new Date();
            var strYear = DateTime.getYear();
            var strMonth = DateTime.getMonth() + 1;
            var strDay = DateTime.getDate();
            var strHours = DateTime.getHours();
            var strMinutes = DateTime.getMinutes();
            var strSeconds = DateTime.getSeconds();
            var dateTime = document.getElementById("lblDateTime");
            dateTime.innerText = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;
            return false;
       }





Vote and Mark as a Answer if it helped.





Vote and Mark as a Answer if it helped.


Can you post your ALL code of page?
Can you post your ALL code of page?