Javascript

ISO 8601 국제표준시간(2016-10-27T17:13:40Z) 날짜만 나오게 자르기

selonjulie 2022. 12. 19. 23:36

위키백과 | ISO 8601

 

ISO란?

  • ISO 8601 은 날짜와 시간과 관련된 데이터 교환을 다루는 국제 표준
  •  그레고리력 (proleptic Gregorian도 가능)에서의 날짜와 (부가적으로 시간대 정보를 포함하는) 24시간제에 기반하는 시간, 시간 간격(time interval) 그리고 그들의 조합에 대한 표현과 형식에 적용됨
  • 조합된 UTC 날짜 및 시간: 2016-10-27T17:13:40Z 
  • 날짜: 2016-10-27

 

const date = new Date();
console.log(date.toISOString());//2022-12-19T14:24:36.447Z

 

문제

위와같이 나오는 형태를 날짜만 제외하고 T부터 없애기 위해서 substring과 indexOf사용

 

Stack overflow | How to output date in javascript in ISO 8601 without milliseconds and with Z

MDN | String.prototype.substring()

substring() 메소드는 string 객체의 시작 인덱스로 부터 종료 인덱스 전 까지 문자열의 부분 문자열을 반환합니다.
const str = 'Mozilla';

console.log(str.substring(1, 3));
// expected output: "oz"

console.log(str.substring(2));
// expected output: "zilla"

 

MDN | String.prototype.indexOf()

 

indexOf() 메서드는 호출한 String 객체에서 주어진 값과 일치하는 첫 번째 인덱스를 반환합니다. 일치하는 값이 없으면 -1을 반환합니다.
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf(searchTerm);

console.log(`The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}`);
// expected output: "The index of the first "dog" from the beginning is 40"

console.log(`The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}`);
// expected output: "The index of the 2nd "dog" is 52"

 

 

구현코드

//MUI

<Typography variant={contentStyle} color={contentColor}>
 {(title === '생성일' || title === '수정일') && content.substring(0, content.indexOf('T'))}
</Typography>

 

 

 

'Javascript' 카테고리의 다른 글

some과 every의 차이  (0) 2023.03.01
함수형 프로그래밍  (0) 2023.01.03
Redux는 무엇인가 (코딩애플, 생활코딩, Udemy 노트필기)  (0) 2022.12.06
class  (2) 2022.11.28
생성자 함수와 new 연산자  (2) 2022.11.26