frontend/react
[javascript] moment 시간 더하기 add(), utc 시간을 kst시간으로 변환하기
meroriiDev
2021. 12. 13. 17:53
728x90
반응형
add 메소드
Key | Shorthand |
years | y |
quarters | Q |
months | M |
weeks | w |
days | d |
hours | h |
minutes | m |
seconds | s |
milliseconds | ms |
moment().add(7, 'days').add(1, 'months'); // with chaining
moment().add({days:7,months:1}); // with object literal
moment().add(1000000, 'milliseconds'); // a million milliseconds
moment().add(360, 'days'); // 360 days
moment([2021, 0, 31]); // January 31
moment([2021, 0, 31]).add(1, 'months'); // February 28
moment([2021, 1, 28]); // February 28
moment([2021, 1, 28]).add(1, 'month'); // March 28
var duration = moment.duration({'days' : 1});
moment([2012, 0, 31]).add(duration); // February 1
utc 시간을 kst시간으로 변환하기
: utc 시간에 9시간 더해주기!
시간을 계산해서 더해주는 방식
export const utcToKst = (value: string, format: string) => {
const utcDateValue = moment(value, format).valueOf();
const localDateValue = utcDateValue + 9 * 60 * 60 * 1000;
return moment(localDateValue).format(format);
}
add를 활용한 방식
export const utcToKst = (value: string, format: string) => {
const dateValue = moment(value, format).add(9, 'h').format(format);
return dateValue;
}
https://momentjscom.readthedocs.io/en/latest/moment/03-manipulating/01-add/
728x90
반응형