How change time in chat window to UTC for all.

65 views
Skip to first unread message

Tomek LimaVictor

unread,
Mar 10, 2016, 4:36:00 PM3/10/16
to AJAX-chat
Hello All. Please help. How do I change the time display in UTC format in the chat window for all users.  At this time for any time zone, you have your local time. I want to display UTC time for all time zones. I want to be in the chat window when the message universal time format UTC. How to change it, please help. Thank you for any help 

Tomek LimaVictor

unread,
Mar 28, 2016, 4:26:28 PM3/28/16
to AJAX-chat
Friends once again, I ask you for help, if someone is able to help me in solving this problem? 

Frug

unread,
Apr 10, 2016, 1:10:14 PM4/10/16
to ajax...@googlegroups.com
Interestingly the date timezone conversion appears to be done automaetically. Messages coming from the server are stamped with a 4 digit offset (eg: <message id="223493" dateTime="Sun, 10 Apr 2016 16:22:24 +0000" ... for UTC )

Javscript then takes that string in the constructor for the Date object and converts it to your browser's time automagically. Something I didn't know was a thing.


I can't find a clean way to make javascript ignore the offset, but one option is to add/subtract the right amount from the date before displaying it. You can get the offset in minutes with getTimezoneOffset, then convert that to seconds and create a new date with that number of seconds added to it.

var dateObj = new Date(dateObj.getTime() + dateObj.getTimezoneOffset()*60000);


So in your custom.js you would add this:

ajaxChat.formatDate = (format, date) {
 date
= (date === null) ? new date() : date;

 date
= new Date(date.getTime() + date.getTimezoneOffset()*60000);

 
return format
 
.replace(/%Y/g, date.getFullYear())
 
.replace(/%m/g, this.addLeadingZero(date.getMonth()+1))
 
.replace(/%d/g, this.addLeadingZero(date.getDate()))
 
.replace(/%H/g, this.addLeadingZero(date.getHours()))
 
.replace(/%i/g, this.addLeadingZero(date.getMinutes()))
 
.replace(/%s/g, this.addLeadingZero(date.getSeconds()));
}



And be sure to clear your cache or do a hard refresh to see the changes. All I've added there is one line to reset the date.

Another solution is to just ask specifically for the UTC minutes and hours (and month, and date, and seconds, I guess). 

ajaxChat
.formatDate = (format, date) {
 date
= (date === null) ? new date() : date;


 
return format
 
.replace(/%Y/g, date.getFullYear())
 
.replace(/%m/g, this.addLeadingZero(date.getUTCMonth()+1))
 
.replace(/%d/g, this.addLeadingZero(date.getUTCDate()))
 
.replace(/%H/g, this.addLeadingZero(date.getUTCHours()))
 
.replace(/%i/g, this.addLeadingZero(date.getUTCMinutes()))
 
.replace(/%s/g, this.addLeadingZero(date.getUTCSeconds()));
}

Tomek LimaVictor

unread,
Apr 10, 2016, 4:26:38 PM4/10/16
to AJAX-chat
Hello Frug,
thank you for your reply and help. Unfortunately, when i add a script to my custom.js, I do not see changes to UTC time format. I delete browsing history and cache, but the format of the time still have local. 
Regards

Frug

unread,
Apr 10, 2016, 4:31:31 PM4/10/16
to AJAX-chat
You can try editing it into your chat.js directly
Reply all
Reply to author
Forward
0 new messages