YAFLogo

Trickstar
  • Trickstar
  • 50.2% (Neutral)
  • YAF Lover Topic Starter
9 years ago
Well I have written a JavaScript code that dynamically updates a clock to the millisecond that the page was loaded and I have integrated it into other ASP.Net applications before. Now since YAF already has its own clock that corrects the time zones, I tried to write a JavaScript code (below) to access it since I couldn't find the source file containing the "Current Time", the closest I came was in forum.ascx but it didn't quite have what I was looking for.

This function I called on the bottom of the page with "forum_ctl03_Welcome_TimeNow" as the ID and the millisecond ASP method for milliseconds:
function setClockYAF(elementId, milliseconds) {
    var element = documnet.getElementById(elementId);
	var content = element.innerHTML;
	var array[];
	var amPm;
	var endValue;
	if (content.search("AM") != -1) {
	    endValue = content.search("AM") + 1;
		amPm = "AM";
	}
	if (content.search("PM") != -1) {
		    endValue = content.search("PM") + 1;
			amPm = "PM";
	}
	var startValue = content.search(": ") + 2;
	var time = content.substring(startValue, endValue);
    	array = time.split(":");
	array[2] = array[2].replace(" " + amPm, "");
	content.replace(time, "<span id=\"setClockYAF\"></span>");
	var clockYAF = new Clock("setClockYAF", array[0], array[1], array[2], milliseconds);
	clockYAF.update = true;
        clockYAF.display();
}

The Clock object is my own dynamic clock. Anyways, the above method didn't work and is more work than I should have to put into it. So my question is, what file contains the time displayed where the forums says "Current Time: hh:mm:ss"?
UserPostedImage 
Sponsor

tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 3.0.3
9 years ago
It is generated in the ../controls/ForumWelcome.ascx

<div id="divTimeNow">
        <asp:Label ID="TimeNow" runat="server" />
    </div>
Trickstar
  • Trickstar
  • 50.2% (Neutral)
  • YAF Lover Topic Starter
9 years ago
Originally Posted by: tha_watcha 

It is generated in the ../controls/ForumWelcome.ascx

<div id="divTimeNow">
        <asp:Label ID="TimeNow" runat="server" />
    </div>[/code][/quote]

OK I had everything running perfectly, except I noticed the time was off compared to what it normally would be. (The time zones werent being used, it was using just UTC). So now I'm trying to figure out how you guys have everything where it all uses the set time zone. What change I made to ForumWelcome.ascx is this:
[code=xml]<%@ Control Language="C#" AutoEventWireup="true" EnableViewState="false"
    Inherits="YAF.Controls.ForumWelcome" Codebehind="ForumWelcome.ascx.cs" %>
<div class="yafForumWelcome">
    <div id="divTimeNow">
       <!-- <asp:Label ID="TimeNow" runat="server" /> --><span id="forum_ctl03_Welcome_TimeNow">Current time: <span id="clock">Error getting time</span>. </span>
       </div>
    <div id="divTimeLastVisit">
        <asp:Label ID="TimeLastVisit" runat="server" /></div>
    <div id="divUnreadMsgs">
        <asp:HyperLink runat="server" ID="UnreadMsgs" Visible="false" /></div>
</div>

And the ASP method I'm using to set the time parameters to my clock are:
<% Response.Write(YAF.Controls.ForumWelcome.Get<IDateTime>().DateTime.UtcNow.Hour.ToString()); %>

I have this code on my design template thats embedded into the default.aspx in order to get my own design so technically its located in the default.aspx, as thats how the server reads it. This is the compilation error I get:

Quote:

CS0117: 'YAF.Controls.ForumWelcome' does not contain a definition for 'Get'



How do I get UtcNow to give the hour according to what I have set as the Time Zone in admin controls? I really need this. Thanks in advance.
UserPostedImage 
tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 3.0.3
9 years ago
Simply use

<% Response.Write(DateTime.UtcNow.Hour.ToString()); %>
Trickstar
  • Trickstar
  • 50.2% (Neutral)
  • YAF Lover Topic Starter
9 years ago
Thats what I'm currently using but it just gives me the time from the server instead of the time set in Admin Control Panel of the forum. Is there a way to check which timezone was selected? Right now I'm using the following to return the time I want, but if a user is from another timezone and changes their settings, it will still display the time from the timezone I programmed. I want the hour to change with the user timezone settings for each specific user and if I change the timezone in Admin CP.

