import time


def json_handle_datetime(obj):
    """Convert a datetime obj to a number of milliseconds since epoch.

    This uses milliseconds since this is probably going to be used to
    feed JS, and JS timestamps use milliseconds.

    """
    try:
        return time.mktime(obj.timetuple()) * 1000
    except AttributeError:
        return obj