Timezone independent time at client side - javascript

I need to obtain the server time but I only have javascript to play with. I need the time from the NA and EU regions. Is there a way to obtain server time. OR is there a JS code snippet that can help me achieve the same?
Just to make things clear, I am not looking for UTC. I haev an application that will do something for the user at a certain time. The application will be used in NA and EU regions where there is a time difference. I need the time from the server. Can it be done?

With jquery, i found this plugin.

You may want to consider using UTC, so your users see a unified time instead of trying to display the server time.
http://www.w3schools.com/jsref/jsref_getutcdate.asp

The most reliable way of determining the time from the server is to ask it !
Anything derived from the client's clock will be more prone to clock skew or other errors because client machines don't usually have decent synchronisation methods like NTPv3 that are typically deployed on servers.
A trivial CGI script (PHP, perl, whatever) on the server combined with an AJAX call to get the data would do the job nicely.

Related

Convert New York time to UTC using Javascript [duplicate]

Now, before you mark this as a duplicate, please note that all the useful answers in the possible duplicate provide functions which work in relation to the current system time, which I cannot accept.
What I want, is that the current time in, say, for example, New York, be consistent to all users, no matter what their time settings are.
For example, I have two computers here, and one is 3 seconds behind the other, and this leaves an unacceptable gap, as my task involve pinpoint precision (slightly exaggerated, but you know what I mean).
How can I overcome this? Must I get the server time and somehow convert it? If so, I'm using PHP.
Hope to not be wrong but javascript is loaded on user machine, not on the server so you will allway get the visitor date/time using javascript. you can use php to get the date/time because is loaded on the server and pass the dates to javascript.
Hope this help
You need to use a timezone library to do this in JavaScript. See my answer here.
The scripts are small, but because they require the timezone database, it can get unwieldy for a client application. You're probably better off doing the conversion server side. Perhaps the pytz library will be useful to you.

How to get the time of specific timezone using javascript, but without being relevant to the current system time?

Now, before you mark this as a duplicate, please note that all the useful answers in the possible duplicate provide functions which work in relation to the current system time, which I cannot accept.
What I want, is that the current time in, say, for example, New York, be consistent to all users, no matter what their time settings are.
For example, I have two computers here, and one is 3 seconds behind the other, and this leaves an unacceptable gap, as my task involve pinpoint precision (slightly exaggerated, but you know what I mean).
How can I overcome this? Must I get the server time and somehow convert it? If so, I'm using PHP.
Hope to not be wrong but javascript is loaded on user machine, not on the server so you will allway get the visitor date/time using javascript. you can use php to get the date/time because is loaded on the server and pass the dates to javascript.
Hope this help
You need to use a timezone library to do this in JavaScript. See my answer here.
The scripts are small, but because they require the timezone database, it can get unwieldy for a client application. You're probably better off doing the conversion server side. Perhaps the pytz library will be useful to you.

Best way to get php server time in jquery

