By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,399 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

Time Zone Help

P: 9
I am in need of your assistance. I have a page with multiple unixtime
stamps and I need to display them in readable time and date with the
user's time zone.

Below is the code.
Expand|Select|Wrap|Line Numbers
  1.  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  3. "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
  4.  
  5. <HTML lang=en xml:lang="en"
  6. xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>Support</TITLE>
  7. <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
  8. <META http-equiv=Content-Style-Type content=text/css>
  9. <script language="JavaScript" type="text/javascript">
  10.  
  11.  
  12. var tzo=(new Date().getTimezoneOffset()/60)*(-1); //Time Zone offset
  13.  
  14. /**************************
  15. // This will display all UNIX Time to normal time
  16. **************************/
  17. function convertToDate() {
  18.        var recentTable = document.getElementById("stripedTable");
  19.  var recentTds =  recentTable.getElementsByTagName("td");
  20.        for (i = 0; i<recentTds.length; i++) {
  21.        var tdValues = recentTds[i].lastChild.nodeValue;
  22.    var tdNewValues = document.createTextNode("I'm working");
  23.                if ((!isNaN(tdValues)) && (tdValues != " ")) {
  24.  
  25.        alert(tdValues);
  26.    }
  27.        }
  28. }
  29.  
  30.  
  31.  
  32. function unixtimetodate() {
  33.  
  34.  var allInputs = document.getElementsByTagName("input");
  35.        for (i = 0; i<allInputs.length; i++) {
  36.                        if (allInputs[i].className == "unixtime") {
  37.  
  38.        var myDate = new Date( (dateString) *1000);
  39.                        document.write(myDate.toGMTString()+"<br>"+myDate.toLocaleString());
  40.                        var utc = 0;
  41.  
  42.        // This creates a unixtime in UTC
  43.        if (utc) {
  44.                dateString = myDate.toGMTString();
  45.  
  46.                // This creates a unixtime in localtime (accounting for the local timezone)
  47.        } else {
  48.                dateString = myDate.toLocaleString();
  49.        }
  50.        get_eid(dateString) = dateString;
  51.        return false;
  52. }
  53.  
  54. function datetounixtime() {
  55.        var utc = 0;
  56.        // This creates a unixtime in UTC
  57.        if (utc) {
  58.                var humDate = new Date(
  59.                        Date.UTC(
  60.                                get_eid('vyy').value,
  61.                                get_eid('vmm').value - 1,
  62.                                get_eid('vdd').value,
  63.                                get_eid('vhh').value,
  64.                                get_eid('vmin').value,
  65.                                get_eid('vsec').value
  66.                                )
  67.                        );
  68.                get_eid(dateString) = (humDate.getTime()/1000.0);
  69.  
  70.                // This creates a unixtime in localtime (accounting for the local timezone)
  71.        } else {
  72.                var humDate = new Date(
  73.                        get_eid('vyy').value,
  74.                        get_eid('vmm').value - 1,
  75.                        get_eid('vdd').value,
  76.                        get_eid('vhh').value,
  77.                        get_eid('vmin').value,
  78.                        get_eid('vsec').value
  79.                        );
  80.                get_eid(dateString) = (humDate.getTime()/1000.0);
  81.        }
  82.        return false;
  83. }
  84.  
  85. function get_month($num) {
  86.        if ($num == 1) { return "Jan"; }
  87.        if ($num == 2) { return "Feb"; }
  88.        if ($num == 3) { return "Mar"; }
  89.        if ($num == 4) { return "Apr"; }
  90.        if ($num == 5) { return "May"; }
  91.        if ($num == 6) { return "Jun"; }
  92.        if ($num == 7) { return "Jul"; }
  93.        if ($num == 8) { return "Aug"; }
  94.        if ($num == 9) { return "Sep"; }
  95.        if ($num == 10) { return "Oct"; }
  96.        if ($num == 11) { return "Nov"; }
  97.        if ($num == 12) { return "Dec"; }
  98. }
  99.  
  100. function get_eid(name) {
  101.        var ret = document.getElementById(name);
  102.        return ret;
  103. }
  104.  
  105. function startup() {
  106.        var dt = new Date();
  107.        var month = dt.getMonth() + 1;
  108.        var year = dt.getFullYear();
  109.        var mday = dt.getDate();
  110.  
  111.        var hour = dt.getHours();
  112.        var min = dt.getMinutes();
  113.        var sec = dt.getSeconds();
  114.  
  115.        // This give MILLIseconds
  116.        var epoch = ((dt.getTime() - dt.getMilliseconds()) / 1000);
  117.  
  118.        get_eid('vyy').value = year;
  119.        get_eid('vmm').value = month;
  120.        get_eid('vdd').value = mday;
  121.  
  122.        get_eid('vhh').value = hour;
  123.        get_eid('vmin').value = min;
  124.        get_eid('vsec').value = sec;
  125.  
  126.        get_eid('unixtimestamp').value = epoch;
  127.  
  128.                        }
  129.                }
  130. }
  131.  
  132. function addLoadEvent(func) {
  133.  var oldonload = window.onload;
  134.  if (typeof window.onload != 'function') {
  135.    window.onload = func;
  136.  } else {
  137.    window.onload = function() {
  138.      oldonload();
  139.      func();
  140.    }
  141.  }
  142. }
  143.  
  144. addLoadEvent(convertToDate);
  145. </script>
  146.  
  147.  
  148. </HEAD>
  149.  
  150.  
  151.    <table width="100%" id='stripedTable' summary="case notes and
  152. history table">
  153.                <tr>
  154.                    <td style="vertical-align: middle">
  155.                        <b><a href="case.do?caseId=C461057">C461057</a></b>
  156.                    </td>
  157.                    <td>
  158.                        testcase14<br/>
  159.                        <b>Summary:</b>7g7g7g7gg7
  160.                    </td>
  161.                    <td align="right" class="unixTime"><b>Last
  162. Updated:</b><br />1229457069</td>
  163.                </tr>
  164.                <tr>
  165.                    <td style="vertical-align: middle">
  166.                        <b><a href="case.do?caseId=C461056">C461056</a></b>
  167.                    </td>
  168.                    <td>
  169.                        test31<br/>
  170.                        <b>Summary:</b>8g8g8g8g8g8g8g
  171.                    </td>
  172.                    <td align="right"><b>Last Updated:</b><br />1229456237</td>
  173.                </tr>
  174.                <tr>
  175.                    <td style="vertical-align: middle">
  176.                        <b><a href="case.do?caseId=C461054">C461054</a></b>
  177.                    </td>
  178.                    <td>
  179.                        the<br/>
  180.                        <b>Summary:</b>0h0hhh0h0h
  181.                    </td>
  182.                    <td align="right"><b>Last Updated:</b><br />1229464275</td>
  183.                </tr>
  184.                <tr>
  185.                    <td style="vertical-align: middle">
  186.                        <b><a href="rmaCase.do?caseId=C461051">C461051</a></b>
  187.                    </td>
  188.                    <td>
  189.                        rma issue<br/>
  190.                        <b>Summary:</b>7f7g7h7f7f7g7h
  191.                    </td>
  192.                    <td align="right"><b>Last Updated:</b><br />1229711822</td>
  193.                </tr>
  194.                <tr>
  195.                    <td style="vertical-align: middle">
  196.                        <b><a href="case.do?caseId=C461026">C461026</a></b>
  197.                    </td>
  198.                    <td>
  199.                        switch problem<br/>
  200.                        <b>Summary:</b>9g9h89g76f7
  201.                    </td>
  202.                    <td align="right"><b>Last Updated:</b><br />1228512455</td>
  203.                </tr>
  204.    </table>
  205. </div>
  206. </BODY></HTML>
  207.  
  208.  
  209.  
In a perfect word, this page would look like this when a visitor
visits the page.

Expand|Select|Wrap|Line Numbers
  1. <HTML lang=en xml:lang="en"
  2. xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>Support</TITLE>
  3. <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
  4. <META http-equiv=Content-Style-Type content=text/css>
  5. <script language="JavaScript" type="text/javascript">
  6.  
  7.  
  8. var tzo=(new Date().getTimezoneOffset()/60)*(-1); //Time Zone offset
  9.  
  10. /**************************
  11. // This will display all UNIX Time to normal time
  12. **************************/
  13. function convertToDate() {
  14.        var recentTable = document.getElementById("stripedTable");
  15.  var recentTds =  recentTable.getElementsByTagName("td");
  16.        for (i = 0; i<recentTds.length; i++) {
  17.        var tdValues = recentTds[i].lastChild.nodeValue;
  18.    var tdNewValues = document.createTextNode("I'm working");
  19.                if ((!isNaN(tdValues)) && (tdValues != " ")) {
  20.  
  21.        alert(tdValues);
  22.    }
  23.        }
  24. }
  25.  
  26.  
  27.  
  28. function unixtimetodate() {
  29.  
  30.  var allInputs = document.getElementsByTagName("input");
  31.        for (i = 0; i<allInputs.length; i++) {
  32.                        if (allInputs[i].className == "unixtime") {
  33.  
  34.        var myDate = new Date( (dateString) *1000);
  35.                        document.write(myDate.toGMTString()+"<br>"+myDate.toLocaleString());
  36.                        var utc = 0;
  37.  
  38.        // This creates a unixtime in UTC
  39.        if (utc) {
  40.                dateString = myDate.toGMTString();
  41.  
  42.                // This creates a unixtime in localtime (accounting for the local timezone)
  43.        } else {
  44.                dateString = myDate.toLocaleString();
  45.        }
  46.        get_eid(dateString) = dateString;
  47.        return false;
  48. }
  49.  
  50. function datetounixtime() {
  51.        var utc = 0;
  52.        // This creates a unixtime in UTC
  53.        if (utc) {
  54.                var humDate = new Date(
  55.                        Date.UTC(
  56.                                get_eid('vyy').value,
  57.                                get_eid('vmm').value - 1,
  58.                                get_eid('vdd').value,
  59.                                get_eid('vhh').value,
  60.                                get_eid('vmin').value,
  61.                                get_eid('vsec').value
  62.                                )
  63.                        );
  64.                get_eid(dateString) = (humDate.getTime()/1000.0);
  65.  
  66.                // This creates a unixtime in localtime (accounting for the local timezone)
  67.        } else {
  68.                var humDate = new Date(
  69.                        get_eid('vyy').value,
  70.                        get_eid('vmm').value - 1,
  71.                        get_eid('vdd').value,
  72.                        get_eid('vhh').value,
  73.                        get_eid('vmin').value,
  74.                        get_eid('vsec').value
  75.                        );
  76.                get_eid(dateString) = (humDate.getTime()/1000.0);
  77.        }
  78.        return false;
  79. }
  80.  
  81. function get_month($num) {
  82.        if ($num == 1) { return "Jan"; }
  83.        if ($num == 2) { return "Feb"; }
  84.        if ($num == 3) { return "Mar"; }
  85.        if ($num == 4) { return "Apr"; }
  86.        if ($num == 5) { return "May"; }
  87.        if ($num == 6) { return "Jun"; }
  88.        if ($num == 7) { return "Jul"; }
  89.        if ($num == 8) { return "Aug"; }
  90.        if ($num == 9) { return "Sep"; }
  91.        if ($num == 10) { return "Oct"; }
  92.        if ($num == 11) { return "Nov"; }
  93.        if ($num == 12) { return "Dec"; }
  94. }
  95.  
  96. function get_eid(name) {
  97.        var ret = document.getElementById(name);
  98.        return ret;
  99. }
  100.  
  101. function startup() {
  102.        var dt = new Date();
  103.        var month = dt.getMonth() + 1;
  104.        var year = dt.getFullYear();
  105.        var mday = dt.getDate();
  106.  
  107.        var hour = dt.getHours();
  108.        var min = dt.getMinutes();
  109.        var sec = dt.getSeconds();
  110.  
  111.        // This give MILLIseconds
  112.        var epoch = ((dt.getTime() - dt.getMilliseconds()) / 1000);
  113.  
  114.        get_eid('vyy').value = year;
  115.        get_eid('vmm').value = month;
  116.        get_eid('vdd').value = mday;
  117.  
  118.        get_eid('vhh').value = hour;
  119.        get_eid('vmin').value = min;
  120.        get_eid('vsec').value = sec;
  121.  
  122.        get_eid('unixtimestamp').value = epoch;
  123.  
  124.                        }
  125.                }
  126. }
  127.  
  128. function addLoadEvent(func) {
  129.  var oldonload = window.onload;
  130.  if (typeof window.onload != 'function') {
  131.    window.onload = func;
  132.  } else {
  133.    window.onload = function() {
  134.      oldonload();
  135.      func();
  136.    }
  137.  }
  138. }
  139.  
  140. addLoadEvent(convertToDate);
  141. </script>
  142.  
  143.  
  144. </HEAD>
  145.  
  146.  
  147.    <table width="100%" id='stripedTable' summary="case notes and
  148. history table">
  149.                <tr>
  150.                    <td style="vertical-align: middle">
  151.                        <b><a href="case.do?caseId=C461057">C461057</a></b>
  152.                    </td>
  153.                    <td>
  154.                        testcase14<br/>
  155.                        <b>Summary:</b>7g7g7g7gg7
  156.                    </td>
  157.                    <td align="right" class="unixTime"><b>Last
  158. Updated:</b><br />Tue, 16 Dec 2008 19:51:09 GMT</td>
  159.                </tr>
  160.                <tr>
  161.                    <td style="vertical-align: middle">
  162.                        <b><a href="case.do?caseId=C461056">C461056</a></b>
  163.                    </td>
  164.                    <td>
  165.                        test31<br/>
  166.                        <b>Summary:</b>8g8g8g8g8g8g8g
  167.                    </td>
  168.                    <td align="right"><b>Last Updated:</b><br />Tue,
  169. 16 Dec 2008 19:37:17 GMT</td>
  170.                </tr>
  171.                <tr>
  172.                    <td style="vertical-align: middle">
  173.                        <b><a href="case.do?caseId=C461054">C461054</a></b>
  174.                    </td>
  175.                    <td>
  176.                        the<br/>
  177.                        <b>Summary:</b>0h0hhh0h0h
  178.                    </td>
  179.                    <td align="right"><b>Last Updated:</b><br />Tue,
  180. 16 Dec 2008 21:51:15 GMT</td>
  181.                </tr>
  182.                <tr>
  183.                    <td style="vertical-align: middle">
  184.                        <b><a href="rmaCase.do?caseId=C461051">C461051</a></b>
  185.                    </td>
  186.                    <td>
  187.                        rma issue<br/>
  188.                        <b>Summary:</b>7f7g7h7f7f7g7h
  189.                    </td>
  190.                    <td align="right"><b>Last Updated:</b><br />Fri,
  191. 19 Dec 2008 18:37:02 GMT</td>
  192.                </tr>
  193.                <tr>
  194.                    <td style="vertical-align: middle">
  195.                        <b><a href="case.do?caseId=C461026">C461026</a></b>
  196.                    </td>
  197.                    <td>
  198.                        switch problem<br/>
  199.                        <b>Summary:</b>9g9h89g76f7
  200.                    </td>
  201.                    <td align="right"><b>Last Updated:</b><br />Fri,
  202. 05 Dec 2008 21:27:35 GMT</td>
  203.                </tr>
  204.    </table>
  205. </div>
  206. </BODY></HTML>
  207.  
But, if the user was Pacific Standard time, the time would be 8 hours
earlier and GMT would be replaced with PST

Please help,
Dec 22 '08 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
what is the problem actually, I didn't get that.

regards
Dec 22 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.