본문 바로가기
Javascript

toLocaleString()

by 코드뭉치 2023. 6. 14.

toLocaleString() 함수는 JavaScript에서 사용할 수 있는 내장 함수이다.

 

숫자, 날짜 시간 등을 Locale에 맞는 문자열로 변환해준다.

 

함수의 인자로는 locale 과 options 를 받을 수 있다.

 

 

1️⃣ locale

변환할 언어를 지정해줄 수 있다.

 

ex)

toLocaleString("en-US")
toLocaleString("ko-KR")
toLocaleString("ja-JP")
...

 

 

2️⃣ options

localestring의 options에 관한 정보는 여기를 참고

 

ex)

const number = 12345
const usd = aa.toLocaleString("en-US", { style: "currency", currency: "USD" })
console.log(usd) // $12,345.00
const date = new Date()
console.log(date) // Thu Jun 15 2023 13:01:54 GMT+0900 (한국 표준시)

const month1 = date.toLocaleString('en-US', {month: 'long'});
const month2 = date.toLocaleString('en-US', {month: 'numeric'});
const month3 = date.toLocaleString('en-US', {month: '2-digit'});
const month4 = date.toLocaleString('en-US', {month: 'short'});

console.log(month1) // June
console.log(month2) // 6
console.log(month3) // 06
console.log(month4) // Jun

 

 

'Javascript' 카테고리의 다른 글

프론트 변조  (0) 2023.06.14
url params  (0) 2023.06.14
[Javascript] truthy, falsy  (0) 2023.06.14
Javascript - const, let, var  (0) 2023.05.16
JavaScript - Export와 Import  (5) 2023.05.16

댓글