kherrerab,
following on from dparsons post above, i used the javascript section in the article and created a few helper methods. here's what i have in my app.
Template.Master (the javascript portion):
<script type="text/javascript">
/* <![CDATA[ */
function AdjustColumnsHeight()
{
// get a reference to the three DIVS that make up the columns
var centerCol = window.document.getElementById('centercol');
var leftCol = window.document.getElementById('leftcol');
var rightCol = window.document.getElementById('rightcol');
// calculate the max height
var hCenterCol = centerCol.offsetHeight;
var hLeftCol = leftCol.offsetHeight;
var hRightCol = rightCol.offsetHeight;
var maxHeight = Math.max(hCenterCol, Math.max(hLeftCol, hRightCol));
// set the height of all 3 DIVS to the max height
centerCol.style.height = maxHeight + 'px';
leftCol.style.height = maxHeight + 'px';
rightCol.style.height = maxHeight + 'px';
// Show the footer
window.document.getElementById('footer').style.vis ibility = 'inherit';
// always get the offset with each pageload
GetLocalTimeOffset();
}
function GetLocalTimeOffset()
{
var rightNow = new Date();
var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
var temp = date1.toGMTString();
var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
var temp = date2.toGMTString();
var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60);
if (hoursDiffDaylightTime == hoursDiffStdTime)
{
//Daylight Saving Time is NOT observed here.
createCookie("daylightSavingTime", "false", 0);
}
else
{
//Daylight Saving Time is observed here.
createCookie("daylightSavingTime", "true", 0);
}
createCookie("ClientDateTime", rightNow.toUTCString(), 0);
}
function createCookie(name,value,days)
{
if (days && days > 0)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else {var expires = "";}
document.cookie = name+"="+value+expires+"; path=/";
}
window.onload = function() { AdjustColumnsHeight(); }
/* ]]> */
</script>
I've then got these two little methods in my Helpers.cs class:
public static DateTime GetUserDateTime(HttpRequest Request, ProfileCommon Profile)
{
if (Request.Cookies["ClientDateTime"] != null && Request.Cookies["daylightSavingTime"] != null)
{
try
{
// try to add to Profile as only logged in user will be able to use this
// if we want anonymous users to be able to use, just remove the two Profile lines
// and use two variables to capture the Cookie values
Profile.ClientDateTime = (string)Request.Cookies["ClientDateTime"].Value.Replace("UTC", "");
Profile.daylightSavingTime = bool.Parse(Request.Cookies["daylightSavingTime"].Value);
DateTime clientTime = DateTime.Parse(Profile.ClientDateTime);
return clientTime.AddHours(Profile.daylightSavingTime ? 1 : 0);
}
catch { return DateTime.Now; }
}
return DateTime.Now;
}
public static DateTime AdjustDateTimeToLocal(DateTime datetimeTobeAdjusted, DateTime userDateTime)
{
//Move to universal time (GMT) with Zero offset
DateTime utcDateTime = DateTime.Now.ToLocalTime();
//Add client side offset
TimeSpan timeDiff = userDateTime.Subtract(utcDateTime);
// clocks should be within 20 mins of each other but just in case!!
// we're accouting here for tehran, bombay adelaide etc that is on a 30 min offset
datetimeTobeAdjusted = datetimeTobeAdjusted.AddMinutes((Math.Abs(timeDiff .Minutes) > 20) ? timeDiff.Minutes : 0);
datetimeTobeAdjusted = datetimeTobeAdjusted.AddHours(timeDiff.Hours);
return datetimeTobeAdjusted;
}
This allows me to either 'adjust' a passed in datetime (i.e. a post time for example) or simply query the user's current datetime (should he/she request info at a particular time of day etc..). for example, here's the portion of code from my ShowThread.aspx page that displays the date:
<asp:Literal ID="lblAddedDate" runat="server" Text='<%# string.Format("{0:g}", Helpers.AdjustDateTimeToLocal(Convert.ToDateTime(E val("AddedDate")), Convert.ToDateTime(Helpers.GetUserDateTime(Request , Profile)))) %>' />
obviously, the functions can be run from 'normal' cs files to derive various offsets as well.
hope this helps
[edit] - oops, just realised that i'd forgotten to add the section that's required for the Profile stuff in the web.config file. anyway, here it is. Under the <profile> section (under <properties>) add the following (doesn't matter where, i did it after ShoppingCart)
<add name="ClientDateTime" type="String"/>
<add name="daylightSavingTime" defaultValue="false" type="bool"/>
[end edit]
jimi
http://www.jamestollan.com