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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I get Now() to use the client date/time - NOT SERVER? 1

Status
Not open for further replies.

DrmWvr

Programmer
Apr 2, 2002
22
0
0
CA
Hi Guys,
this web site, I'm working on, is posting messages from across Canada and various time zones. Is there any way to get the local time into the SQL query below? Right now it is using the time from the web server, which is located one time zone to the east. And is one hour ahead.

<CFQUERY NAME=&quot;Update&quot; DATASOURCE=&quot;autonews&quot;>

INSERT INTO NewsMessages (title_e, title_f, itc_e, itc_f, message_e, message_f, <b>date_posted</b>, Expire)

VALUES ('#form.title_e#','#form.title_f#','#form.itc_e#','#form.itc_f#','#form.message_e#','#form.message_f#', <b>#CreateODBCDateTime(Now())#</b>,'#DateFormat(form.expire,&quot;DD/MM/YYYY&quot;)#')

</CFQUERY>

Thanks ahead of time.
I owe you one,
Marc
Québec, Canada
 
Ok, I guess I have to use Javascript, because it processes on the client and not the server. However, Javascript doesn't seem to have comparable Dateformat, Timeformat and Now fuctions.

Any suggestions?

Marc
 
That should read as Functions. Does this web site allow editing of threads?
 
Something like this could work:

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
[COLOR=666666]<!--[/color]
[COLOR=666666]/****************************** dateformat *********************************
*
* Object Extension for Date object
* description:
* -Stores day and month arrays in Date object
* -Provides a format function to 'pretty' print
* the date in custom formats
* parameters:
* format - accpets any variation of the following list
* yyyy is a 4-digit year - i.e., 2002
* yy is a 2-digit year - i.e., 02
* month is the full month - i.e., September
* mon is the first three letters of the month - i.e., Sep
* mmm is the number of the month - i.e., 9
* hh is hours - i.e., 3
* mm is minutes (always 2-digit) - i.e., 05
* ss is seconds (always 2-digit) - i.e., 08
* ddd is the first three letters of the day - i.e., Wed
* dd is the numerical day of the month - i.e, 25
* day is the full day of the week - i.e., Wednesday
* timezone is the the timezone in hours from GMT - i.e., GMT+5
* time24 is the time based on a 24 hour clock - i.e., 18:24
* time is the time based on am/pm - i.e., 6:24PM
* example:
* myDate = new Date()
* myDate.format(&quot;day, month dd, yyyy hh:mm:ss timezone&quot;)
* would return &quot;Wednesday, September 25, 2002 12:14:11 GMT-5&quot;
******************************************************************************/
[/color]

[COLOR=666666]//Store the date info in the Date object[/color]
Date.prototype.Months =
Code:
[
&quot;January&quot;, &quot;February&quot;, &quot;March&quot;,
&quot;April&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;,
&quot;August&quot;, &quot;September&quot;, &quot;October&quot;,
&quot;November&quot;, &quot;December&quot;
Code:
]
;
Date.prototype.Days =
Code:
[
&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;,
&quot;Wednesday&quot;, &quot;Thursday&quot;,
&quot;Friday&quot;, &quot;Saturday&quot;
Code:
]
;
Date.prototype.format = dateFormat;

function dateFormat(format) {
var dateString = format;

[COLOR=666666]//yyyy is a 4-digit year - i.e., 2002 [/color]
dateString = dateString.replace( new RegExp(&quot;yyyy&quot;, &quot;gi&quot;), this.getYear() );
[COLOR=666666]//yy is a 2-digit year - i.e., 02[/color]
dateString = dateString.replace( new RegExp(&quot;yy&quot;, &quot;gi&quot;), new String( this.getYear() ).substring(2,4) );
[COLOR=666666]//month is the full month - i.e., September[/color]
dateString = dateString.replace( new RegExp(&quot;month&quot;, &quot;gi&quot;), this.Months
Code:
[
this.getMonth()
Code:
]
);
[COLOR=666666]//mon is the first three letters of the month - i.e., Sep[/color]
dateString = dateString.replace( new RegExp(&quot;mon&quot;, &quot;gi&quot;), new String( this.Months
Code:
[
this.getMonth()
Code:
]
).substring(0,3) );
[COLOR=666666]//mmm is the number of the month - i.e., 9[/color]
dateString = dateString.replace( new RegExp(&quot;mmm&quot;, &quot;gi&quot;), (this.getMonth() + 1) );
[COLOR=666666]//hh is hours - i.e., 3[/color]
dateString = dateString.replace( new RegExp(&quot;hh&quot;, &quot;gi&quot;), this.getHours() );
[COLOR=666666]//mm is minutes (always 2-digit) - i.e., 05[/color]
var mm = new String( this.getMinutes() );
if (mm.length == 1) mm = &quot;0&quot; + mm; [COLOR=666666]//pad if single digit[/color]
dateString = dateString.replace( new RegExp(&quot;mm&quot;, &quot;gi&quot;), mm );
[COLOR=666666]//ss is seconds (always 2-digit) - i.e., 08[/color]
var ss = new String( this.getSeconds() );
if (ss.length == 1) ss = &quot;0&quot; + mm; [COLOR=666666]//pad if single digit[/color]
dateString = dateString.replace( new RegExp(&quot;ss&quot;, &quot;gi&quot;), ss );
[COLOR=666666]//ddd is the first three letters of the day - i.e., Wed[/color]
dateString = dateString.replace( new RegExp(&quot;ddd&quot;, &quot;gi&quot;), new String( this.Days
Code:
[
this.getDay()
Code:
]
).substring(0,3) );
[COLOR=666666]//dd is the numerical day of the month - i.e, 25[/color]
dateString = dateString.replace( new RegExp(&quot;dd&quot;, &quot;gi&quot;), this.getDate() );
[COLOR=666666]//day is the full day of the week - i.e., Wednesday[/color]
dateString = dateString.replace( new RegExp(&quot;day&quot;, &quot;gi&quot;), this.Days
Code:
[
this.getDay()
Code:
]
);

[COLOR=666666]//timezone is the the timezone in hours from GMT - i.e., GMT+5[/color]
tz = this.getTimezoneOffset();
timezone = &quot;&quot;;
if (tz < 0)
timezone = &quot;GMT-&quot; + tz / 60;
else if (tz == 0)
timezone = &quot;GMT&quot;;
else
timezone = &quot;GMT+&quot; + tz / 60;
dateString = dateString.replace( new RegExp(&quot;timezone&quot;, &quot;gi&quot;), timezone );

[COLOR=666666]//time24 is the time based on a 24 hour clock - i.e., 18:24 [/color]
var minutes = new String( this.getMinutes() );
if (minutes.length == 1) minutes = &quot;0&quot; + minutes; [COLOR=666666]//pad if single digit[/color]
var time24 = new String( this.getHours() + &quot;:&quot; + minutes );
dateString = dateString.replace( new RegExp(&quot;time24&quot;, &quot;gi&quot;), time24 );

[COLOR=666666]//time is the time based on am/pm - i.e., 6:24PM[/color]
var time;
var ampm;
var hour = this.getHours();
if ( hour < 12) {
if (hour == 0) hour = 12;
ampm = &quot;AM&quot;
} else {
if (hour !=12)
hour = hour - 12;
ampm = &quot;PM&quot;;
}
time = new String(hour + &quot;:&quot; + minutes + ampm);
dateString = dateString.replace( new RegExp(&quot;time&quot;, &quot;gi&quot;), time );

return dateString;
}


[COLOR=666666]//-->[/color]
</SCRIPT>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
[COLOR=666666]<!--[/color]
myDate = new Date();
document.write(myDate.format(&quot;day, month dd, yyyy hh:mm:ss timezone&quot;));
[COLOR=666666]//-->[/color]
</SCRIPT> - tleish
 
Thanks tleish,
it all works fine except for the year, which keeps showing 102 rather than 2002.

Marc
 
I hadn't checked on Netscape. To make JavaScript 1.2 ECMA-Script compliant, Netscape engineers changed the definition of getYear(). Now, getYear() will return the specified year minus 1900, as shown in Code Fragment #2.

So, I've replaced getYear() with getFullYear(). Here are the corrections:

<script language=&quot;JavaScript1.2&quot;>
[COLOR=666666]<!--[/color]
[COLOR=666666]/****************************** dateformat *********************************
*
* Object Extension for Date object
* description:
* -Stores day and month arrays in Date object
* -Provides a format function to 'pretty' print
* the date in custom formats
* parameters:
* format - accpets any variation of the following list
* yyyy is a 4-digit year - i.e., 2002
* yy is a 2-digit year - i.e., 02
* month is the full month - i.e., September
* mon is the first three letters of the month - i.e., Sep
* mmm is the number of the month - i.e., 9
* hh is hours - i.e., 3
* mm is minutes (always 2-digit) - i.e., 05
* ss is seconds (always 2-digit) - i.e., 08
* ddd is the first three letters of the day - i.e., Wed
* dd is the numerical day of the month - i.e, 25
* day is the full day of the week - i.e., Wednesday
* timezone is the the timezone in hours from GMT - i.e., GMT+5
* time24 is the time based on a 24 hour clock - i.e., 18:24
* time is the time based on am/pm - i.e., 6:24PM
* example:
* myDate = newDate()
* myDate.format(&quot;day, month dd, yyyy hh:mm:ss timezone&quot;)
* would return &quot;Wednesday, September 25, 2002 12:14:11 GMT-5&quot;
******************************************************************************/
[/color]

[COLOR=666666]//Store the date info in the Date object[/color]
Date.prototype.Months =
Code:
[
&quot;January&quot;, &quot;February&quot;, &quot;March&quot;,
&quot;April&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;,
&quot;August&quot;, &quot;September&quot;, &quot;October&quot;,
&quot;November&quot;, &quot;December&quot;
Code:
]
;
Date.prototype.Days =
Code:
[
&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;,
&quot;Wednesday&quot;, &quot;Thursday&quot;,
&quot;Friday&quot;, &quot;Saturday&quot;
Code:
]
;
Date.prototype.format = dateFormat;

function dateFormat(format) {
var dateString = format;

[COLOR=666666]//yyyy is a 4-digit year - i.e., 2002 [/color]
dateString = dateString.replace( new RegExp(&quot;yyyy&quot;, &quot;gi&quot;), this.getFullYear() );
[COLOR=666666]//yy is a 2-digit year - i.e., 02[/color]
dateString = dateString.replace( new RegExp(&quot;yy&quot;, &quot;gi&quot;), new String( this.getFullYear() ).substring(2,4) );
[COLOR=666666]//month is the full month - i.e., September[/color]
dateString = dateString.replace( new RegExp(&quot;month&quot;, &quot;gi&quot;), this.Months
Code:
[
this.getMonth()
Code:
]
);
[COLOR=666666]//mon is the first three letters of the month - i.e., Sep[/color]
dateString = dateString.replace( new RegExp(&quot;mon&quot;, &quot;gi&quot;), new String( this.Months
Code:
[
this.getMonth()
Code:
]
).substring(0,3) );
[COLOR=666666]//mmm is the number of the month - i.e., 9[/color]
dateString = dateString.replace( new RegExp(&quot;mmm&quot;, &quot;gi&quot;), (this.getMonth() + 1) );
[COLOR=666666]//hh is hours - i.e., 3[/color]
dateString = dateString.replace( new RegExp(&quot;hh&quot;, &quot;gi&quot;), this.getHours() );
[COLOR=666666]//mm is minutes (always 2-digit) - i.e., 05[/color]
var mm = new String( this.getMinutes() );
if (mm.length == 1) mm = &quot;0&quot; + mm; [COLOR=666666]//pad if single digit[/color]
dateString = dateString.replace( new RegExp(&quot;mm&quot;, &quot;gi&quot;), mm );
[COLOR=666666]//ss is seconds (always 2-digit) - i.e., 08[/color]
var ss = new String( this.getSeconds() );
if (ss.length == 1) ss = &quot;0&quot; + mm; [COLOR=666666]//pad if single digit[/color]
dateString = dateString.replace( new RegExp(&quot;ss&quot;, &quot;gi&quot;), ss );
[COLOR=666666]//ddd is the first three letters of the day - i.e., Wed[/color]
dateString = dateString.replace( new RegExp(&quot;ddd&quot;, &quot;gi&quot;), new String( this.Days
Code:
[
this.getDay()
Code:
]
).substring(0,3) );
[COLOR=666666]//dd is the numerical day of the month - i.e, 25[/color]
dateString = dateString.replace( new RegExp(&quot;dd&quot;, &quot;gi&quot;), this.getDate() );
[COLOR=666666]//day is the full day of the week - i.e., Wednesday[/color]
dateString = dateString.replace( new RegExp(&quot;day&quot;, &quot;gi&quot;), this.Days
Code:
[
this.getDay()
Code:
]
);

[COLOR=666666]//timezone is the the timezone in hours from GMT - i.e., GMT+5[/color]
tz = this.getTimezoneOffset();
timezone = &quot;&quot;;
if (tz < 0)
timezone = &quot;GMT-&quot; + tz / 60;
else if (tz == 0)
timezone = &quot;GMT&quot;;
else
timezone = &quot;GMT+&quot; + tz / 60;
dateString = dateString.replace( new RegExp(&quot;timezone&quot;, &quot;gi&quot;), timezone );

[COLOR=666666]//time24 is the time based on a 24 hour clock - i.e., 18:24 [/color]
var minutes = new String( this.getMinutes() );
if (minutes.length == 1) minutes = &quot;0&quot; + minutes; [COLOR=666666]//pad if single digit[/color]
var time24 = new String( this.getHours() + &quot;:&quot; + minutes );
dateString = dateString.replace( new RegExp(&quot;time24&quot;, &quot;gi&quot;), time24 );

[COLOR=666666]//time is the time based on am/pm - i.e., 6:24PM[/color]
var time;
var ampm;
var hour = this.getHours();
if ( hour < 12) {
if (hour == 0) hour = 12;
ampm = &quot;AM&quot;
} else {
if (hour !=12)
hour = hour - 12;
ampm = &quot;PM&quot;;
}
time = new String(hour + &quot;:&quot; + minutes + ampm);
dateString = dateString.replace( new RegExp(&quot;time&quot;, &quot;gi&quot;), time );

return dateString;
}


[COLOR=666666]//-->[/color]
</script>

<script language=&quot;JavaScript&quot;>
[COLOR=666666]<!--[/color]
myDate = new Date();
document.write(myDate.format(&quot;day, month dd, yyyy hh:mm:ss timezone&quot;));
[COLOR=666666]//-->[/color]
</script> - tleish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top