如何在没有时间的情况下获取 ReferenceTime
How to get ReferenceTime without time
MomentJS 有选项 "referanceTime" - http://momentjs.com/docs/#/displaying/calendar-time/ 并且它显示这样的日期时间 Last Monday 2:30 AM
问题是当我有没有时间的日期时 - 它显示 Last Monday 0:00 AM
- 如何在没有日期的情况下摆脱时间?
与我的回答非常相似但稍微更高级。
我认为自定义日历时间的唯一方法是使用 custom locale。在您的情况下,您需要使用自定义函数。
这里有一个有点复杂但完整的方法:
// Factory-type function that returns a function.
// Used to set argument (fstr) without using `bind` (since bind changes `this`)
var stripZeroTime = function (fstr) {
return function () {
if (this.format("H:mm:ss") === "0:00:00") { // if midnight
return fstr.replace("LT", "") //remove time
.replace("at", "") //remove at
.replace(/\s+/g, ' ').trim(); //strip extra spaces
}
return fstr;
}
}
moment.locale('en-cust', {
calendar: {
lastDay: stripZeroTime('[Yesterday at] LT'), // Default format strings
sameDay: stripZeroTime('[Today at] LT'),
nextDay: stripZeroTime('[Tomorrow at] LT'),
lastWeek: stripZeroTime('[last] dddd [at] LT'),
nextWeek: stripZeroTime('dddd [at] LT'),
sameElse: stripZeroTime('L')
}
});
一个更简单的方法是在午夜时分去掉 0:00:00
。
function stripMidnight(m){
if(m.format("H:mm:ss") === "0:00:00"){
return m.calendar().replace("0:00:00","");
}else{
return m.calendar();
}
}
从 2.10.5 开始我们可以通过调用定义我们自己的渲染引擎(calendar
函数的第二个参数)- http://momentjs.com/docs/#/displaying/calendar-time/
但现在我们只能使用字符串而不是函数来定义模式
MomentJS 有选项 "referanceTime" - http://momentjs.com/docs/#/displaying/calendar-time/ 并且它显示这样的日期时间 Last Monday 2:30 AM
问题是当我有没有时间的日期时 - 它显示 Last Monday 0:00 AM
- 如何在没有日期的情况下摆脱时间?
与我的回答非常相似
我认为自定义日历时间的唯一方法是使用 custom locale。在您的情况下,您需要使用自定义函数。
这里有一个有点复杂但完整的方法:
// Factory-type function that returns a function.
// Used to set argument (fstr) without using `bind` (since bind changes `this`)
var stripZeroTime = function (fstr) {
return function () {
if (this.format("H:mm:ss") === "0:00:00") { // if midnight
return fstr.replace("LT", "") //remove time
.replace("at", "") //remove at
.replace(/\s+/g, ' ').trim(); //strip extra spaces
}
return fstr;
}
}
moment.locale('en-cust', {
calendar: {
lastDay: stripZeroTime('[Yesterday at] LT'), // Default format strings
sameDay: stripZeroTime('[Today at] LT'),
nextDay: stripZeroTime('[Tomorrow at] LT'),
lastWeek: stripZeroTime('[last] dddd [at] LT'),
nextWeek: stripZeroTime('dddd [at] LT'),
sameElse: stripZeroTime('L')
}
});
一个更简单的方法是在午夜时分去掉 0:00:00
。
function stripMidnight(m){
if(m.format("H:mm:ss") === "0:00:00"){
return m.calendar().replace("0:00:00","");
}else{
return m.calendar();
}
}
从 2.10.5 开始我们可以通过调用定义我们自己的渲染引擎(calendar
函数的第二个参数)- http://momentjs.com/docs/#/displaying/calendar-time/
但现在我们只能使用字符串而不是函数来定义模式