Last active
December 2, 2016 04:52
-
-
Save rhostem/9c500f323fda1e79634cde97d1ea32cf to your computer and use it in GitHub Desktop.
getLocaleTimeString.js - 지역 시간대와 형식에 맞는 문자열을 반환
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* dateString을 이용해서 지역 시간대와 형식에 맞는 문자열을 반환 | |
* ex) "2016년 5월 12일 오후 7:50:01" | |
* | |
* @param {[String]} dateString | |
* @param {[Number]} timezoneOffset [ -12 ~ 12] | |
*/ | |
export const getLocaleTimeString = (dateString, timezoneOffset) => { | |
if (!dateString) { | |
return ''; | |
} | |
let time = null; | |
// 타임존을 적용할 필요가 있을 때 사용 | |
if (timezoneOffset) { | |
const defaultTime = new Date(dateString).getTime(); | |
time = new Date(defaultTime + (timezoneOffset * 60 * 60 * 1000)); | |
} else { | |
time = new Date(dateString); | |
} | |
return time.toLocaleTimeString('ko-KR', { | |
year: 'numeric', | |
month: 'narrow', | |
day: 'numeric', | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment