Solved

Display different time zones on web page

Posted on 2009-04-15
7
810 Views
Last Modified: 2012-05-06
Would someone please advise how to display a time for different timezones using <%Response.Write(System.DateTime.Now)%>.  It seems like it would be something really easy, yet I can find nothing online for this specific request.
<%Response.Write(System.DateTime.Now)%>

Open in new window

0
Comment
Question by:Blessed7777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24150879
This code sample works only for .net version 3.5
DateTime dt = DateTime.Now.ToUniversalTime();
        StringBuilder builder = new StringBuilder();
 
        foreach (TimeZoneInfo tzi in TimeZoneInfo.GetSystemTimeZones())
        {
            builder.Append(tzi.StandardName + " : " + dt.Add(tzi.BaseUtcOffset).ToString());
            builder.Append("<br/>");
        }
 
        Label1.Text = builder.ToString();

Open in new window

0
 

Author Comment

by:Blessed7777
ID: 24151353
This would work if I needed to pick up one user's time zonezone.  But I actually need to display 4 different time zones on the page.  We have employees in 4 different time zones sharing the app and working together in it.  One for Central, one for mountain, one for pacific and one for eastern.  Is there an insert for after .now that will give these values?  Ie:  something like <%Response.Write(System.DateTime.Now.EDT)%> to represent eastern time.

Thank youfor your assistance with this
0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24162156
There is no function which supports the format you are looking for. We have to calculate the time by adjusting UTC offset from UTC time.

Do you want to display the time as clock like

Central         Mountain          Pacific         Eastern
 xx:xx             xx:xx             xx:xx            xx:xx

and keeps on refreshing every minute?
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:Blessed7777
ID: 24206041
Hi GiftsonDJohn, yes that is exactly what i am looking to do.  Any suggestions you have would be great.
0
 
LVL 14

Accepted Solution

by:
GiftsonDJohn earned 500 total points
ID: 24206333
Hi,

How I used in my company's intranet home page is like below.

I used to have a xml file and xsl file. Xml file holds all the location and timezone along with daylight saving information.

Just have put a Label Label1 in the place you wish to display the time and include the following code to display the time.
//JavaSCript
<script type=""text/javascript"">
    <%= GetTimeZoneData()%>
    var lastLocalDateTime = new Date();
    var nregion;
 
    function updateTime(boolForce) {
        var localDateTime = new Date();
 
        if (boolForce || (localDateTime.getMinutes() != lastLocalDateTime.getMinutes())) {
 
            for (var i = 0; i < clockData.length; i++) {
 
                nregion = clockData[i][0];
                nOffset = clockData[i][1];
 
                strBaseDateTime = localDateTime.toUTCString();
                newDateTime = new Date(Date.parse(strBaseDateTime.substring(0, strBaseDateTime.length - 4)));
                newDateTime.setMinutes(newDateTime.getMinutes() + (nOffset * 60));
 
                dateStartDST = new Date(Date.parse(clockData[i][2]));
                dateEndDST = new Date(Date.parse(clockData[i][3]));
 
                if (newDateTime >= dateStartDST && newDateTime < dateEndDST) {
                    newDateTime.setMinutes(newDateTime.getMinutes() + 60);
                }
 
                strHours = String(newDateTime.getHours());
                if (strHours.length == 1)
                    strHours = ""0"" + strHours;
 
                strMinutes = String(newDateTime.getMinutes());
                if (strMinutes.length == 1)
                    strMinutes = ""0"" + strMinutes;
 
                document.getElementById(""divTime_"" + nregion).innerHTML = strHours + "":"" + strMinutes;
            }
 
            lastLocalDateTime = localDateTime;
        }
    }
    updateTime(true);
    setInterval(""updateTime(false)"", 1000);
</script>
 
//C#
//In Page_Load
 
