所以我希望我的 javascript 代码将当前日期时间与帖子的日期时间进行比较。然后,如果差异仅以小时为单位,那么我希望它显示:x 小时前发布。 但如果是年、月或日,那么我希望它以不同的方式显示,例如:5 天前发布,3 年前发布。
我在php中做了这个逻辑,但现在我必须把它转换成javascript。这是我写的php代码:
$dateofmessage = date_create($posts['date_time']);
date_default_timezone_set("{$_GET['userTimezone']}");
$date11 = date("y-m-d H:i:s");
date_default_timezone_set('America/New_York');
$date22 = date("y-m-d H:i:s");
if ($date11 > $date22) {
$date1 = date_create($date11);
$date2 = date_create($date22);
$diff = date_diff($date1, $date2);
$differenceToAddOrSub = $diff->format("%y years %m months %d days %h hours %i minutes %s seconds");
$changeddate = date_add($dateofmessage, date_interval_create_from_date_string("$differenceToAddOrSub"));
} elseif ($date22 > $date11) {
$date1 = date_create($date11);
$date2 = date_create($date22);
$diff = date_diff($date2, $date1);
$differenceToAddOrSub = $diff->format("%y years %m months %d days %h hours %i minutes %s seconds");
$changeddate = date_sub($dateofmessage, date_interval_create_from_date_string("$differenceToAddOrSub"));
}
if (date_format($date1, "Y") == date_format($changeddate, "Y")) {
if (date_format($date1, "d") == date_format($changeddate, "d") && date_format($date1, "m") == date_format($changeddate, "m")) {
$time = date_format($changeddate, "h:i a");
if ($filetype == "null") {
echo "<div class='post-div' id='$id'><p class='time'>$time</p><h1 class='title'>$title</h1><div class='sender'>Posted by : $username</div><div class='like_buttons'><button class='likes fas fa-thumbs-up' onclick='community_like(true, $id)'></button><span id='likes_$id' class='like-dislike'>$likes</span><button class='likes fas fa-thumbs-down' onclick='community_like(false, $id)'></button><span class='like-dislikes' id='dislike_$id'>$dislikes</span></div><p class='post'>$post</p></div><div class='comments'><p class='see_all_comments' onclick='commentsAppear($id, $communityid)'>See all comments</p></div>";
}
if ($filetype == "video") {
echo "<div class='post-div' id='$id'><p class='time'>$time</p><h1 class='title'>$title</h1><div class='sender'>Posted by : $username</div><div class='like_buttons'><button class='likes fas fa-thumbs-up' onclick='community_like(true, $id)'></button><span id='likes_$id' class='like-dislike'>$likes</span><button class='likes fas fa-thumbs-down' onclick='community_like(false, $id)'></button><span class='like-dislikes' id='dislike_$id'>$dislikes</span></div><video style='width:100%;' autoplay controls src='$pic_or_vid'></video></div><div class='comments'><p class='see_all_comments' onclick='commentsAppear($id, $communityid)'>See all comments</p></div>";
}
if ($filetype == "image") {
echo "<div class='post-div' id='$id'><p class='time'>$time</p><h1 class='title'>$title</h1><div class='sender'>Posted by : $username</div><div class='like_buttons'><button class='likes fas fa-thumbs-up' onclick='community_like(true, $id)'></button><span id='likes_$id' class='like-dislike'>$likes</span><button class='likes fas fa-thumbs-down' onclick='community_like(false, $id)'></button><span class='like-dislikes' id='dislike_$id'>$dislikes</span></div><img style='width:100%;' src='$pic_or_vid'></img></div><div class='comments'><p class='see_all_comments' onclick='commentsAppear($id, $communityid)'>See all comments</p></div>";
}
if ($filetype == "textImage") {
echo "<div class='post-div' id='$id'><p class='time'>$time</p><h1 class='title'>$title</h1><div class='sender'>Posted by : $username</div><div class='like_buttons'><button class='likes fas fa-thumbs-up' onclick='community_like(true, $id)'></button><span id='likes_$id' class='like-dislike'>$likes</span><button class='likes fas fa-thumbs-down' onclick='community_like(false, $id)'></button><span class='like-dislikes' id='dislike_$id'>$dislikes</span></div><p class='post'>$post</p><img style='width:100%;' src='$pic_or_vid'></img></div><div class='comments'><p class='see_all_comments' onclick='commentsAppear($id, $communityid)'>See all comments</p></div>";
}
if ($filetype == "textVideo") {
echo "<div class='post-div' id='$id'><p class='time'>$time</p><h1 class='title'>$title</h1><div class='sender'>Posted by : $username</div><div class='like_buttons'><button class='likes fas fa-thumbs-up' onclick='community_like(true, $id)'></button><span id='likes_$id' class='like-dislike'>$likes</span><button class='likes fas fa-thumbs-down' onclick='community_like(false, $id)'></button><span class='like-dislikes' id='dislike_$id'>$dislikes</span></div><p class='post'>$post</p><video style='width:100%;' autoplay controls src='$pic_or_vid'></video></div><div class='comments'><p class='see_all_comments' onclick='commentsAppear($id, $communityid)'>See all comments</p></div>";
}
} else {
$changeddatee = date_format($changeddate, "y-m-d H:i:s");
$date = date( "M jS", strtotime($changeddatee));
$time = date_format($changeddate, "h:i a");
if ($filetype == "null") {
echo "<div class='post-div' id='$id'><p class='time'>$time $date</p><h1 class='title'>$title</h1><div class='sender'>Posted by : $username</div><div class='like_buttons'><button class='likes fas fa-thumbs-up' onclick='community_like(true, $id)'></button><span id='likes_$id' class='like-dislike'>$likes</span><button class='likes fas fa-thumbs-down' onclick='community_like(false, $id)'></button><span class='like-dislikes' id='dislike_$id'>$dislikes</span></div><p class='post'>$post</p></div><div class='comments'><p class='see_all_comments' onclick='commentsAppear($id, $communityid)'>See all comments</p></div>";
}
if ($filetype == "video") {
echo "<div class='post-div' id='$id'><p class='time'>$time $date</p><h1 class='title'>$title</h1><div class='sender'>Posted by : $username</div><div class='like_buttons'><button class='likes fas fa-thumbs-up' onclick='community_like(true, $id)'></button><span id='likes_$id' class='like-dislike'>$likes</span><button class='likes fas fa-thumbs-down' onclick='community_like(false, $id)'></button><span class='like-dislikes' id='dislike_$id'>$dislikes</span></div><video style='width:100%;' autoplay controls src='$pic_or_vid'></video></div><div class='comments'><p class='see_all_comments' onclick='commentsAppear($id, $communityid)'>See all comments</p></div>";
}
if ($filetype == "image") {
echo "<div class='post-div' id='$id'><p class='time'>$time $date</p><h1 class='title'>$title</h1><div class='sender'>Posted by : $username</div><div class='like_buttons'><button class='likes fas fa-thumbs-up' onclick='community_like(true, $id)'></button><span id='likes_$id' class='like-dislike'>$likes</span><button class='likes fas fa-thumbs-down' onclick='community_like(false, $id)'></button><span class='like-dislikes' id='dislike_$id'>$dislikes</span></div><img style='width:100%;' src='$pic_or_vid'></img></div><div class='comments'><p class='see_all_comments' onclick='commentsAppear($id, $communityid)'>See all comments</p></div>";
}
if ($filetype == "textImage") {
echo "<div class='post-div' id='$id'><p class='time'>$time $date</p><h1 class='title'>$title</h1><div class='sender'>Posted by : $username</div><div class='like_buttons'><button class='likes fas fa-thumbs-up' onclick='community_like(true, $id)'></button><span id='likes_$id' class='like-dislike'>$likes</span><button class='likes fas fa-thumbs-down' onclick='community_like(false, $id)'></button><span class='like-dislikes' id='dislike_$id'>$dislikes</span></div><p class='post'>$post</p><img style='width:100%;' src='$pic_or_vid'></img></div><div class='comments'><p class='see_all_comments' onclick='commentsAppear($id, $communityid)'>See all comments</p></div>";
}
if ($filetype == "textVideo") {
echo "<div class='post-div' id='$id'><p class='time'>$time $date</p><h1 class='title'>$title</h1><div class='sender'>Posted by : $username</div><div class='like_buttons'><button class='likes fas fa-thumbs-up' onclick='community_like(true, $id)'></button><span id='likes_$id' class='like-dislike'>$likes</span><button class='likes fas fa-thumbs-down' onclick='community_like(false, $id)'></button><span class='like-dislikes' id='dislike_$id'>$dislikes</span></div><p class='post'>$post</p><video style='width:100%;' autoplay controls src='$pic_or_vid'></video></div><div class='comments'><p class='see_all_comments' onclick='commentsAppear($id, $communityid)'>See all comments</p></div>";
}
}
} else {
$changeddatee = date_format($changeddate, "y-m-d H:i:s");
$date = date( "M jS Y", strtotime($changeddatee));
$time = date_format($changeddate, "h:i a");
if ($filetype == "null") {
echo "<div class='post-div' id='$id'><p class='time'>$time $date</p><h1 class='title'>$title</h1><div class='sender'>Posted by : $username</div><div class='like_buttons'><button class='likes fas fa-thumbs-up' onclick='community_like(true, $id)'></button><span id='likes_$id' class='like-dislike'>$likes</span><button class='likes fas fa-thumbs-down' onclick='community_like(false, $id)'></button><span class='like-dislikes' id='dislike_$id'>$dislikes</span></div><p class='post'>$post</p></div><div class='comments'><p class='see_all_comments' onclick='commentsAppear($id, $communityid)'>See all comments</p></div>";
}
if ($filetype == "video") {
echo "<div class='post-div' id='$id'><p class='time'>$time $date</p><h1 class='title'>$title</h1><div class='sender'>Posted by : $username</div><div class='like_buttons'><button class='likes fas fa-thumbs-up' onclick='community_like(true, $id)'></button><span id='likes_$id' class='like-dislike'>$likes</span><button class='likes fas fa-thumbs-down' onclick='community_like(false, $id)'></button><span class='like-dislikes' id='dislike_$id'>$dislikes</span></div><video style='width:100%;' autoplay controls src='$pic_or_vid'></video></div><div class='comments'><p class='see_all_comments' onclick='commentsAppear($id, $communityid)'>See all comments</p></div>";
}
if ($filetype == "image") {
echo "<div class='post-div' id='$id'><p class='time'>$time $date</p><h1 class='title'>$title</h1><div class='sender'>Posted by : $username</div><div class='like_buttons'><button class='likes fas fa-thumbs-up' onclick='community_like(true, $id)'></button><span id='likes_$id' class='like-dislike'>$likes</span><button class='likes fas fa-thumbs-down' onclick='community_like(false, $id)'></button><span class='like-dislikes' id='dislike_$id'>$dislikes</span></div><img style='width:100%;' src='$pic_or_vid'></img></div><div class='comments'><p class='see_all_comments' onclick='commentsAppear($id, $communityid)'>See all comments</p></div>";
}
if ($filetype == "textImage") {
echo "<div class='post-div' id='$id'><p class='time'>$time $date</p><h1 class='title'>$title</h1><div class='sender'>Posted by : $username</div><div class='like_buttons'><button class='likes fas fa-thumbs-up' onclick='community_like(true, $id)'></button><span id='likes_$id' class='like-dislike'>$likes</span><button class='likes fas fa-thumbs-down' onclick='community_like(false, $id)'></button><span class='like-dislikes' id='dislike_$id'>$dislikes</span></div><p class='post'>$post</p><img style='width:100%;' src='$pic_or_vid'></img></div><div class='comments'><p class='see_all_comments' onclick='commentsAppear($id, $communityid)'>See all comments</p></div>";
}
if ($filetype == "textVideo") {
echo "<div class='post-div' id='$id'><p class='time'>$time $date</p><h1 class='title'>$title</h1><div class='sender'>Posted by : $username</div><div class='like_buttons'><button class='likes fas fa-thumbs-up' onclick='community_like(true, $id)'></button><span id='likes_$id' class='like-dislike'>$likes</span><button class='likes fas fa-thumbs-down' onclick='community_like(false, $id)'></button><span class='like-dislikes' id='dislike_$id'>$dislikes</span></div><p class='post'>$post</p><video style='width:100%;' autoplay controls src='$pic_or_vid'></video></div><div class='comments'><p class='see_all_comments' onclick='commentsAppear($id, $communityid)'>See all comments</p></div>";
}
}
}
}
请帮忙,我在日期时间和 javascript 方面遇到很多麻烦!
答案 0 :(得分:1)
如何使用 Moment.js?
您上面的整个代码可能只替换为:
moment(*any valid date*).fromNow()
一些例子:
const year = moment("2011-10-31", "YYYY-MM-DD").fromNow()
console.log(year)
const day = moment().startOf('day').fromNow()
console.log(day)
const hour = moment().subtract(1, 'hour').fromNow()
console.log(hour)
const now = moment().subtract(1, 'second').fromNow()
console.log(now)
const yourJsDate = new Date(2021,3,15) // or any valid date as a string
const momentDate = moment(yourJsDate).fromNow()
console.log(momentDate)
const timezone = moment("2021-04-05 12:40:00 -07:30", "YYYY-MM-DD HH:mm:ss Z").fromNow() // UTC -7h30min
console.log(timezone)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>