Backend 백엔드

[07] 회원가입 함수 만들기 퀴즈

박민우_ 2024. 7. 23. 01:21

 

회원가입 환영 이메일 전송 함수 실습 

function createUser({name , age ,school , email , createdAt}){
    // 1. 이메일이 정상인지 확인 ( 1 - 존재여부 , 2 - "@" 포함여부) 

        const isValid = checkEmail(email);

        if(isValid == false) return;
       
    // 2. 가입환영 템플릿 만들기
        const template =getWelcomeTemplate({name,age,school,createdAt})
    // 3. 이메일에 가입환영 템플릿 전송하기
        sendTemplate(email,template)

}

function checkEmail(email){
    if(email === undefined || email.includes("@") === false){
        console.log("에러 발생!! 이메일 주소를 제대로 입력해 주세요");
        return false;
    }
    return true;
}

function getWelcomeTemplate({name,age, school , createdAt}){
    const myTemplate = `
        <html>
            <body>
                <h1>${name}님 가입을 환영합니다 !</h1>
                <hr /> 
                <div>이름 : ${name} </div>
                <div>나이 : ${age} </div>
                <div>학교 : ${school} </div>
                <div>가입일 : ${createdAt} </div>
            </body>
        </html>
    `

    return myTemplate
}

function sendTemplate(email,template){
    console.log(`${email} 로 가입환영템플릿 ${template} 를 전송합니다`)
}

const name = "민우"
const age = 29
const school = '홍익'
const email = "a@a.com"
const createdAt = "2021-10-04"
createUser({name,age,school,email,createdAt})

 

날짜 동적으로 만드는 함수 만들어보기 

날짜를 YYYY-MM-DD 형식의 문자열로 변환

function formatDate(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
}

 

시간을 HH:MM:SS 형식의 문자열로 변환

function formatTime(date) {
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    return `${hours}:${minutes}:${seconds}`;
}

 

유튜브처럼 상대시간 표시하기 ( 1일전, 1년전 )

function getRelativeTime(timestamp: Date): string {
    const now = new Date(); // 현재 시간
    const diffInSeconds = Math.floor((now.getTime() - timestamp.getTime()) / 1000); // 현재 시간과 주어진 시간의 차이를 초 단위로 계산

    // 각 시간 단위를 초 단위로 정의
    const secondsInMinute = 60;
    const secondsInHour = 60 * secondsInMinute;
    const secondsInDay = 24 * secondsInHour;
    const secondsInWeek = 7 * secondsInDay;
    const secondsInMonth = 30 * secondsInDay; // 대략적인 값
    const secondsInYear = 365 * secondsInDay; // 대략적인 값

    // 시간 차이에 따라 적절한 형식으로 반환
    if (diffInSeconds < secondsInMinute) {
        return `${diffInSeconds}초 전`;
    } else if (diffInSeconds < secondsInHour) {
        return `${Math.floor(diffInSeconds / secondsInMinute)}분 전`;
    } else if (diffInSeconds < secondsInDay) {
        return `${Math.floor(diffInSeconds / secondsInHour)}시간 전`;
    } else if (diffInSeconds < secondsInWeek) {
        return `${Math.floor(diffInSeconds / secondsInDay)}일 전`;
    } else if (diffInSeconds < secondsInMonth) {
        return `${Math.floor(diffInSeconds / secondsInWeek)}주 전`;
    } else if (diffInSeconds < secondsInYear) {
        return `${Math.floor(diffInSeconds / secondsInMonth)}개월 전`;
    } else {
        return `${Math.floor(diffInSeconds / secondsInYear)}년 전`;
    }
}

// 예시 사용법:
const postDate = new Date('2023-07-28T12:00:00Z');
console.log(getRelativeTime(postDate)); // 출력 예시: "1일 전" 또는 "1년 전" 등

 

 

 

728x90