μƒˆμ†Œμ‹

FrontEnd/JavaScript

[JavaScript] toLocaleDateString

  • -

🐣 개인 ν”„λ‘œμ νŠΈλ₯Ό μ§„ν–‰ν•˜λ‹€ 마주친 μ‹œκ°„


개인 ν”„λ‘œμ νŠΈλ₯Ό λ¦¬νŒ©ν† λ§μ„ μ§„ν–‰ν•˜λ©΄μ„œ κ²Œμ‹œκΈ€ λ‚ μ§œμ— λŒ€ν•΄μ„œ μ‚΄νŽ΄λ³΄κ²Œ λ˜μ—ˆλ‹€. λ‹¨μˆœνžˆ μž‘μ„±μΌ 2024λ…„ 00μ›” 00일둜 λ‚˜νƒ€λ‚Ό 것이 μ•„λ‹ˆλΌ, κΈ€ μž‘μ„± μ‹œκ°„ κΈ°μ€€μœΌλ‘œ 1λΆ„ μ „ / 1μ‹œκ°„ μ „ / 24μ‹œκ°„ μ „ / 1일 μ „ / 2024λ…„ 00μ›” 00일 순으둜 ν‘œμ‹œλ₯Ό ν•˜κ³  μ‹Άμ—ˆλ‹€. μ•„λ¬΄νŠΌ μ΄λŸ¬ν•œ κΈ°λŠ₯을 μœ„ν•΄μ„œ μ‚¬μš©ν•œ μ½”λ“œ toLocaleDateString에 λŒ€ν•΄μ„œ 정리해보도둝 ν•˜κ² λ‹€.

 

 

🐣 toLocaleDateString


1. κ°œλ…

Date 객체λ₯Ό μ§€μ •λœ λ‘œμΊ˜μ— 따라 ν˜•μ‹ν™”λœ λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€. 이 λ©”μ„œλ“œλŠ” μ§€μ—­ν™”λœ λ‚ μ§œ ν˜•μ‹μ— 따라 λ…„, μ›”, 일을 λ°˜ν™˜
  • locales(선택 사항): 이 맀개 λ³€μˆ˜λŠ” 둜캘 λ˜λŠ” 둜캘 배열을 μ§€μ •. μ‚¬μš©ν•  μ–Έμ–΄ 및 μ§€μ—­ ν˜•μ‹μ„ κ²°μ •. λ‚΄ 경우 'ko-KR'둜 μ„€μ •ν•˜μ—¬ ν•œκ΅­μ–΄μ™€ ν•œκ΅­ 지역을 μ§€μ • κ°€λŠ₯.
  • options(선택 사항): 이 λ§€κ°œλ³€μˆ˜λŠ” λ‚ μ§œ ν˜•μ‹μ„ μ§€μ •ν•˜λŠ” 속성을 ν¬ν•¨ν•˜λŠ” 개체. 이미 getDate ν•¨μˆ˜μ— options 개체λ₯Ό μ •μ˜ν–ˆλ‹Ή.

2. μ½”λ“œ

μ•„μ—μ„œ optionκ°μ²΄λŠ” λ‚ μ§œλ₯Ό μ–΄λ–»κ²Œ ν˜•μ‹ν™”ν• μ§€λ₯Ό μ§€μ •ν•˜λŠ”λ°, 'numeric', 'long', 'shor' λ“±μ˜ μ˜΅μ…˜μ„ μ‚¬μš©ν•  수 μžˆλ‹€. 이λ₯Ό 톡해 μ›ν•˜λŠ” ν˜•μ‹μœΌλ‘œ λ‚ μ§œλ₯Ό ν‘œμ‹œ κ°€λŠ₯ν•˜λ‹€.

const postDate = new Date();  // μ˜ˆμ‹œλ‘œ ν˜„μž¬ λ‚ μ§œλ₯Ό μ‚¬μš©
const options = { year: 'numeric', month: 'long', day: 'numeric' };
const formattedDate = postDate.toLocaleDateString(undefined, options);

console.log(formattedDate);


// μ•„λž˜μ™€ 같이 μ˜΅μ…˜ κ°€λŠ₯
const options: DateTimeFormatOptions = {
  year: 'numeric',
  month: 'long',
  day: 'numeric',
};

 

  1. 연도: 'numeric'
    • 이 μ˜΅μ…˜μ€ 연도 ν‘œμ‹œ 방법을 μ§€μ •
    • ``숫자''둜 μ„€μ •ν•˜λ©΄ μ—°λ„μ˜ 전체 숫자 ν‘œν˜„μ΄ ν‘œμ‹œ(예: 2024).
  2. μ›”: 'long'
    • 이 μ˜΅μ…˜μ€ μ›” ν‘œμ‹œ 방법을 μ§€μ •
    • ``long''으둜 μ„€μ •ν•˜λ©΄ ν•΄λ‹Ή μ›”μ˜ 전체 이름(예: 1μ›”)이 ν‘œμ‹œ
  3. μš”μΌ: 'numeric'
    • 이 μ˜΅μ…˜μ€ λ‚ μ§œ ν‘œμ‹œ 방법을 μ§€μ •
    • ``숫자''둜 μ„€μ •ν•˜λ©΄ λ‚ μ§œμ˜ 전체 숫자 ν‘œν˜„(예: 31)이 ν‘œμ‹œ

