I am trying to get the time a user goes on a page and save it as a cookie (in JavaScript) I'm able to get the current time but I can't seem to be able to save it as a cookie.
The reason why I'm doing this is because I'd like to know how long it took my class to go from the first page to the last page of my training site. I collect the first time stamp when they load the first page, and I collect the second time stamp when they load the finale page (their are 5 pages).
I'm not sure if there is a better way of doing this with Javascript?
Here is my code: (thanks in advance)
var d = new Date();
document.getElementById("test").innerHTML = d;
docCookies.setItem("time", "test");
<p id="test"></p>
Use following code. This will add your current date in cookie.
var d = new Date();
document.cookie="time="+d;
Try this :-
document.cookie = "name=John Doe; expires=Tue, 01 Aug 2017 12:00:00 UTC";
You can also go for Local Storage to store temporary data
localStorage.setItem("name", "John Doe");
Happy Coding...
Related
Is there a way to set an expiry date on tracking tags that run on your website, specifically when using google tag manager?
There isn't a built in way to do this, but you can set it up without too much trouble. First set up a new User-Defined Variable, use the type "Custom Javascript" , and call it something helpful like "Date". Put the following script in as the value:
function () {
var d = new Date();
var y = d.getFullYear();
var m = (d.getMonth() + 1 + '').padStart(2,0);
var d = (d.getDate() + '').padStart(2,0);
return y + m + d;
}
That will give you a variable which returns the date in a format like YYYYMMDD. So, for example, on February 14, 2007, it would return 20070214.
Save that variable, and in the trigger that fires your tag, set a condition like [name of your variable] less than [desired date of expiration]. Along the lines of :
Date less than 20200603
With that, the tag will only fire if the date is before June 3, 2020.
You can use this same technique to only fire tags _after _ a certain date as well.
In Google Tag Manager you can set a firing schedule for a tag under "Advanced Settings".
The limitation here is that you cannot use variables for dates (if you feel that actually somehow limits here).
I am trying to get current Standard IST time but I am getting system time only using below function.
var currentTime = new Date();
var currentOffset = currentTime.getTimezoneOffset();
var ISTOffset = 330;
var d1 = new Date(currentTime.getTime() + (ISTOffset + currentOffset)*60000);
Is there any api or library that helps me in getting standard IST time.?
You could use moment.js, a standard utility to handle all time and date related functions. You simple import moment.js and for your desired use case implement a method such as
moment().utcOffset("+05:30").format()
for setting the the time in IST which is +05:30 from GMT.
Java script currently doesn't helps to get current time, I have finally used the timestamp in my response headers in service calls
In html file I need to display when that file was saved (time and date)
How can i do it? JavaScript?
<script>
var d = new Date(0);
document.getElementById("demo").innerHTML = d;
</script>
I need to know the the date when it has last been saved at the file system! And how can i change this 11-9-2014 at 23:7:55 to smth normal September 11 at 23:07?
document.lastModified will give you the time the document was last modified so far as the browser is able to determine it. You won't find anything closer unless you implement some method to embed the time stamp into the file when you save it.
Is this what you are looking for?
document.getElementById("demo").innerHTML = document.lastModified;
Demo here: http://jsfiddle.net/donal/w58v89Lh/
I have a code that runs to select a date from calender, but I want to set the current_date to that of the server in the mentioned calender. And with a functionality that compares client_date with server_date.
That's possible. As suggested elsewhere, you should convert the dates to strings for two reasons:
Easier to read/understand when you search for a bug
The "current time" is always different between client and server
To send the current date to the client, you can use a hidden form field or an AJAX request or a web service or REST or JSON or JSONP or ... well, there are too many methods to do it.
To compare dates, convert the string to a Date object and then use the usual methods to compare them (compareTo() in Java and <,==,> in JavaScript).
You can do it by using the following way:
In Java
public class YourJavaClass{
public static String getServerDate()
{
Calendar cal = Calendar.getInstance();
return cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DATE);
}
}
In jsp
$('input.datepicker').datepicker(
{
minDate : new Date(<%out.print(YourJavaClass.getServerDate());%>)
});
The easiest way is to compare both dates with conversation to milliseconds. Both languages provides methods for it.
Javascript:
<script type="">
var myDate = new Date();
var milliseconds = myDate.getTime();
</script>
Java (JSP):
<script type="text/javascript">
<%!
Date myJavaDate = new Date();
long myJavaMilliseconds = 0L;
myJavaMilliseconds = myJavaDate.getTime();
%>
var myJavaMillisondsInJs = <%= myJavaMilliseconds %>;
</script>
/* Put code here to compare between those dates */
If you want to compare the dateas server side you have to submit the javatime to the server via Ajax or a form.
One way of doing it is to pass the current date from your server script to the template, so when the pages renders, javascript can access it and make comparison. This has obvious issue - if the user will leave the page open till next day, the date will be out of date.
Other way is to use asynchronous call to your back-end and obtain the date from there.
I want to use client system date on page load event,
How can I do that.
Since this is not a post back so please let me know how to do it.
Thanks
var a = "&date=" + (new Date().replace(/\s+/g, ""));
Just append the value of a to all your links back to the server. On the serverside you will see the datetime come across in the HTTP request.
var currentDate = new Date()
The currentDate object will have the methods:
getMonth()
getDate()
getFullYear()
This should give you what you need.
Edit:
Since you mentioned time in the title of the post, it's probably worth mentioning that currentDate will also have:
getHours()
getMinutes()
should you want to display the current time as well.
Edit 2:
To tie into page loading
<script type="text/javascript">
function buildDate() {
var date = new Date();
/*build date string here*/
}
</script>
<body onload="buildDate()">
Other stuff here...
</body>