JS中Date与字符串的转换

string to date
/**
 * parse date string YYYY-MM-dd to date
 */
function parseDate(datestr) {
    var parts = datestr.split('-');
    return new Date(parts[0], parts[1] - 1, parts[2])
}


/**
 * format date to YYYY-MM-dd mm:ss format string
 */
function formatDate(d) {
    return d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
}


/**
 * n = 1 means tomorrow n = -1 means yestoday
 */
function rollDay(d, n) {
    return new Date(d - 0 + n * 86400000);
}

你可能感兴趣的:(JavaScript)