[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',
};
- μ°λ: 'numeric'
- μ΄ μ΅μ μ μ°λ νμ λ°©λ²μ μ§μ
- ``μ«μ''λ‘ μ€μ νλ©΄ μ°λμ μ 체 μ«μ ννμ΄ νμ(μ: 2024).
- μ: 'long'
- μ΄ μ΅μ μ μ νμ λ°©λ²μ μ§μ
- ``long''μΌλ‘ μ€μ νλ©΄ ν΄λΉ μμ μ 체 μ΄λ¦(μ: 1μ)μ΄ νμ
- μμΌ: '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);
};
μνλ λλ‘ μκ°μ΄ μμ! μλ£!
'FrontEnd > JavaScript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[JavaScript] ASCII μ½λ - charCodeAt() μ fromCharCode() (0) | 2024.06.14 |
---|---|
[JavaScript] λ°λ³΅λ¬Έ for...of κ·Έλ¦¬κ³ for...in (0) | 2024.06.14 |
[JavaScript] TDZ(Temporal Dead Zone) : λ³μμ μκ° μ¬ν~ (0) | 2024.01.06 |
[JavaScript] var, let, const μ°¨μ΄μ (0) | 2024.01.06 |
[JavaScript] λ³μμ μ μΈ, μ΄κΈ°ν, ν λΉ (0) | 2024.01.05 |
μμ€ν κ³΅κ° κ°μ¬ν©λλ€