JavaScriptは現在の日付時間を取得します.

8823 ワード

現在の日付時間を取得
//         
var myDate = new Date();

myDate.toLocaleDateString();                //      
var mytime=myDate.toLocaleTimeString();     //      
myDate.toLocaleString( );                   //       

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)

//         
function getDatetime() {
    var now = new Date();
    var year = now.getFullYear();       
    var month = now.getMonth() + 1;     
    var day = now.getDate();            
    var hh = now.getHours();            
    var mm = now.getMinutes();          
    var ss = now.getSeconds();          
    var clock = year + "-";
    if (month < 10)
        clock += "0";
    clock += month + "-";
    if (day < 10)
        clock += "0";
    clock += day + " ";
    if (hh < 10)
        clock += "0";
    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm + ":";
    if (ss < 10) clock += '0';
    clock += ss;
    return clock;
}

//         
function timestampToTime(timestamp) {
    var date = new Date(timestamp);
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() + ' ';
    var hh = date.getHours() < 10 ? '0' + date.getHours() : date.getHours() + ':';
    var mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() + ':';
    var ss = date.getSeconds() < 10 ? '0' + date.getDate() : date.getSeconds() ;
    return Y + M + D + hh + mm + ss;
}