protected void Page_Load(object sender, EventArgs e)
{
   StreamReader sreader = new StreamReader(new FileStream("clocks.xml", FileMode.Open));
        string xmldata = sreader.ReadToEnd();
        sreader.Close();
        TextReader tr1 = new StringReader(xmldata);
        XmlTextReader tr11 = new XmlTextReader(tr1);
        XPathDocument xml = new XPathDocument(tr11);
 
        sreader = new StreamReader(new FileStream("clocks.xsl", FileMode.Open));
        TextReader tr2 = new StringReader(sreader.ReadToEnd());
        sreader.Close();
        XmlTextReader tr22 = new XmlTextReader(tr2);
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load(tr22);
 
        StringBuilder sb = new StringBuilder();
        TextWriter tw = new StringWriter(sb);
 
        xslt.Transform(xml, null, tw);
 
        Label1.Text="<div id=\"divClocks\">" + sb.ToString() + "</div>";
}
 
protected string GetTimeZoneData()
    {
        StreamReader sreader = new StreamReader(new FileStream("clocks.xml", FileMode.Open));
        string xmldata = sreader.ReadToEnd();
        sreader.Close();  
       
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(xmldata);
 
        StringBuilder builder = new StringBuilder();
        builder.AppendLine("var clockData = new Array();");
        int i = 0;
        foreach (XmlNode node in xmldoc.FirstChild.ChildNodes)
        {
            string dststart = node.Attributes["dst-start"].Value;
            string dstend = node.Attributes["dst-end"].Value;
            string offset = node.Attributes["offset"].Value;
            string city = node.FirstChild.InnerText;
            builder.AppendFormat("clockData[{0}]=new Array(\"{1}\",\"{2}\",\"{3}\",\"{4}\");{5}", i, city, offset, dststart, dstend, Environment.NewLine);
            i++;
        }
        return builder.ToString();
    }
 
//XML
 
<clocks>
	<clock dst-start="Sunday March 9, 2008 02:00:00" dst-end="Sunday November 2, 2008 02:00:00" offset="-8">
		<city>LA</city>		
	</clock>
	<clock dst-start="Sunday March 9, 2008 02:00:00" dst-end="Sunday November 2, 2008 02:00:00" offset="-5">
		<city>NYC</city>		
	</clock>
	<clock dst-start="Sunday March 30, 2008 01:00:00" dst-end="Sunday October 26, 2008 02:00:00" offset="0">
		<city>UK</city>		
	</clock>
	<clock dst-start="Sunday March 30, 2008 02:00:00" dst-end="Sunday October 26, 2008 03:00:00" offset="1">
		<city>France</city>		
	</clock>
	<clock dst-start="Friday March 28, 2008 02:00:00" dst-end="Sunday October 5, 2008 01:00:00" offset="2">
		<city>Israel</city>		
	</clock>
	<clock dst-start="Sunday March 30, 2008 02:00:00" dst-end="Sunday October 26, 2008 03:00:00" offset="3">
		<city>Russia</city>
	</clock>
	<clock dst-start="" dst-end="" offset="5.5">
		<city>India</city>
	</clock>
	<clock dst-start="" dst-end="" offset="8">
		<city>China</city>
	</clock>
	<clock dst-start="" dst-end="" offset="9">
		<city>Korea</city>
	</clock>
	<clock dst-start="Sunday October 29, 2007 02:00:00" dst-end="Sunday April 6, 2008 03:00:00" offset="11">
		<city>Australia</city>
	</clock>
</clocks>
 
 
//XSL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/*">
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="ClockContainer">
			<tr>
				<xsl:apply-templates select="/*/clock"/>
			</tr>
		</table>
	</xsl:template>
 
	<xsl:template match="clock">
		<td align="center">
			<div class="ClockCity">
				<xsl:value-of select="city"/>
			</div>
			<div class="ClockTime">
				<xsl:attribute name="id">divTime_<xsl:value-of select="./city"/></xsl:attribute>
			</div>
		</td>
	</xsl:template>
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:Blessed7777
ID: 24215696
Oh!  Very cool :)  I am going to followed this example and it worked like a charm.  Thanks for your help with this.  I'm increasing the points to 500 as you went the extra mile.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Any Way to rotate banner ads from Commission Junction? 2 57
Securing WEBAPI on Azure 2 54
Record locking on classic ASP 3 59
How does this modal work? 3 30
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to count occurrences of each item in an array.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question