Date and Time of Last Visit

This is simple script record which displays the date and time of your visitor's last visit. It will display when the user returns to your site. If it is your visitor's first visit to your site, a greeting message will be shown. You can customize the message to be displayed on your site. It will display the message, date and time of the user's last visit to the site. It's easy to implement this in your site.

Directions:

Kindly insert this script below where you want to display the Date and Time of Last Visit in your site.
  1. <script type = "text/javascript">
  2. var days = 730; // the cookie will expire = 2 years
  3. var lastvisit=new Object();
  4. var firstvisitmsg="<b>This is your first visit to this page.</b> <b>Welcome!</b>";
  5. lastvisit.subsequentvisitmsg="<p style='font-size:18px;'>Welcome back visitor!</p> <b>Your last visit was on</b> <b style='font-size:18px; color:blueviolet;'>[date_displayed]</b>";
  6.  
  7. lastvisit.getCookie=function(Name){
  8. var re=new RegExp(Name+"=[^;]+", "i");
  9. if (document.cookie.match(re))
  10. return document.cookie.match(re)[0].split("=")[1];
  11. return'';
  12. }
  13.  
  14. lastvisit.setCookie=function(name, value, days){
  15. var expireDate = new Date();
  16.  
  17. var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days));
  18. document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
  19. }
  20.  
  21. lastvisit.showmessage = function() {
  22. var wh = new Date();
  23. if (lastvisit.getCookie("visit_record") == "") {
  24. lastvisit.setCookie("visit_record", wh, days);
  25. document.write(firstvisitmsg);
  26. }
  27.  
  28. else {
  29. var lv = lastvisit.getCookie("visit_record");
  30. var lvp = Date.parse(lv);
  31. var now = new Date();
  32. now.setTime(lvp);
  33. var day = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
  34. var month = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  35. var dd = now.getDate();
  36. var dy = now.getDay();
  37. dy = day[dy];
  38. var mn = now.getMonth();
  39. mn = month[mn];
  40. yy = now.getFullYear();
  41. var hh = now.getHours();
  42. var ampm = "AM";
  43. if (hh >= 12) {ampm = "PM"}
  44. if (hh >12){hh = hh - 12};
  45. if (hh == 0) {hh = 12}
  46. if (hh < 10) {hh = "0" + hh};
  47. var mins = now.getMinutes();
  48. if (mins < 10) {mins = "0"+ mins}
  49. var secs = now.getSeconds();
  50. if (secs < 10) {secs = "0" + secs}
  51. var dispDate = dy + ", " + mn + " " + dd + ", " + yy + " " + hh + ":" + mins + ":" + secs + " " + ampm
  52. document.write(lastvisit.subsequentvisitmsg.replace("\[date_displayed\]", dispDate))
  53. }
  54.  
  55. lastvisit.setCookie("visit_record", wh, days);
  56.  
  57. }
  58.  
  59. lastvisit.showmessage();
  60. </script>
Note: You can customize the message shown, by the two variables. And, you can add CSS to the design of the text.
  1. var firstvisitmsg="<b>This is your first visit to this page.</b> <b>Welcome!</b>";
  2.  
  3. lastvisit.subsequentvisitmsg="<p style='font-size:18px;'>Welcome back visitor!</p> <b>Your last visit was on</b> <b style='font-size:18px; color:blueviolet;'>[date_displayed]</b>";

Output


The user first time to visit in this page. This is the ouput. Result
After refreshing the page, this would be the output as shown in the image below. Result
Hope that this simple work that I created may help you to your future projects. Also, for the beginners who want to learn basic of coding in JavaScript. So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much. Enjoy coding!!!

Add new comment