Solved

Need help fixing code so the input field named startdt's time is local computer's time zone

Posted on 2007-03-19
12
248 Views
Last Modified: 2013-11-18
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.value = darray[1] + darray[2];} if(document.form1.enddt.value == '')
{ document.form1.enddt.value = 'March 31 2010 10:00:00 AM';}  if((document.form1.order.value == '') ||  (document.form1.order[0].value == ''))
{ if(document.form1.order.length == undefined)  
{ document.form1.order.value = '1'; }
else { document.form1.order[0].value = ']]>
<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())

0
Comment
Question by:TrueBlue
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
12 Comments
 
LVL 5

Expert Comment

by:Oscurochu
ID: 18753615
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.
0
 
LVL 9

Expert Comment

by:owenli27
ID: 18753736
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.value = darray[1];} if(document.form1.enddt.value == ''){ document.form1.enddt.value = '31 Oct 2008 23:00:00 UTC';}  if((document.form1.order.value == '') ||  (document.form1.order[0].value == '')) { if(document.form1.order.length == undefined)  { document.form1.order.value = '1'; } else { document.form1.order[0].value = ']]> <xsl:value-of select="$users + 1"/><![CDATA['; }}  if(doSubmit) { document.form1.submit(); } ]]>
      </xsl:attribute>
  </INPUT>
 </td>
0
 
LVL 9

Expert Comment

by:owenli27
ID: 18753755
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.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:TrueBlue
ID: 18753875
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
0
 
LVL 9

Expert Comment

by:owenli27
ID: 18753938
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.
0
 

Author Comment

by:TrueBlue
ID: 18755407
Owen,
I am pretty sure that the hosting company's server is EST.
0
 
LVL 9

Expert Comment

by:owenli27
ID: 18756057
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()+(curdate.getTimezoneOffset()*60*1000)-(4*60*60*1000)); var dstring1 = hostdate.toLocaleString(); var darray = dstring1.split(',');  document.form1.startdt.value = darray[1] + darray[2];} if(document.form1.enddt.value == ''){ document.form1.enddt.value = 'March 31 2010 10:00:00 AM';}  if((document.form1.order.value == '') ||  (document.form1.order[0].value == '')) { if(document.form1.order.length == undefined)  { document.form1.order.value = '1'; } else { document.form1.order[0].value = ']]> <xsl:value-of select="$users + 1"/><![CDATA['; }}  if(doSubmit) { document.form1.submit(); } ]]>
      </xsl:attribute>
  </INPUT>
</td>
0
 

Author Comment

by:TrueBlue
ID: 18756194
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
0
 
LVL 9

Expert Comment

by:owenli27
ID: 18756786
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','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); var curdate = new Date(); var d = new Date(curdate.getTime()+(curdate.getTimezoneOffset()*60*1000)-(4*60*60*1000)); document.form1.startdt.value = 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.value == ''){ document.form1.enddt.value = 'Mar 31 2010 10:00:00 AM';}  if((document.form1.order.value == '') ||  (document.form1.order[0].value == '')) { if(document.form1.order.length == undefined)  { document.form1.order.value = '1'; } else { document.form1.order[0].value = ']]> <xsl:value-of select="$users + 1"/><![CDATA['; }}  if(doSubmit) { document.form1.submit(); } ]]>
      </xsl:attribute>
  </INPUT>
</td>
0
 

Author Comment

by:TrueBlue
ID: 18757609
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
0
 
LVL 9

Accepted Solution

by:
owenli27 earned 400 total points
ID: 18758015
Sorry, it was my mistake. Please change d.getDay()  to d.getDate()

getDay()  - Returns the day of the week from a Date object
getDate() - Returns the day of the month from a Date object
0
 
LVL 9

Expert Comment

by:owenli27
ID: 18758030
By the way,  You don't have to add points for asking any questions in one same question.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question