我使用 moment.js ,例如,我有3个不同的日期。
我现在尝试计算从这些日期到今天(如果少于7天前)到今天的几周或直到今天(如果超过7天前)的几周之内的差异,并将其分成几个跨度。
>更新 感谢托马斯!
我知道了
$(document).ready(function(){
$('.timestamp').html((index, html) => {
let date = moment(html, "DD.MM.YYYY HH:mm", true),
now = moment(),
days = Math.floor(Math.abs(date - now) / 86400000),
weeks = Math.floor(days/7),
result = date.format("DD.MM.YYYY") + " - ";
if(weeks){
result += weeks + (weeks===1? " week ": " weeks ");
days = days % 7;
}
if(days || weeks===0){
result += days + (days === 1? " day": " days");
}
return result;
});
});
我还需要什么:
不显示初始日期,仅显示"3 Days"
。如果它删除了“结果”,我将继续工作。
未显示"7 weeks 2 days"
,应为"7 weeks"
这是实际的fiddle。
答案 0 :(得分:3)
您可以使用momentjs diff()
method 来执行此操作,它可以返回*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body{
background: #ddd;
font-family: 'Roboto', sans-serif;
}
header{
background-color: #333333;
color: #fff;
}
.container{
width: 1140px;
margin: auto;
padding: 10px;
}
.no-padding{
padding: 0;
}
.container-fluid{
width: 100%;
}
[class*="col-"] {
float: left;
}
.col-6{
width: 50%;
}
.col-12{
width: 100%;
}
.row::after{
content: '';
clear: both;
display: table;
}
.right ul {
list-style-type: none;
}
.right ul li{
display: inline-block;
margin-left: 10px;
}
.right ul li a{
color: #aaaaaa;
font-size: 13px;
}
.left ul {
list-style-type: none;
}
.left ul li{
display: inline-block;
}
.left ul li a{
color: #aaaaaa;
font-size: 13px;
}
.contact span{
color: #aaaaaa;
font-size: 12px;
}
.contact ul li a .fas{
transform: rotateY(180deg);
}
.logo{
height: 60px;
position: absolute;
display: flex;
justify-content: center;
align-items: center;
margin-left: 18px;
}
.logo img{
width: 85px;
height: auto;
float: left;
}
nav{
height: 60px;
background: #fff;
width: 100%;
}
nav ul{
margin-left: 690px;
}
nav ul li{
list-style-type: none;
display: inline-block;
float: left;
line-height: 60px;
}
nav ul li a{
display: block;
text-decoration: none;
font-size: 15px;
color: #000;
font-weight: 550;
padding: 0 13px;
}
nav ul li a:hover{
color: #0345bf;
}
nav ul li.active a{
color: #0345bf;
}
.sticky{
position: fixed;
top: 0;
left: 0;
z-index:auto;
}
.hero{
background-image: linear-gradient(rgba(0,0,0,0.5),rgba(0,0,0,0.5)),url(images/hero.jpg);
height: 90vh;
width: 100%;
background-size: cover;
background-position: center top;
position: relative;
background-attachment: fixed;
}
.herotext{
position: absolute;
top: 47%;
left: 50%;
transform: translate(-50%,-50%);
width: 900px;
/*border: 1px solid red;*/
color: #fff;
}
.herotext h2{
font-size: 76px;
}
.herotext p{
line-height: 28px;
font-size: 18px;
margin: 10px 0 35px 0;
}
.herotext .button{
text-decoration: none;
color: #fff;
font-size: 19px;
padding: 14px 16px;
background: orangered;
border-radius: 30px;
transition: all 0.5s ease;
}
.herotext .button:hover{
background: #AA2E00;
}
.intro{
background: #fff;
min-height: 400px;
}
,dates
,{{1 }},days
,weeks
,...,取决于您传递给它的选项。
这应该是您的代码:
months
演示:
hours
minutes
答案 1 :(得分:0)
Moment.js具有fromNow()函数,该函数从当前日期/时间返回“ x天”或“ x小时前”。
moment([2007, 0, 29]).fromNow(); // 4 years ago
moment([2007, 0, 29]).fromNow(true); // 4 years