Time ticks on client [duplicate] - javascript

This question already has answers here:
The best way to synchronize client-side javascript clock with server date
(8 answers)
Closed 9 years ago.
I have a scenario in which I have to show ticking time to the user of a post. The post will come with is original DateTime string from server then the values will be calculated on client to find the difference of the time elapsed. The issue is that client time can't be relied upon. So the decision is that to retrieve the server current time and find the difference between the client and server and add that difference to the client date and then add the difference to the client time before calcuating the time elapsed.. however I don't know how to find the difference in javascript and add it client date variable.. Can someone talk on this scenario and codify it.. I am not sure whether I am doing right or is there alternative to it?

So I come up with the solution now.. as I mentioned to use the variable to store the server current time and update that variable after each specific interval in setinterval. This variable is going to work as our client side clock and compare the post time with it.. the issue with client clock is that it can't be trusted..

Related

What is Firebase current timestamp is for? [duplicate]

What would you recommend to store dates using Firebase between a datetime and a timestamp, and why?
Use a timestamp as this is locale-agnostic and does not require formatting and parsing agreements between clients. It's also a bit shorter.
Furthermore, utilize Firebase.ServerValue.TIMESTAMP, rather than trusting the clients to have their clocks set correctly. If, for example, a client's clock is off by 5 minutes and you utilize the client timestamps in a chat conversation, then rendering back the messages, the time shown would appear to be 5 minutes in the future for other users.

Backend increment counter PHP or javascript [duplicate]

This question already has answers here:
PHP Increment a counting variable in a text file
(5 answers)
Closed 6 years ago.
After researching I am not really sure which is a matching solution for my project. I want a serverside number incr. counter, that daily resets.
Every website vistor should see the same number of the counter. Is there
a PHP script or javascript meaningful? Can I store the daily counts in a txt.-file without using a database like MySql?
It would be helpful if you specified a little further what you are trying to achieve. What is the counter counting? number of visits? number of clicks?
You could store this in a text file. I wouldn'r recommend it thought, it will be bulky and you will run into trouble when several updates takes place simultaneously. A better solution if you want an easy and file-based database, check SQLite.

Best way to deal with dates based on location. SQL/JS [duplicate]

This question already has answers here:
How can I handle time zones in my webapp?
(7 answers)
Closed 9 years ago.
Simple scenario:
John Doe lives in California and posts a comment.
Jane Doe lives in Maryland and views the comment.
I'm used to dealing with local based websites and usually use GETDATE(). What's the best way to deal with this so that John and Jane see the date based on their time location? Do I send a parameter to the SQL query based on a javascript function to grab their timezone? Is there a better way?
A good example is Facebook. How do they deal with all the different time zones? Is it client side? server side? etc.
Thanks!
The handling is usually two fold. To handle it on server side, you'll need reliable time zone information. This is usually obtained from the user's preferences for time zone in their profile page.
Having said that we need to make sure that all date data captured on client side and when stored in DB is in UTC format. JS functions have a ready support for the UTC datetime.
When you store you use UTC() method in JS to get date.
When displaying back either you supply the time-zone equivalent from DB(based on stored user time zone data) or use JS to get it from the browser locale like this
<script type="text/javascript">
function showDateInClientSideFormat(dValue)
{
var d = new Date()
var n = d.getTimezoneOffset();
var dateClientSide = new Date(dValue +n);
return dateClientSide;
}
</script>
Also see my answer here https://stackoverflow.com/a/21573366/1123226

Sync server and client time? [duplicate]

This question already has answers here:
The best way to synchronize client-side javascript clock with server date
(8 answers)
Closed 9 years ago.
So my question is really two parts, firstly; I want to measure the total latency of my requests and client-side js, including time spent waiting for dns to resolve etc.
This all works fine in development because my server and web browser are all on the same machine, so when I compare Time.now.to_i, with the current time as determined with Javascript in the browser it's the true latency of the entire request end-to-end.
The second part of my question is, am I just thinking about this whole thing in the wrong way? Should I be measuring my requests in a different fashion?
P.S., this is not a rails app.
You can't reliably sync time in a browser.
If you just want to measure request time, you don't really need to care what time the server thinks it is. In the browser, just measure the time between initiating the request and when you get a response.
var start = Date.now();
$.get('/url', function() { // or equivalent in whatever JS framework
var latency = Date.now() - start; // in ms
});

Getting Olson timezone ID information from browser via javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Javascript/PHP and timezones
It is possible to get the Olson timezone id from javascript on the client's machine e.g. (America/New York). I know that PHP can do this via the timezone object, something like this. $timezone->getLocation(); Does similar functionality exist for JS?
I know that you can grab the timezone offset of the client as followed:
var curdate = new Date();
var offset = curdate.getTimeZoneOffset();
But I need more granular information provided by the Olson Id. Thank you for your help.
You can't directly access timezone in JavaScript. You can, however, measure offsets reported at several different specific dates to deduce what exactly time zone is in use by comparing regular and daylight savings times to database of zones. There's a jsTimezoneDetect library that can do most of this work for you.

Categories

Resources