Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    HELP! Converting dates from UTC Format to GMT

    I’m just a beginner in Javascript.

    I have created an asp page (Week.asp), where there is a form (frmDayAdd). On this form the user can enter a date by clicking on a button and a new window pops up with a calendar. When they click on required date, the calendar window is closed and the date passed to the form field (txtDate) in format dd/mm/yyyy. When the user then clicks the drop down select box (cboTarget) it triggers the javascript function (getDays). This then finds the corresponding day (e.g. Monday) to the date selected (txtDate) and writes it to the table cell (id='spnDay').

    This all appears to work ok (i.e. a day is shown when above process is carried out) however, the days passed are not the correct days for the date. I think the code is getting confused somewhere with GMT & UTC time formats.

    I want it all to work with GMT.

    Ive tried various things but with no success, as I said before Ive only just started working with JavaScript.

    I would be very grateful if anyone could tell me how to adapt my code

    I hope this all makes sense.

    My Code is below.



    HTML Code:
    Code:
    ………………………………………………………………………………………………… 
    function getDays(aText) 
    {   
       myDays=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]; 
       myDate=new Date(eval('"'+aText.value+'"')); 
       spnDay.innerHTML=myDays[myDate.getDay()] 
    } 
    
    
    
    <table align="center" width="60%"  border="0" cellspacing="1" cellpadding="5"> 
    <form action="Week.asp?WeekID=<%=strWeekID%>&Day=Add&Mode=Edit" name="frmDayAdd" Method="Post"> 
    <tr> 
    <td> </td> 
               <td><input type="text"  name="txtDate_vis" Value="<%=strDate%>"  size="15" style="font-family: Verdana; font-size: 10pt"> 
                        <input type="button" value="..." onClick="OpenDateWin('Y','txtDate','frmDayAdd')"> 
                       <input type="hidden"  name="txtDate" Value="<%=strDate%>"></td> 
    <td class="content" id='spnDay'></td> 
               <td><select name="cboTarget" onFocus="getDays(frmDayAdd.txtDate_vis)" style="font-family: Verdana; font-size: 10pt"> 
                      <option value="">Select</option></td> 
    </tr> 
    </table> 
    …………………………………………………………………………………………………

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    It's getting confused with the date format. Since you don't know if the client date format uses dd/mm/yyyy or not, you need to parse the date manually to get consistent result.
    Code:
    function getDays(aText) 
    {   
       var myDays=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];    
       var d = aText.value.split("/");
       if (d.length == 3 && aText.value.length==10){ //validate format
          var myDate = new Date(d[2], d[1]-1, d[0]); //new Date(yyyy, mm, dd)
          if (!isNaN(myDate) && myDate.getDate()==d[0] && myDate.getMonth()==d[1]-1 && myDate.getFullYear()==d[2]){ //check validity of date
            document.getElementById("spnDay").innerHTML=myDays[myDate.getDay()];
            return;
          }
       }
       document.getElementById("spnDay").innerHTML="Invalid date";
    } 
    ...
    <select name="cboTarget" onFocus="getDays(this.form.txtDate_vis)" ..>
    I've also modified it to work cross-browser. Your original code will only work with IE because of direct reference to name (form) and id (td). And in case the target browser is IE only, it is a good habit to do it the proper way.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •