Advertisement
metalx1000

Javascript time between two days

Oct 2nd, 2017
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <title>Until Vacation TIME!!!</title>
  5.     <meta charset="utf-8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1">
  7.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" crossorigin="anonymous">
  8.     <!--https://github.com/datejs/Datejs-->
  9.     <script src="https://cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js"></script>
  10.     <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" crossorigin="anonymous"></script>
  11.     <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" crossorigin="anonymous"></script>
  12.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" crossorigin="anonymous"></script>
  13.     <script>
  14.       var today = new Date().toString('M/d/yyyy');
  15.       var day = "11/16/2017";
  16.       $(document).ready(function(){
  17.         //get time between dates;
  18.         var b = dateDiff(today,day,"d");
  19.         $("#main").html("There are " + b + " days until " + day);
  20.       });
  21.  
  22.       function toDate(s) {
  23.         var s = s.split('/');
  24.         return new Date(s[2], --s[0], s[1]);
  25.       }
  26.  
  27.       function dateDiff(from,to,format) {
  28.         var milliseconds = toDate(to) - toDate(from);
  29.         var days = milliseconds / 86400000;
  30.         var hours = milliseconds / 3600000;
  31.         var weeks = milliseconds / 604800000;
  32.         var months = milliseconds / 2628000000;
  33.         var years = milliseconds / 31557600000;
  34.         if (format == "h") {
  35.           return Math.round(hours);
  36.         }
  37.         if (format == "d") {
  38.           return Math.round(days);
  39.         }
  40.         if (format == "w") {
  41.           return Math.round(weeks);
  42.         }
  43.         if (format == "m") {
  44.           return Math.round(months);
  45.         }
  46.         if (format == "y") {
  47.           return Math.round(years);
  48.         }
  49.       }
  50.     </script>
  51.   </head>
  52.   <body>
  53.  
  54.     <div id="main" class="container">
  55.  
  56.     </div>
  57.  
  58.   </body>
  59. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement