javascript moment.calendar() 没有时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32420447/
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.calendar() without the time
提问by carl
I would like to use the moment.calendar() option without the time... so instead of "Last Tuesday at 5pm" I want "Last Tuesday". Does anybody know whether moment has a solution for that by now? I found this fiddle http://jsfiddle.net/nawxZ/, which apparently shows a solution for that, but I can't see how this is supposed to work? thanks carl
我想在没有时间的情况下使用 moment.calendar() 选项......所以我想要“上周二”而不是“上周二下午 5 点”。有谁知道现在是否有解决方案?我找到了这个小提琴http://jsfiddle.net/nawxZ/,它显然显示了一个解决方案,但我看不出这应该如何工作?谢谢卡尔
function log(str) {
$('body').append('<p>' + str + '</p>');
}
log(moment().calendar());
log(moment().calendar(true));
回答by Fareed Alnamrouti
starting from moment 2.10.5 you can do:
从 2.10.5 开始,您可以执行以下操作:
moment(/*your date*/).calendar(null,{
lastDay : '[Yesterday]',
sameDay : '[Today]',
nextDay : '[Tomorrow]',
lastWeek : '[last] dddd',
nextWeek : 'dddd',
sameElse : 'L'
})
回答by Henrik Andersson
moment().calendar()
supports custom formatted strings and formatting functions.
moment().calendar()
支持自定义格式化字符串和格式化函数。
moment().calendar();
>> "Today at 9:06 AM"
Then set your formatted strings
然后设置格式化的字符串
moment.locale('yourlang', {
calendar: {
lastDay: function () {
return '[last]';
},
sameDay: function () {
return '[Today]';
}
}
});
moment().calendar();
// Will now output
>> "Today"
should do the trick. The docs are an invaluable source
应该做的伎俩。该文档是一个非常宝贵的源
回答by Mahfuzur Rahman
this is working perfectly. try it.
这是完美的工作。试试看。
(moment(time).calendar().split(" at"))[0]
(时刻(时间).日历().split(" at"))[0]
回答by Fabian von Ellerts
If you don't have custom times set and are only working on a day base, you can use this simple and multilang (tested for EN/DE/FR/IT) solution. (full days are always saved with a 00:00 timetag)
如果您没有设置自定义时间并且只在一天工作,您可以使用这个简单的多语言(针对 EN/DE/FR/IT 测试)解决方案。(全天总是与 00:00 时间标签一起保存)
let dateParts = moment(DATE).calendar().split(' '),
index = dateParts.indexOf('00:00');
// ['Yesterday', 'at', '00:00', 'PM'] -> ['Yesterday']
dateParts.length = (index !== -1 ? (index - 1) : dateParts.length);
回答by Jaime Pajuelo
This code works partially. The problem lies on separating the word atfrom [Today at].
此代码部分工作。问题在于将单词at与[Today at]分开。
In the example below, a las(means atin English) is part of the result.
在下面的示例中,las(在英语中的意思是at)是结果的一部分。
moment().locale()
// "es"
moment().calendar(null, {
sameDay: function (now) {
var sameDay = now._locale._calendar.sameDay
if (typeof sameDay === 'function') {
sameDay = sameDay.call(this)
}
return sameDay.replace(' LT', '')
}
})
// "hoy a las"
If the translations were "[Today] [at] LL", we would be able to separate the word at.
如果翻译是“[Today] [at] LL”,我们将能够将单词at分开。
回答by Clouren
I have the same requirement and unfortunately this is not supported currently (version 2.24.0).
我有同样的要求,不幸的是目前不支持(版本 2.24.0)。
If you look at an arbitrary language file, like en-gb.js, you can see that all the calendar variants contain the time.
There is no option to leave out the time. That would require a variant of calendar without the time for every language.
如果您查看任意语言文件,例如en-gb.js,您可以看到所有日历变体都包含时间。
没有选择不留出时间。这将需要一种日历的变体,而没有每种语言的时间。
回答by JabbyPanda
Since Moment 2.25.0
it is now possible to call moment.calendar(formats)without reference time value
由于 Moment2.25.0
现在可以在没有参考时间值的情况下调用moment.calendar(formats)
回答by Jonathan
I created an angular directive that provides a calendar with the date only. If you don't use angular, just use the function.
我创建了一个 angular 指令,该指令仅提供带有日期的日历。如果您不使用 angular,只需使用该功能。
app.filter('amCalendarDate', function($translate) {
return function(dt) {
var md = moment(dt), key = '';
if (!dt || !md || (md + '').toLowerCase() == 'invalid date')
return '';
var today = moment();
var diff = today.diff(md, 'days');
if (!diff)
key = 'Today';
else if (diff == -1)
key = 'Tomorrow';
else if (diff == 1)
key = 'Yesterday';
else if (Math.abs(diff) <= 6) {
if (diff < 0)
key = 'Next';
else
key = 'Last';
var days = 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_');
today.day(diff);
key += ' ' + days[today.day()];
}
if (key) {
// if you don't have translate, just return the key
return $translate.amCalendarDate[key];
} else
return md.format('L');
}
});