Code (in a client-side script tag):
                            <%  var hour = DateTime.UtcNow.Hour;
                                if (hour < 5) {
                                    hour += 19;
                                }
                                else {
                                    hour -= 5;
                                }
                            %>
                            if (document.getElementById("clock")!= undefined) { 
                                var clock = new Clock("clock", <% Response.Write(hour.ToString()); %>, <% Response.Write(DateTime.UtcNow.Minute.ToString()); %>, <% Response.Write(DateTime.UtcNow.Second.ToString()); %>, <% Response.Write(DateTime.UtcNow.Millisecond.ToString()); %>);
                                clock.update = true;
                                clock.display();

UserPostedImage 
Trickstar
  • Trickstar
  • 50.2% (Neutral)
  • YAF Lover Topic Starter
9 years ago
Sorry for the bump but I need to know if theres a variable that contains what Time Zone the registered user is in? Just as theres one that tells you what theme is selected (below).

<% if (YafContext.Current.Get<ITheme>().ThemeFile == "default.xml") {
%>

EDIT:
The following works pretty well, but now I just need to be able to check for daylight savings time and its pretty hard since AccountForDST() is set to private so I'm trying to figure out a way to take the code to that function and use it for what I'm looking for. This is what I have so far:

<% var hour = DateTime.UtcNow.Hour + (YafContext.Current.TimeZoneUser / 60);
            if (YafContext.Current.DSTUser)
            {
                if (TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time").IsDaylightSavingTime(DateTime.UtcNow + YAF.Core.Services.YafDateTime.TimeOffset))
                {
                    hours += 1;
                }
            } %>

I get a compilation error because I can't use TimeOffset in this context and by looking at YafDateTime I can't really figure out how its used because its always used as "this.TimeOffset"
UserPostedImage 
tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 3.0.3
9 years ago
Quote:

get a compilation error because I can't use TimeOffset in this context and by looking at YafDateTime I can't really figure out how its used because its always used as "this.TimeOffset"



use the interface IDateTime instead of YafDateTime
Replace it with...

<% var hour = DateTime.UtcNow.Hour + (YafContext.Current.TimeZoneUser / 60);
if (YafContext.Current.DSTUser)
{
if (TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time").IsDaylightSavingTime(DateTime.UtcNow + YAF.Core.Services.YafDateTime.TimeOffset))
{
hours += 1;
}
}var hour = DateTime.UtcNow.Hour + (YafContext.Current.TimeZoneUser / 60);
if (YafContext.Current.DSTUser)
{
if (TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time").IsDaylightSavingTime(DateTime.UtcNow + YafContext.Current.Get().TimeOffset))
{
hours += 1;
}
} %>

By the Way can i recommend a better solution to create a Ticking Clock. I use this little jquery plugin

http://www.tcpweb.com.br/JS-Clock/ 

example

<span id="clock">Error getting time</span>

js sample code...

$(document).ready(function(){
			$('#clock').jsclock('<%# YafContext.Current.Get<IDateTime>().FormatTime(DateTime.UtcNow) %>')
		});

and of course you need to load the jquery plugin js file.


Trickstar
  • Trickstar
  • 50.2% (Neutral)
  • YAF Lover Topic Starter
9 years ago
Originally Posted by: tha_watcha 



By the Way can i recommend a better solution to create a Ticking Clock. I use this little jquery plugin

http://www.tcpweb.com.br/JS-Clock/ 

example

<span id="clock">Error getting time</span>

js sample code...

$(document).ready(function(){
			$('#clock').jsclock('<%# YafContext.Current.Get<IDateTime>().FormatTime(DateTime.UtcNow) %>')
		});

and of course you need to load the jquery plugin js file.




Thank you, it works perfectly now. And I may use your suggestion some day, I just never had experience with JQuery so I developed my own method so I would have complete control over how my output would look. Also, I have an unfinished part to my Clock object that will count down, because I am professional web designer and have had the foresight to see I may have a project (such as a store) that would need it. I have been doing this for awhile but I see how backward I am that I never took interest in HTML5 or JQuery. I'll start to implement it someday, as I'm pursuing a degree in Computer Science and need to be as technically up to date as possible. Anyways, thanks for your help.
UserPostedImage 
YAF Logo Copyright © YetAnotherForum.NET & Ingo Herbote. All rights reserved
About Us

The YAF.NET is an open source .NET forum project. YAF.NET is supported by an team of international developers who are build community by building community software.

Powered by Resharper Donate with PayPal button