open:momentjs-대신-date-fns-사용하기

snippet.javascript
import React, { Component } from "react";
import { View, Text } from "react-native";
//https://stackoverflow.com/questions/45264638/react-native-moment-locale-does-not-work
import moment from "moment/min/moment-with-locales";
moment.locale("ko");
 
export default class DateText extends Component {
  render() {
    const dateText = moment(this.props.date).fromNow();
    return <Text style={{ fontSize: 12 }}>{dateText}</Text>;
  }
}
snippet.javascript
import React, { Component } from "react"
import { FlexRow } from "styles/flex"
import Icon from "components/common/Icon"
 
import ko from "date-fns/locale/ko"
import distanceInWordsToNow from "date-fns/distance_in_words_to_now"
 
interface Props {
  text: string
  icon: string
}
 
export default class DateText extends Component<Props> {
  render() {
    const { text, icon } = this.props
    var result = distanceInWordsToNow(new Date(text), {
      locale: ko,
    })
 
    return (
      <FlexRow>
        <Icon icon={icon} />
        <span style={{ margin: "2px" }}>
          {result}</span>
      </FlexRow>
    )
  }
}

  • open/momentjs-대신-date-fns-사용하기.txt
  • 마지막으로 수정됨: 2020/06/02 09:25
  • 저자 127.0.0.1