943,929 Members | Top Members by Rank

Ad:
Aug 28th, 2006
0

DateTime - TimeZone conversion with Javascript. Partially working!

Expand Post »
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>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Gravis is offline Offline
4 posts
since Aug 2006
Aug 28th, 2006
0

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

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";
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Gravis is offline Offline
4 posts
since Aug 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Alternating backgrounds?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: close event for firefox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC