TrueBlue
asked on
Need help fixing code so the input field named startdt's time is local computer's time zone
Hello,
One of the gurus on this site wrote this code...
<td align="center">Start Date
<input maxLength="29" id="startdt" name="startdt" size="19" value=""/>
</td>
<td align="center">End Date
<input maxLength="29" id="enddt" name="enddt" size="19" value=""/>
</td>
<td align="center">
<INPUT type="button" name="add" value="Add">
<xsl:attribute name="onclick">
<![CDATA[ if(document.form1.startdt. value == ''){ var curdate = new Date(); var dstring1 = curdate.toLocaleString(); var darray = dstring1.split(',');
document.form1.startdt.val ue = darray[1] + darray[2];} if(document.form1.enddt.va lue == '')
{ document.form1.enddt.value = 'March 31 2010 10:00:00 AM';} if((document.form1.order.v alue == '') || (document.form1.order[0].v alue == ''))
{ if(document.form1.order.le ngth == undefined)
{ document.form1.order.value = '1'; }
else { document.form1.order[0].va lue = ']]>
<xsl:value-of select="$users + 1"/>
<![CDATA['; }} if(doSubmit) { document.form1.submit(); } ]]>
</xsl:attribute>
</INPUT>
</td>
The time is off by one hour. I thought it was the hosting company.
Any ideas how to fix the time on the startdt so it is an hour later (Eastern Standard Time) or whatever the local computer's time zone is? (I think it needs one of these functions toGMTString()
toUTCString())
One of the gurus on this site wrote this code...
<td align="center">Start Date
<input maxLength="29" id="startdt" name="startdt" size="19" value=""/>
</td>
<td align="center">End Date
<input maxLength="29" id="enddt" name="enddt" size="19" value=""/>
</td>
<td align="center">
<INPUT type="button" name="add" value="Add">
<xsl:attribute name="onclick">
<![CDATA[ if(document.form1.startdt.
document.form1.startdt.val
{ document.form1.enddt.value
{ if(document.form1.order.le
{ document.form1.order.value
else { document.form1.order[0].va
<xsl:value-of select="$users + 1"/>
<![CDATA['; }} if(doSubmit) { document.form1.submit(); } ]]>
</xsl:attribute>
</INPUT>
</td>
The time is off by one hour. I thought it was the hosting company.
Any ideas how to fix the time on the startdt so it is an hour later (Eastern Standard Time) or whatever the local computer's time zone is? (I think it needs one of these functions toGMTString()
toUTCString())
javascript is never because of the host. javascript is a CLIENTside script. everything is obtained from the user viewing the webpage. so the time displayed in javascript is representing the time on your pc. if it isn't then your script is simply not correct.
Agree with Oscurochu. You cannot get hosting server current time by using JavaScript. You surely can use UTC time by changing code to:
<td align="center">Start Date
<input maxLength="29" id="startdt" name="startdt" size="19" value=""/>
</td>
<td align="center">End Date
<input maxLength="29" id="enddt" name="enddt" size="19" value=""/>
</td>
<td align="center">
<INPUT type="button" name="add" value="Add">
<xsl:attribute name="onclick"><![CDATA[ if(document.form1.startdt. value == ''){ var curdate = new Date(); var dstring1 = curdate.toUTCString(); var darray = dstring1.split(','); document.form1.startdt.val ue = darray[1];} if(document.form1.enddt.va lue == ''){ document.form1.enddt.value = '31 Oct 2008 23:00:00 UTC';} if((document.form1.order.v alue == '') || (document.form1.order[0].v alue == '')) { if(document.form1.order.le ngth == undefined) { document.form1.order.value = '1'; } else { document.form1.order[0].va lue = ']]> <xsl:value-of select="$users + 1"/><![CDATA['; }} if(doSubmit) { document.form1.submit(); } ]]>
</xsl:attribute>
</INPUT>
</td>
<td align="center">Start Date
<input maxLength="29" id="startdt" name="startdt" size="19" value=""/>
</td>
<td align="center">End Date
<input maxLength="29" id="enddt" name="enddt" size="19" value=""/>
</td>
<td align="center">
<INPUT type="button" name="add" value="Add">
<xsl:attribute name="onclick"><![CDATA[ if(document.form1.startdt.
</xsl:attribute>
</INPUT>
</td>
But the date/time format will be changed to '31 Oct 2008 23:00:00 UTC'. If you want to keep the original format, I can rewrite JavaScript to do formating. It is pretty simple.
ASKER
Owen,
It would be great if you could rewrite the Javascript to get the original format as I am having to send this information to a third party webservice.
TIA
It would be great if you could rewrite the Javascript to get the original format as I am having to send this information to a third party webservice.
TIA
No problem. I will do it tomorrow early morning. It is 11:25 PM here now.
Actually, I got an idea to get the hosting server current time if it always be 5 hours different from GMC.
Actually, I got an idea to get the hosting server current time if it always be 5 hours different from GMC.
ASKER
Owen,
I am pretty sure that the hosting company's server is EST.
I am pretty sure that the hosting company's server is EST.
It should be 4 hours difference between EST and UST. So, try this code:
<td align="center">Start Date
<input maxLength="29" id="startdt" name="startdt" size="19" value=""/>
</td>
<td align="center">End Date
<input maxLength="29" id="enddt" name="enddt" size="19" value=""/>
</td>
<td align="center">
<INPUT type="button" name="add" value="Add">
<xsl:attribute name="onclick"><![CDATA[ if(document.form1.startdt. value == ''){ var curdate = new Date(); var hostdate = new Date(curdate.getTime()+(cu rdate.getT imezoneOff set()*60*1 000)-(4*60 *60*1000)) ; var dstring1 = hostdate.toLocaleString(); var darray = dstring1.split(','); document.form1.startdt.val ue = darray[1] + darray[2];} if(document.form1.enddt.va lue == ''){ document.form1.enddt.value = 'March 31 2010 10:00:00 AM';} if((document.form1.order.v alue == '') || (document.form1.order[0].v alue == '')) { if(document.form1.order.le ngth == undefined) { document.form1.order.value = '1'; } else { document.form1.order[0].va lue = ']]> <xsl:value-of select="$users + 1"/><![CDATA['; }} if(doSubmit) { document.form1.submit(); } ]]>
</xsl:attribute>
</INPUT>
</td>
<td align="center">Start Date
<input maxLength="29" id="startdt" name="startdt" size="19" value=""/>
</td>
<td align="center">End Date
<input maxLength="29" id="enddt" name="enddt" size="19" value=""/>
</td>
<td align="center">
<INPUT type="button" name="add" value="Add">
<xsl:attribute name="onclick"><![CDATA[ if(document.form1.startdt.
</xsl:attribute>
</INPUT>
</td>
ASKER
Owen,
It has the correct time :)
Is there a way to do a substring or something of the month's name so that it would only be the first three letters, for example instead of March it would be Mar (the rest of the format is fine)?
TIA
It has the correct time :)
Is there a way to do a substring or something of the month's name so that it would only be the first three letters, for example instead of March it would be Mar (the rest of the format is fine)?
TIA
try this way: (If you don't want to display Seconds in time, I can remove it easily)
<td align="center">Start Date
<input maxLength="29" id="startdt" name="startdt" size="19" value=""/>
</td>
<td align="center">End Date
<input maxLength="29" id="enddt" name="enddt" size="19" value=""/>
</td>
<td align="center">
<INPUT type="button" name="add" value="Add">
<xsl:attribute name="onclick"><![CDATA[ if(document.form1.startdt. value == ''){var monNames = new Array('Jan','Feb','Mar','A pr','May', 'Jun','Jul ','Aug','S ep','Oct', 'Nov','Dec '); var curdate = new Date(); var d = new Date(curdate.getTime()+(cu rdate.getT imezoneOff set()*60*1 000)-(4*60 *60*1000)) ; document.form1.startdt.val ue = monNames[d.getMonth()] + ' ' + d.getDay() + ' ' + d.getFullYear() + ' ' + ((h = d.getHours() % 12) ? h : 12) + ':' + d.getMinutes() + ':' + d.getSeconds() + ' ' + (d.getHours() < 12 ? 'AM' : 'PM');}
if(document.form1.enddt.va lue == ''){ document.form1.enddt.value = 'Mar 31 2010 10:00:00 AM';} if((document.form1.order.v alue == '') || (document.form1.order[0].v alue == '')) { if(document.form1.order.le ngth == undefined) { document.form1.order.value = '1'; } else { document.form1.order[0].va lue = ']]> <xsl:value-of select="$users + 1"/><![CDATA['; }} if(doSubmit) { document.form1.submit(); } ]]>
</xsl:attribute>
</INPUT>
</td>
<td align="center">Start Date
<input maxLength="29" id="startdt" name="startdt" size="19" value=""/>
</td>
<td align="center">End Date
<input maxLength="29" id="enddt" name="enddt" size="19" value=""/>
</td>
<td align="center">
<INPUT type="button" name="add" value="Add">
<xsl:attribute name="onclick"><![CDATA[ if(document.form1.startdt.
if(document.form1.enddt.va
</xsl:attribute>
</INPUT>
</td>
ASKER
Owen,
I just tried it and got Mar 2 2007 12:26:20 PM for the startdt.
The only problem I see is the second digit is missing from the day of the month.
Any ideas?
TIA
I just tried it and got Mar 2 2007 12:26:20 PM for the startdt.
The only problem I see is the second digit is missing from the day of the month.
Any ideas?
TIA
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
By the way, You don't have to add points for asking any questions in one same question.