본문 바로가기
프론트엔드/Javascript

[javascript] 현재 날짜 시간 구하기 date format ( yyyy-MM-dd, yyyy-MM-dd hh:mm:ss )

by 작은소행성 2021. 9. 9.

 

//yyyy-MM-dd
function convertDate(datetime){
    var date = new Date(datetime);
    var year = date.getFullYear();
    var month = date.getMonth()+1;
    month = month >= 10 ? month : '0' + month;
    var day = date.getDate();
    day = day >= 10 ? day : '0' + day;

    return [year,month,day].join('-');
}

 

 

 

//yyyy-MM-dd HH:mm:ss
function convertDateTime(datetime){
    var date = new Date(datetime);
    var year = date.getFullYear();
    var month = date.getMonth()+1;
    month = month >= 10 ? month : '0' + month;
    var day = date.getDate();
    day = day >= 10 ? day : '0' + day;

    var hour = date.getHours();
    hour = hour < 10 ? '0' + hour.toString() : hour.toString();

    var minites = date.getMinutes();
    minites = minites < 10 ? '0' + minites.toString() : minites.toString();

    var seconds = date.getSeconds();
    seconds = seconds < 10 ? '0' + seconds.toString() : seconds.toString();


    return [year,month,day].join('-') +" "+ hour+":"+minites+":"+seconds;
}

 

 

 

 

반응형