View Full Version : HELP! Converting dates from UTC Format to GMT

04-21-2005, 04:08 PM
:mad: Im 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:

function getDays(aText)
myDate=new Date(eval('"'+aText.value+'"'));

<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">
<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>

04-22-2005, 06:03 AM
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.

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="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.