code: function getOffset(picker, input) {
          var extraY = 0;
          var dpWidth = picker.outerWidth();
          var dpHeight = picker.outerHeight();
          var inputHeight = input.outerHeight();
          var doc = picker[0].ownerDocument;
          var docElem = doc.documentElement;
          var viewWidth = docElem.clientWidth + $(doc).scrollLeft();
          var viewHeight = docElem.clientHeight + $(doc).scrollTop();
          var offset = input.offset();
          offset.top += inputHeight;
  
          offset.left -=
              Math.min(offset.left, (offset.left + dpWidth > viewWidth && viewWidth > dpWidth) ?
              Math.abs(offset.left + dpWidth - viewWidth) : 0);
  
          offset.top -=
              Math.min(offset.top, ((offset.top + dpHeight > viewHeight && viewHeight > dpHeight) ?
              Math.abs(dpHeight + inputHeight - extraY) : extraY));
  
          return offset;
      }
docs: checkOffset - get the offset below/above and left/right element depending on screen position
  Thanks https://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.datepicker.js