//날짜 형식 유효성 검사

function isDatetime(d)

{

var re = /^(19|20)\d{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[0-1]) (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/;

return re.test(d);

}

"yyyy-mm-dd HH:mm:ss"

var Dt= new Date(); //현재 날짜 및 시간

 

//현재시간 기준 계산

alert(new Date(Date.parse(Dt) - 30 * 1000 * 60 * 60 * 24)); //30일전
alert(new Date(Date.parse(Dt) - 15 * 1000 * 60 * 60 * 24)); //보름전
alert(new Date(Date.parse(Dt) - 7 * 1000 * 60 * 60 * 24)); //일주일전
alert(new Date(Date.parse(Dt) - 1 * 1000 * 60 * 60 * 24)); //하루전
alert(new Date(Date.parse(Dt) + 1 * 1000 * 60 * 60 * 24)); //하루후
alert(new Date(Date.parse(Dt) + 7 * 1000 * 60 * 60 * 24)); //일주일후
alert(new Date(Date.parse(Dt) + 15 * 1000 * 60 * 60 * 24)); //보름후
alert(new Date(Date.parse(Dt) + 30 * 1000 * 60 * 60 * 24)); //한달후

 

alert(new Date(Date.parse(Dt) + 1000 * 60 * 60)); //한시간후
alert(new Date(Date.parse(Dt) + 1000 * 60)); //1분후
alert(new Date(Date.parse(Dt) + 1000)); //1초후

 

 

//응용

alert(new Date(Date.parse(Dt) + (15000*50) + 1000*60*65))); //15초씩 50번 지난 이후 한시간 5분후

 

 

//Date 개체를 입력받아 yyyy-MM-dd hh:mm:ss 형식으로 반환

function timeSt(dt) {
    var d = new Date(dt);
    var yyyy = d.getFullYear();
    var MM = d.getMonth()+1;
    var dd = d.getDate();
    var hh = d.getHours();
    var mm = d.getMinutes();
    var ss = d.getSeconds();

    return (yyyy + '-' + addzero(MM) + '-' + addzero(dd) + ' ' + addzero(hh) + ':' + addzero(mm) + ':' + addzero(ss));
}

 

//10보다 작으면 앞에 0을 붙임

function addzero(n) {
    return n < 10 ? "0" + n : n;
}

 

alert(timeSt(new Date()));

 

'JavaScript > 날짜' 카테고리의 다른 글

javascript 날짜 형식 유효성 검사  (0) 2022.11.15

+ Recent posts