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;

        if (Math.min(offset.left, (offset.left + dpWidth > viewWidth && viewWidth > dpWidth))) {
            offset.left -= Math.abs(offset.left + dpWidth - viewWidth);
            picker.attr('data-x-position','right');
        } else {
            offset.left -= 0;
            picker.attr('data-x-position','left');
        }

        if (Math.min(offset.top, (offset.top + dpHeight > viewHeight && viewHeight > dpHeight))) {
            offset.top -= Math.abs(dpHeight + inputHeight - extraY);
            picker.attr('data-y-position','top');
        } else {
            offset.top -= extraY;
            picker.attr('data-y-position','bottom');
        }

        return offset;
    }
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