我正在使用带有 dayjs 的本机反应。
我想比较一下写评论的时间和当前时间,然后用dayjs在console.log(expectedtime)上记录比较的时间
像这个例子
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)
如何修复和添加我的代码?