Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert a db value to local date/time

Status
Not open for further replies.

vilaine

Programmer
Jul 18, 2001
16
US
I want to take a db date/time value in GMT and convert it to the local user's date/time. We have a lot of site users all over the world and I thought it would be helpful for them if it were easier to know when our online events were going to happen by seeing it in their own date/time. I've seen other pages that talk about doing this for forum systems, but those rely on cookies and I can't distill out just the info I need.

Currently I've got my js (below) set to read the user's system clock for the initial date/time, but eventually I'll pass in the value from the db. It determines the difference between their date/time and GMT, does the calculations to adjust the time difference, and then displays it.

Here's my test page if you want to see it in action.

I can't figure out if it will work when it's not Daylight Saving Time (DST), but I'm using ColdFusion and there's a function to determine DST so I could manipulate the value before I pass it into the Javascript. Time zones are always really hard for me to figure out.

Thanks,
Vilaine (Jessica)

<!--- need to send the database GMT date/time field into this --->
<script language=&quot;JavaScript&quot;>
myDate = new Date() //get the date/time from the user's machine
document.write('my system date/time is ' + myDate + '<BR>');
diff = myDate.getTimezoneOffset(); //get the difference between their date/time and GMT
document.write('the time difference in minutes ' + diff + '<BR>');
if ((navigator.appVersion.indexOf('MSIE 3') != -1)) diff = diff * (-1);
myDate.setTime(myDate.getTime() + diff*60*1000);
timezone = (diff/60);
myDate.setTime(myDate.getTime() + -timezone*60*60*1000);
document.write('my converted date/time is ' + myDate.toLocaleString());
</SCRIPT>
 
Cool! I'm glad it works. I know that it just reads the user's system time so I shouldn't need to worry about DST, but I just wanted to make sure it was correct.

So how do I have it convert all the values that come from the db to the user's time? I know I can pass CF values into it, but I'm really bad at javascript and can't figure out how to make it do all of them at once. This should be possible, right? As far as I understand, the CF will process, then the javascript would read the user's machine and the data would be processed server-side.

Thanks for your help,
Jess
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top