λ”°λΌμ„œ μ΄λŸ¬ν•œ μ˜΅μ…˜μ„ 연도와 일에 λŒ€ν•΄ μˆ«μž''둜, 월에 λŒ€ν•΄ 'long'으둜 μ„€μ •ν•˜λ©΄ κ²°κ³Ό λ‚ μ§œ ν˜•μ‹μ€ ν•œκ΅­μ–΄λ‘œ "2024λ…„ 1μ›” 31일"κ³Ό κ°™κ²Œ λ˜λŠ”λ°, "2024"λŠ” 연도, 1"μ›”"은 μ›”, "일"은 일을 μ˜λ―Έν•¨.

 

🐣 μ΄ν•΄ν–ˆλ‹ˆ? quiz λ‹€!


console.log(new Date());
console.log(new Date().toString());
console.log(result[10].date);
console.log(new Date(result[10].date));
console.log(new Date(result[10].date).getTime());

 

정닡은 μ•„λž˜μ—!

 

🐣 νƒ€μž… μ§€μ •?


  • typescriptλ₯Ό μ΄μš©ν•˜μ—¬ 개발 쀑이기 λ•Œλ¬Έμ— ν•΄λ‹Ή νƒ€μž…μ„ λ§žμΆ°μ€˜μ•Ό ν•œλ‹€. 사싀 κ³„μ†λœ νƒ€μ˜€λ₯˜λ₯Ό λ§ˆμ£Όμ³€μ—ˆμŒπŸ£
  • typescriptμ—μ„œ toLocaleDateString μ˜΅μ…˜ 객체의 경우 λ‹€μŒκ³Ό 같은 ν˜•μ‹μ„ κ°€μ Έμ•Ό 함
interface DateTimeFormatOptions {
  localeMatcher?: 'lookup' | 'best fit';
  weekday?: 'long' | 'short' | 'narrow';
  era?: 'long' | 'short' | 'narrow';
  year?: 'numeric' | '2-digit';
  month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow';
  day?: 'numeric' | '2-digit';
  hour?: 'numeric' | '2-digit';
  minute?: 'numeric' | '2-digit';
  second?: 'numeric' | '2-digit';
  timeZoneName?: 'long' | 'short';
  formatMatcher?: 'basic' | 'best fit';
  hour12?: boolean;
  timeZone?: string;
}

const options: DateTimeFormatOptions = { year: 'numeric', month: 'long', day: 'numeric' };

 

🐣 μ½”λ“œ λ‚˜μ—΄~


  1. μ½”λ“œ λ‚˜μ—΄(뢄리 이전)

μ•„λž˜μ™€ 사진과 같이 (λ°©κΈˆμ „/1λΆ„μ „/1μ‹œκ°„μ „/1일전/2024~)순으둜 ν‘œμ‹œ 됨

const PostList: React.FC<PostListProps> = ({ result }) => {
  //μ‹œκ°„
  const getTimesAgo = (postDate: string) => {
    const postDateTime = new Date(postDate);
    const currentDateTime = new Date();

    const timeDifference = currentDateTime.getTime() - postDateTime.getTime();
    const minutesAgo = Math.floor(timeDifference / (1000 * 60));
    const hoursAgo = Math.floor(timeDifference / (1000 * 60 * 60));
    const daysAgo = Math.floor(timeDifference / (1000 * 60 * 60 * 24));

    if (minutesAgo < 1) {
      return '방금 μ „';
    }
    if (minutesAgo < 60) {
      return `${minutesAgo}λΆ„ μ „`;
    }
    if (hoursAgo < 24) {
      return `${hoursAgo}μ‹œκ°„ μ „`;
    }
    if (daysAgo < 30) {
      return `${daysAgo}일 μ „`;
    }
    if (daysAgo >= 365) {
      const options: DateTimeFormatOptions = {
        month: 'long',
        day: 'numeric',
      };
      return postDateTime.toLocaleDateString(undefined, options);
    }
    interface DateTimeFormatOptions {
      localeMatcher?: 'lookup' | 'best fit';
      weekday?: 'long' | 'short' | 'narrow';
      era?: 'long' | 'short' | 'narrow';
      year?: 'numeric' | '2-digit';
      month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow';
      day?: 'numeric' | '2-digit';
      hour?: 'numeric' | '2-digit';
      minute?: 'numeric' | '2-digit';
      second?: 'numeric' | '2-digit';
      timeZoneName?: 'long' | 'short';
      formatMatcher?: 'basic' | 'best fit';
      hour12?: boolean;
      timeZone?: string;
    }

    const options2: DateTimeFormatOptions = {
      year: 'numeric',
      month: 'long',
      day: 'numeric',
    };
    return postDateTime.toLocaleDateString(undefined, options2);
  };

 

μ›ν•˜λŠ” λŒ€λ‘œ μ‹œκ°„μ΄ 슝슝! μ™„λ£Œ!
Contents

ν¬μŠ€νŒ… μ£Όμ†Œλ₯Ό λ³΅μ‚¬ν–ˆμŠ΅λ‹ˆλ‹€

이 글이 도움이 λ˜μ—ˆλ‹€λ©΄ 곡감 λΆ€νƒλ“œλ¦½λ‹ˆλ‹€.