JavaScript Date 對象方法總結

相似文章推薦:數組

JavaScript String 對象方法總結bash

JavaScript Array 對象方法總結函數

JavaScript Math 對象方法總結post

經常使用 Date 類方法

var myDate = new Date();

myDate.getYear();  // 獲取當前年份(2位)
myDate.getFullYear(); // 獲取完整的年份(4位,1970-????)
myDate.getMonth(); // 獲取當前月份(0-11,0表明1月)
myDate.getDate(); // 獲取當前日(1-31)
myDate.getDay(); // 獲取當前星期X(0-6,0表明星期天)
myDate.getTime(); // 獲取當前時間(從1970.1.1開始的毫秒數)
myDate.getHours(); // 獲取當前小時數(0-23)
myDate.getMinutes(); // 獲取當前分鐘數(0-59)
myDate.getSeconds(); // 獲取當前秒數(0-59)
myDate.getMilliseconds(); // 獲取當前毫秒數(0-999)
myDate.toLocaleDateString(); // 獲取當前日期
myDate.toLocaleTimeString(); // 獲取當前時間
myDate.toLocaleString( ); // 獲取日期與時間
複製代碼

獲取當月天數

/*獲取一個月的天數 */
function getCountDays() {
    var curDate = new Date();
    /* 獲取當前月份 */
    var curMonth = curDate.getMonth();
    /*  生成實際的月份: 因爲curMonth會比實際月份小1, 故需加1 */
    curDate.setMonth(curMonth + 1);
    /* 將日期設置爲0, 這裏爲何要這樣設置, 我不知道緣由, 這是從網上學來的 */
    curDate.setDate(0);
    /* 返回當月的天數 */

    return curDate.getDate();
}

var day = getCountDays();
複製代碼

經過數組來添加天數

/*緊接上一步*/
function getEvryDay() {
    var dayArry=[];
    for (var i = 1; i <= day; i++) {
        dayArry.push(i);
    }
    return dayArry; // dayArry[] 中顯示每一天的數字
};
複製代碼

獲取當前時間

function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
}
複製代碼

日期大小比較

function getCfompareDate(d1, d2) { 
    if (d1 === '' || d2 === '') {
        return false;
    }
    var _beginDate = new Date(d1.replace(/\-/g, "\/"));  
    var _endDate=new Date(d2.replace(/\-/g, "\/"));  

    if (d1 >= d2) {  
        alert("開始時間不能大於結束時間!");  
        return false;  
    }  
}

getCfompareDate('2019-01-01', '2019-01-09');
複製代碼

js時間戳與日期格式的相互轉換

  1. 將時間戳轉換成日期格式:
function timestampToTime(timestamp) {
    if (!timestamp) {
        return '-';
    }
    let date = new Date(timestamp);
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());

    return Y + M + D + h + m + s;
}
timestampToTime(1403058804);
console.log(timestampToTime(1403058804)); // 2014-06-18 10:33:24
複製代碼

注意:若是是Unix時間戳記得乘以1000。好比:PHP函數time()得到的時間戳就要乘以1000ui

  1. 將日期格式轉換成時間戳:
function timeToTimestamp(time) {
    var _date = new Date(time);
    // 有三種方式獲取
    var time1 = _date.getTime();
    var time2 = _date.valueOf();
    var time3 = Date.parse(_date);

    return time1;
}

timeToTimestamp('2014-04-23 18:55:49:123');
複製代碼
相關文章
相關標籤/搜索