I am using react native with dayjs.
I want to compare the time when the comment was written with the current time, and then record the compared time on the console.log(expectedtime) by using dayjs
Like this example
function displayedAt(createdAt) {
const milliSeconds = new Date() - createdAt
const seconds = milliSeconds / 1000
if (seconds < 60) return `Just before`
const minutes = seconds / 60
if (minutes < 60) return `${Math.floor(minutes)}minutes ago`
const hours = minutes / 60
if (hours < 24) return `${Math.floor(hours)} hour before`
const days = hours / 24
if (days < 7) return `${Math.floor(days)}days ago`
const weeks = days / 7
if (weeks < 5) return `${Math.floor(weeks)}weeks ago`
const months = days / 30
if (months < 12) return `${Math.floor(months)}months ago`
const years = days / 365
return `${Math.floor(years)}year ago`
}
But i want to get time using dayjs
this is my code
import dayjs from 'dayjs'
const TodoItem = ({item }) => {
const date = new Date()
const day = dayjs(date).format('YYYY-MM-DD HH:mm:ss');
console.log("day:",day);
// day: 2021-04-01 17:46:49
console.log("item:",item);
// item:{
// PostId: 2
// ReplyComments: []
// User: {id: 5, nickname: "일론머스크"}
// UserId: 5
// content: "ㅋ"
// createdAt: "2021-03-29T13:36:35.000Z"
// id: 273
// updatedAt: "2021-03-29T13:36:35.000Z"
// }
console.log(expectedtime)
how can i fix and add my code?
You can use .fromNow() method of daysjs.
const day = dayjs(date).fromNow();