User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 374,611 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,459 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 11079 | Replies: 1 | Solved
Reply
Join Date: Aug 2006
Location: India
Posts: 4
Reputation: Gravis is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Gravis Gravis is offline Offline
Newbie Poster

Question DateTime - TimeZone conversion with Javascript. Partially working!

  #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.

 
<script language="JavaScript">
function GetMonth(intMonth){
    var MonthArray = new Array("Jan", "Feb", "Mar",
                               "Apr", "May", "Jun",
                               "Jul", "Aug", "Sep",
                               "Oct", "Nov", "Dec") 
    return MonthArray[intMonth]      
    }
function disp_time(offset){
var time = document.createoutage.isttime.value;
var timesplit = time.split(" ");
var date = timesplit[0];
var month = timesplit[1];
var year = timesplit[2];
var hhmm = timesplit[3];
var hours = timesplit[3].split(":");
var hh = hours[0];
var mm = hours[1];
//var ss = hours[2];
var AMorPM = timesplit[4];  
  if (AMorPM=="PM")
  {
   if (parseInt(hh,10)==12)
    hh=12;
   else 
    hh=parseInt(hh,10)+12;
  } 
  else if (AMorPM=="AM")
  {
   if (hh==12)
    hh-=12;
   hh=parseInt(hh,10);
  }
var TimezoneOffset = offset  // adjust for time zone
   var timetime = month+" "+date+", "+year+" "+hh+":"+mm+":"+"00";
  var localTime = new Date(timetime);

  var ms = localTime.getTime()+ (localTime.getTimezoneOffset() * 60000)+ TimezoneOffset * 3600000;
  
  var time =  new Date(ms)
  var month = time.getMonth();
  month = parseFloat(month)+1;
  var date = time.getDate();
  var hour = time.getHours() ;
  var minute = time.getMinutes();
  var curTime = date+" "+GetMonth(time.getMonth())+" "+ ((hour > 12) ? hour - 12 : hour)
  if(hour==0) curTime = "12"
  curTime += ((minute < 10) ? ":0" : ":") + minute
  curTime += (hour >= 12) ? " PM" : " AM"
  return curTime;
  
}
function convertdate()
{
  var esttime = disp_time('-4');
  var edttime = disp_time('-4');
  var cettime = disp_time('+2');
  document.createoutage.esttime.value=esttime;
 document.createoutage.edttime.value=edttime;
 document.createoutage.cettime.value=cettime;
} 
</script>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2006
Location: India
Posts: 4
Reputation: Gravis is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Gravis Gravis is offline Offline
Newbie Poster

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

  #2  
Aug 28th, 2006
Ok, I resolved this myself. I had to change the code down below to this...
var hour = time.getHours() ;
  var minute = time.getMinutes();
  var curTime = date+" "+GetMonth(time.getMonth())+" ";
  if(hour==0) { hour = "12";}
  curTime += ((hour > 12) ? hour - 12 : hour);
  curTime += ((minute < 10) ? ":0" : ":") + minute;
  curTime += (hour >= 12) ? " PM" : " AM";
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 8:30 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC