DateTime - TimeZone conversion with Javascript. Partially working!

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Aug 2006
Posts: 4
Reputation: Gravis is an unknown quantity at this point 
Solved Threads: 0
Gravis Gravis is offline Offline
Newbie Poster

DateTime - TimeZone conversion with Javascript. Partially working!

 
0
  #1
Aug 28th, 2006
Greetings,
I need help with the javascript below. So far, I am able to pass the date through a pop-up calender and this script receives the date in format of "28 Aug 2006 11:47 PM" as a string. I then split the string and take each field for further processing. The problem is that, when the time output is 12:xx AM (only), the date, month and year part are ommited. Example :- if the input is "28 Aug 2006 03:30 AM" then the output becomes "12:00 AM" otherwise when the output is not 12.xx it works just fine. Note that date, month and year are not there in the output when it is 12:00AM

The code for your review is as below. I am using "convertdate" to change display the time in different time zones in HTML.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. <script language="JavaScript">
  3. function GetMonth(intMonth){
  4. var MonthArray = new Array("Jan", "Feb", "Mar",
  5. "Apr", "May", "Jun",
  6. "Jul", "Aug", "Sep",
  7. "Oct", "Nov", "Dec")
  8. return MonthArray[intMonth]
  9. }
  10. function disp_time(offset){
  11. var time = document.createoutage.isttime.value;
  12. var timesplit = time.split(" ");
  13. var date = timesplit[0];
  14. var month = timesplit[1];
  15. var year = timesplit[2];
  16. var hhmm = timesplit[3];
  17. var hours = timesplit[3].split(":");
  18. var hh = hours[0];
  19. var mm = hours[1];
  20. //var ss = hours[2];
  21. var AMorPM = timesplit[4];
  22. if (AMorPM=="PM")
  23. {
  24. if (parseInt(hh,10)==12)
  25. hh=12;
  26. else
  27. hh=parseInt(hh,10)+12;
  28. }
  29. else if (AMorPM=="AM")
  30. {
  31. if (hh==12)
  32. hh-=12;
  33. hh=parseInt(hh,10);
  34. }
  35. var TimezoneOffset = offset // adjust for time zone
  36. var timetime = month+" "+date+", "+year+" "+hh+":"+mm+":"+"00";
  37. var localTime = new Date(timetime);
  38.  
  39. var ms = localTime.getTime()+ (localTime.getTimezoneOffset() * 60000)+ TimezoneOffset * 3600000;
  40.  
  41. var time = new Date(ms)
  42. var month = time.getMonth();
  43. month = parseFloat(month)+1;
  44. var date = time.getDate();
  45. var hour = time.getHours() ;
  46. var minute = time.getMinutes();
  47. var curTime = date+" "+GetMonth(time.getMonth())+" "+ ((hour > 12) ? hour - 12 : hour)
  48. if(hour==0) curTime = "12"
  49. curTime += ((minute < 10) ? ":0" : ":") + minute
  50. curTime += (hour >= 12) ? " PM" : " AM"
  51. return curTime;
  52.  
  53. }
  54. function convertdate()
  55. {
  56. var esttime = disp_time('-4');
  57. var edttime = disp_time('-4');
  58. var cettime = disp_time('+2');
  59. document.createoutage.esttime.value=esttime;
  60. document.createoutage.edttime.value=edttime;
  61. document.createoutage.cettime.value=cettime;
  62. }
  63. </script>
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 4
Reputation: Gravis is an unknown quantity at this point 
Solved Threads: 0
Gravis Gravis is offline Offline
Newbie Poster

Re: DateTime - TimeZone conversion with Javascript. Partially working!

 
0
  #2
Aug 28th, 2006
Ok, I resolved this myself. I had to change the code down below to this...
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var hour = time.getHours() ;
  2. var minute = time.getMinutes();
  3. var curTime = date+" "+GetMonth(time.getMonth())+" ";
  4. if(hour==0) { hour = "12";}
  5. curTime += ((hour > 12) ? hour - 12 : hour);
  6. curTime += ((minute < 10) ? ":0" : ":") + minute;
  7. curTime += (hour >= 12) ? " PM" : " AM";
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC