Javascript Moment.js - 明天、今天和昨天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35441820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Moment.js - tomorrow, today and yesterday
提问by Ziarno
I'd like the moment().fromNow()
functionality, but when the date is close it is too precise - ex. I don't want it to show 'in 3 hours' but 'today' - so basically with a 'daily' precision.
我想要这个moment().fromNow()
功能,但是当日期接近时它太精确了 - 例如。我不希望它显示“在 3 小时内”而是“今天”——所以基本上是“每日”精度。
I tried using the moment().calendar()
function, it doesn't format if the date difference is more than 1 day
我尝试使用该moment().calendar()
功能,如果日期差异超过 1 天,则不会格式化
回答by HussienK
You can also do this to get the date for today and tomorrow and yesterday
您也可以这样做来获取今天、明天和昨天的日期
let today = moment();
let tomorrow = moment().add(1,'days');
let yesterday = moment().add(-1, 'days');
回答by guillaumepotier
I use a combination of add()
and endOf()
with moment
我使用add()
和endOf()
与时刻的组合
//...
const today = moment().endOf('day')
const tomorrow = moment().add(1, 'day').endOf('day')
if (date < today) return 'today'
if (date < tomorrow) return 'tomorrow'
return 'later'
//...
回答by svangordon
You can customize the way that both the .fromNow
and the .calendar
methods display dates using moment.updateLocale
. The following code will change the way that .calendar
displays as per the question:
您可以使用 自定义.fromNow
和.calendar
方法显示日期的方式moment.updateLocale
。以下代码将.calendar
根据问题更改显示方式:
moment.updateLocale('en', {
calendar : {
lastDay : '[Yesterday]',
sameDay : '[Today]',
nextDay : '[Tomorrow]',
lastWeek : '[Last] dddd',
nextWeek : '[Next] dddd',
sameElse : 'L'
}
});
Based on the question, it seems like the .calendar
method would be more appropriate -- .fromNow
wants to have a past/present prefix/suffix, but if you'd like to find out more you can read the documentation at http://momentjs.com/docs/#/customization/relative-time/.
基于这个问题,这个.calendar
方法似乎更合适——.fromNow
想要一个过去/现在的前缀/后缀,但如果你想了解更多信息,你可以阅读http://momentjs.com 上的文档/docs/#/customization/relative-time/。
To use this in only one place instead of overwriting the locales, pass a string of your choice as the first argument when you define the moment.updateLocale
and then invoke the calendar method using that locale (eg. moment.updateLocale('yesterday-today').calendar( /* moment() or whatever */ )
)
要仅在一个地方使用它而不是覆盖语言环境,请在定义时传递您选择的字符串作为第一个参数moment.updateLocale
,然后使用该语言环境调用日历方法(例如。moment.updateLocale('yesterday-today').calendar( /* moment() or whatever */ )
)
EDIT:Moment ^2.12.0 now has the updateLocale
method. updateLocale
and locale
appear to be functionally the same, and locale
isn't yet deprecated, but updated the answer to use the newer method.
编辑:时刻 ^2.12.0 现在有updateLocale
方法。updateLocale
并且locale
似乎在功能上相同,locale
尚未弃用,但更新了答案以使用较新的方法。
回答by Ben
Requirements:
要求:
- When the date is further away, use the standard
moment().fromNow()
functionality. - When the date is closer, show
"today"
,"yesterday"
,"tomorrow"
, etc.
- 当日期较远时,使用标准
moment().fromNow()
功能。 - 当日期接近,显示
"today"
,"yesterday"
,"tomorrow"
等。
Solution:
解决方案:
// call this function, passing-in your date
function dateToFromNowDaily( myDate ) {
// get from-now for this date
var fromNow = moment( myDate ).fromNow();
// ensure the date is displayed with today and yesterday
return moment( myDate ).calendar( null, {
// when the date is closer, specify custom values
lastWeek: '[Last] dddd',
lastDay: '[Yesterday]',
sameDay: '[Today]',
nextDay: '[Tomorrow]',
nextWeek: 'dddd',
// when the date is further away, use from-now functionality
sameElse: function () {
return "[" + fromNow + "]";
}
});
}
NB: From version 2.14.0, the formats argument to the calendar function can be a callback, see http://momentjs.com/docs/#/displaying/calendar-time/.
注意:从 2.14.0 版开始,日历函数的格式参数可以是回调,请参阅http://momentjs.com/docs/#/displaying/calendar-time/。
回答by Alexandr Egorov
You can use this:
你可以使用这个:
const today = moment();
const tomorrow = moment().add(1, 'days');
const yesterday = moment().subtract(1, 'days');
回答by Adara Hv
I have similar solution, but allows to use locales:
我有类似的解决方案,但允许使用语言环境:
let date = moment(someDate);
if (moment().diff(date, 'days') >= 1) {
return date.fromNow(); // '2 days ago' etc.
}
return date.calendar().split(' ')[0]; // 'Today', 'yesterday', 'tomorrow'
回答by twernt
In Moment.js, the from()method has the daily precision you're looking for:
在 Moment.js 中,from()方法具有您正在寻找的日常精度:
var today = new Date();
var tomorrow = new Date();
var yesterday = new Date();
tomorrow.setDate(today.getDate()+1);
yesterday.setDate(today.getDate()-1);
moment(today).from(moment(yesterday)); // "in a day"
moment(today).from(moment(tomorrow)); // "a day ago"
moment(yesterday).from(moment(tomorrow)); // "2 days ago"
moment(tomorrow).from(moment(yesterday)); // "in 2 days"
回答by pravin
From 2.10.5 moment supports specifying calendar output formats per invocation For a more detailed documentation check Moment - Calendar.
从 2.10.5 开始,moment 支持为每次调用指定日历输出格式有关更详细的文档,请查看Moment-Calendar。
**Moment 2.10.5**
moment().calendar(null, {
sameDay: '[Today]',
nextDay: '[Tomorrow]',
nextWeek: 'dddd',
lastDay: '[Yesterday]',
lastWeek: '[Last] dddd',
sameElse: 'DD/MM/YYYY'
});
From 2.14.0 calendar can also take a callback to return values.
从 2.14.0 开始,日历也可以采用回调来返回值。
**Moment 2.14.0**
moment().calendar(null, {
sameDay: function (now) {
if (this.isBefore(now)) {
return '[Will Happen Today]';
} else {
return '[Happened Today]';
}
/* ... */
}
});
回答by Ziarno
So this is what I ended up doing
所以这就是我最终做的
var dateText = moment(someDate).from(new Date());
var startOfToday = moment().startOf('day');
var startOfDate = moment(someDate).startOf('day');
var daysDiff = startOfDate.diff(startOfToday, 'days');
var days = {
'0': 'today',
'-1': 'yesterday',
'1': 'tomorrow'
};
if (Math.abs(daysDiff) <= 1) {
dateText = days[daysDiff];
}
回答by Ashutosh
You can use .add() and .subtract() method to get yesterday and tomorrow date. Then use format method to get only date .format("D/M/Y"), D stand for Day, M for Month, Y for Year. Check in Moment Docs
您可以使用 .add() 和 .subtract() 方法来获取昨天和明天的日期。然后使用 format 方法只获取日期 .format("D/M/Y"),D 代表日,M 代表月,Y 代表年。签入 Moment 文档
let currentMilli = Date.now()
let today = Moment(currentMilli).format("D/M/Y");
let tomorrow = Moment(currentMilli).add(1, 'days').format("D/M/Y");
let yesterday = Moment(currentMilli).subtract(1, 'days').format("D/M/Y");
Result will be:
结果将是:
Current Milli - 1576693800000
today - 19/12/2019
tomorrow - 18/12/2019
yesterday - 18/12/2019