How to get current server UTC time in JavaScript - javascript

var k=new Date();
alert(k);
It alert local time. But I need to display current server UTC time.

JavaScript is basically on client side. You will not be able to get server time in java script as it will be executed on client and give you the server time only (Node.js is exception here).
If you really wish to have server time in javascript. You can write a Api or Webservice on serverside which will give you server time in format you want. Call this webservice/Api using XMLHTTP or Ajax request and parse the response and display the time as per your wish.
Ajax post Code Example :
$.post( "Your Web Service Page", function( data ) {
//Assuming that Your web service will not need any paramerter
and just write the server time in utc in response
alert(data );
});

Try this new Date().toUTCString()

Related

Last Modified timestamp

I am trying to add the last modified timestamp on a webpage but I want the date to change only one the page is modified.
Here is the code I am using:
<script>
var date = document.lastModified;
document.write("Last Modified: "+date);
</script>
But this is showing the current date, so each time I visit the page I see today's date. How can I change to have the date updated only when the page is updated?
Many thanks.
That is because the js file gets sent to the client browser and the browser is executing that command to check on the lastModified - being current data/time of the client.
I do not see a way to get it from js - you would have to put that information from server side - will not be possible to read it on client side.
you can't use JS here because "lastModified" generates on runtime.
"lastModified" should be part of static page content (like Last Modified:
10/10/2020) or generated by BE and delivered to your page with ajax request.
You can't get this information by using only js. In this case you would need a server to keep track of the changes being made to a page and request this information with an http call.

convert AJAX short polling to long polling [duplicate]

I'm trying to understand more about long polling to "manipulate" a website in real time, saw some videos and I'm thinking so far:
Say I have an old date that the sql and I make an echo on it. As long polling will know if the old date will not be the same as it will look from time to time according to the setInterval function ...?
Say I want to show publication of a blog in which all text is in mysql, but repende I publish a new publication, and who is on the page at the time, you will see the publication time (not tell me?), Then how one long polling code will know the difference between the old and the new publication? Ate even not to give conflicting or repeating the same date engraved on the sql.
Since your initial question was what the difference between the two techniques is, I will start with this:
AJAX polling
Using AJAX polling to update a page will mean, that you send a request in a defined interval to the server, which would look like this:
The client sends a request to the server and the server responses immediately.
A simple example (using jQuery) would look like this:
setInterval(function(){
$('#myCurrentMoney').load('getCurrentMoney.php');
}, 30000);
The problem with this is, that this will cause a lot of useless requests since there won't be always new things on every request.
AJAX long polling
Using AJAX long polling will mean, that the client sends a request to the server and the server waits for new data to be available before he responds. This would look like this:
The client sends a request and the server responds "irregularly". As soon as the server responds, the client will send a new request to the server.
The client side would look like this:
refresh = function() {
$('#myCurrentMoney').load('getCurrentMoney.php',function(){
refresh();
});
}
$(function(){
refresh();
});
What this will do is just load the getCurrentMoney.php's output into the current money element and as soon as there is a callback, start a new request.
On the server side you usually use a loop. To solve your question how the server will know, which are new publications: either you pass the timestamp of the newest to the client available publication to the server or you use the time of the "long polling start" as indiactor:
<?
$time = time();
while ($newestPost <= $time) {
// note that this will not count as execution time on linux and you won't run into the 30 seconds timeout - if you wan't to be save you can use a for loop instead of the while
sleep(10000);
// getLatestPostTimestamp() should do a SELECT in your DB and get the timestamp of the latest post
$newestPost = getLatestPostTimestamp();
}
// output whatever you wan't to give back to the client
echo "There are new posts available";
Here we won't have "useless" requests.

How to get local date & time of browser in the file when the file itself in rendered on the server

I am trying to the browser/local date and time in a file[PDF] . My issue is that the rendering of the file/PDF happens on the server itself. So there's no way I can think of in which I can use a JavaScript, since my rendering is happening on the server side.
Any suggestions on how can I pass my current browser time directly in that file or anyother suggestions which can help?
Template: <p>Generated {{today|date:'short'}}</p>
TypeScript: today: number = new Date();
You could pass your date from the client to the server via http and print that date on the pdf, then send the pdf to the client.

Send local Javascript date to server on first access

Is there a way to read the user's date when first requesting a page to the server? I'm puzzled on how to sync server time and client time, and maybe sending the client time to the server would be a good solution.
I'm currently doing the inverse, outputting the server time on the page, so JS will read it, but I need to actually read the user's time and then process data on the server based on it.
The only way I can do it until now is loading the page then using an ajax call to send the time.
From the client side, you can execute an XmlHttpRequest to the server using jQuery:
$(function() {
var userTime = new Date();
$.post("path/to/record.php", {"userTime": userTime}, function(data) {
// do something with data, if necessary...
});
});
On the server, you can log the information however you feel fit:
<?php
if(!empty($_POST['userTime'])) {
$userTime = $_POST['userTime'];
// do something with it.
}
?>

jquery.post(): how do i honor a redirect from the server?

I'm trying my hand at unobtrusive JS, using JQuery in my Ruby On Rails app.
After the user fills out a form, the client-side JQuery code calls:
$.post("/premises", ui.form)
I can see the POST hit the server, and I can see the server emit a redirect notice to http://localhost:3000/users/42 complete with the data to be displayed.
But the browser page doesn't change. This doesn't really surprise me -- the whole point of client-side javascript is to control what gets updated -- I get that. But in this case, I'd like to honor whatever the server replies with.
I tried extending the call to post() based on How to manage a redirect request after a jQuery Ajax call:
$.post("/premises",
ui.item,
function(data, textStatus) {
if (data.redirect) {
// data.redirect contains the string URL to redirect to
window.location.href = data.redirect;
} else {
// data.form contains the HTML for the replacement form
$("#myform").replaceWith(data.form);
}
});
... but (among other problems) data.redirect is undefined. I suspect the real answer is simple, right? Looking forward to it!
The post you refer to uses JSON as return value and it is constructing that json on server side. it means if there is redirect your data object would look like
{redirect:'redirecturl.html'}
and if it is not redirect then data object would be like
{form:html-string-for-form}
now job is to construct json object accordingly on server side
The server is saying that the data you want to process with JavaScript is available at a different URL, not that the browser should load a new document into the top level frame. Sending the browser to the URL where it was told the data it was requesting with JS is wouldn't be honouring the redirect.
If you want to do that, then the server should respond with data (in the body of the response) that the JavaScript interprets as a reason to assign a new value to location.
data.redirect is probably undefined because you're not specifying it on the server side. In the answer you linked to the point was to have the server always respond with 200 regardless of the outcome, and then the JSON body it sends back determines how the client reacts. So, on the server side you'd want to respond with {"redirect" : "/where/to/go"}

Categories

Resources