I am building a web app using this calendar.
Jquery Based Full Calendar
This calendar is based on jquery and jquery is client side code so it works on client side time.I want this calendar to use my php server time for its whole working.
So which would be the best method for this.
Should i get server time every time once this calendar gets loaded
and keep it counting using that time .
Or should i sync it with server at every 57sec and get server time.
Make your users select a time zone and calculate their time with UTC time you get from your server. Most of the websites use this system as I know.
You should take a look at this question - it should have a lot of useful information for you in regards to your question about syncing time.
That said, think about how your application scales and work from there. A request per minute can easily add up to a load that isn't easy to handle.
Do you really need real-time syncing of time events if you're just going to have people schedule events at a future period of time/date? If this is for a calendar, you really just need to make sure that the client's time matches your at the hour and minute level, and that the dates are the same. After that, you can rely on the client's time, unless you're scheduling by the second. (Then again, I don't know what this calendar is for...)
Additionally, if you're saving their events server side, I'm not sure that it would do you any good to sync time in the way you're considering. If you're looking to push updates to all calendars at once when someone makes a change, you might look into alternative ways to do that.
Good luck - I hope this helps.

cron job vs Javascript timing event

I'm developing website where I need to execute one code at particular time.
Which is faster and better choice to write Cron Job or to use JavaScript Timing Event
or something similar or JavaScript
You are asking a question about two completely different things.
Cron job is based on the server, JavaScript (unless you are using NodeJS) is based on the client. Depending on whether this is a task that:
must be performed and cannot be relied on the client (eg. the data is sensitive), or
can depend on the client execution (which means the browser window should remain open and the JavaScript should be enabled),
choose Cron (1) or JavaScript (2) respectively.
It is really like a comparison between apples and oranges. Unless you will tell us whether you want orange juice or apple pie, we won't be able to help you more. Just remember that Cron is for more reliable server-side task execution, and JavaScript timeout is per-user (or rather per-client), less reliable execution.
It entirely depends on the nature of the code you need to execute at a particular time.
If it's something that has to happen every day at 2pm or whatever, regardless of whether or not anyone's looking at the website, then you should use a cron job for that.
On the other hand, if it's something that needs to execute at a certain time per user (i.e., to automatically log a user out of a page after some amount of idle time), then the appropriate call is Javascript timing functions.
Javascript timing functions will only work if someone's actually looking at the page, and then it'll be called multiple times for multiple users, which may or may not be desirable depending on your situation.
Of course, you may be running Node.js on the server, in which case you can use Javascript timing functions as if they were a cron job.
In short, use cron
In your comment to another answer, you said:
I want to execute php function every week one time
In this case, you have one main option (assuming you are using *nix) and that is cron (I don't know what the Windows alternative is). Cron is specifically designed for this function, and whether or not you choose to use it, it is most likely running on your server anyway (for other system functions) so speed is not an issue.
Don't use Node.js
Node.js is an alternative serverside technology to PHP. You would use it server side instead of php. If you're already using PHP, then forget it. The only reason Node.js has been mentioned is because you've asked about JavaScript.
Also, for a weekly timing event, A JavaScript timer wouldn't be a good idea. The setTimeout() function works in milliseconds, and is good for working in seconds and minutes (possibly hours), but not weeks.
If you were to use serverside JavaScript (like Node.js), you would probably need to do something similar to the PHP Alternative below.
PHP Alternative
Of course, depending on your hosting environment (especially cheaper ones), cron may not be available. In this case you would have to come up with a different strategy, and you would probably be best to use PHP. Something that I've seen done before goes along these lines:
Have some register of jobs that need to be performed. (In a database, or a file, or whatever)
Every time you run your main PHP script (usually index.php), check the register to see if there are any outstanding jobs.
2.a. Run the job.
2.b. Update the register, so you remember the last time the job was performed.
Pros:
It works if you don't have access to cron.
Cons:
If your script is not run very often (because this method relies on people visiting your page), your jobs may not be run as often as you like.
If your script is run very often, you will suffer unnecessary overhead in your script.
If your jobs take a long time to run, it will effect the page load times.
You're basically replicating cron, but using PHP which is far less efficient than using cron.
It's unlikely (unless you invest a lot of time) that you'll develop a solution that is as good as cron.
For a javascript timing event to run you would need to open the webpage. That means you have to expose that page publicly. You don't want to do that. Cron jobs are easy and effective. I like them. You should do that.

what is the fastest way to get server time in JavaScript from a web browser?

in fact, i wanna synchronize client-side time with server time, this solution is really good, but there are any other ideas faster? in other words, can we find a faster way to get server time in JavaScript from a web browser?
Sway - the link you mentioned is the better way to go, i dont think you can anyway sync with server clock without knowing what is the time on the server.
in the below example he has given a good pictorial representation too.
http://codemadesimple.wordpress.com/2012/06/18/timesync-with-asp-net-mvc-4/

Categories