Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <h4>The closest time is: </h4>
  <span id="time"></span>
  <script>
    var timeSrc = ["10:00 PM", "08:00 AM", "11:05 AM", "12:00 AM", "01:00 AM", "12:00 PM", 
     "03:00 AM", "07:00 AM", "06:00 PM"];
    var curDate = new Date();
    curDate = curDate.toDateString();
    var times = timeSrc.map((t) => {
      return new Date(curDate + " " + t); // Make the time as a datetime with current date.
    });
    var now = new Date();
    var min = Math.abs(now - times[0]);
    var result = '';
    //Get the difference of each time with current time. The minimum difference is the closest.
    for(let i = 1; i < times.length; i++) {
      if (Math.abs(now - times[i]) <= min) {
          min = Math.abs(now - times[i]);
          result = timeSrc[i];
       }
    }
    console.log(result);
    document.getElementById("time").innerHTML = result;
    
  </script>
</body>
</html>
 
var timeSrc = ["10:00 PM", "08:00 AM", "11:05 AM", "12:00 AM", "01:00 AM", "12:00 PM", 
     "03:00 AM", "07:00 AM", "06:00 PM"];
    var curDate = new Date();
    curDate = curDate.toDateString();
    var times = timeSrc.map((t) => {
      return new Date(curDate + " " + t); // Make the time as a datetime with current date.
    });
    var now = new Date();
    var min = Math.abs(now - times[0]);
    var result = '';
    //Get the difference of each time with current time. The minimum difference is the closest.
    for(let i = 1; i < times.length; i++) {
      if (Math.abs(now - times[i]) <= min) {
          min = Math.abs(now - times[i]);
          result = timeSrc[i];
       }
    }
    console.log(result);
    document.getElementById("time").innerHTML = result;
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers