如何更改我在Instagram上写评论的时间?使用dayjs进行reactnative



我使用的是带有dayjs的react native。

我想将写评论的时间与当前时间进行比较,然后使用dayjs 将比较的时间记录在console.log上(预期时间(

像这个例子

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`
}

但我想用dayjs 获得时间

这是我的代码

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)

如何修复和添加代码?

您可以使用daysjs的.fromNow((方法。

const day = dayjs(date).fromNow();

最新更新