聲明:我對JavaScript(和angular)非常陌生。讓Moment JS在前一天顯示到昨天,然後開始顯示格式化的日期
我試圖讓momentjs顯示從現在到昨天的時間。一旦過了3天,它會顯示實際的日期。
例使用隨機日期(日期差異將永遠是 「今天」):
- 17年10月10日在下午6時41分(未來)
- 明天下午6時41
- 今天,在下午6時41
- 昨天下午6點41
- 17年10月10日在下午6時41
不知道世界衛生大會t我應該提供的其他信息,但請詢問是否有幫助。
聲明:我對JavaScript(和angular)非常陌生。讓Moment JS在前一天顯示到昨天,然後開始顯示格式化的日期
我試圖讓momentjs顯示從現在到昨天的時間。一旦過了3天,它會顯示實際的日期。
例使用隨機日期(日期差異將永遠是 「今天」):
不知道世界衛生大會t我應該提供的其他信息,但請詢問是否有幫助。
謝謝大家的意見。使用你的例子,我能夠解決這個問題。不久之後,我發現了一個更好的方式來做我想要的,我將在這裏分享。
使用momentJS日曆功能使得此操作變得非常簡單。這是我的過濾器,正是我想要的,再加上一些額外的功能。
//This filter will display today, tomorrow, yesterday then display the actual date as 'M/D/Y at h:mm a'.
//Default preposition is 'at', but any other can be passed in as the second parameter.
.filter('formatForTimeAgo', function() {
return function timeTodayDateElse(date, preposition){
preposition=preposition|| 'at'
moment.lang('en', {
'calendar' : {
'lastDay' : '[Yesterday] [\n][ '+preposition+' ] h:mm a',
'sameDay' : '[Today] [\n][ '+preposition+' ] h:mm a',
'nextDay' : '[Tomorrow] [\n][ '+preposition+' ] h:mm a',
'lastWeek' : 'M/D/YY [\n]['+preposition+'] h:mm a',
'nextWeek' : 'M/D/YY [\n]['+preposition+'] h:mm a',
'sameElse' : 'M/D/YY [\n]['+preposition+'] h:mm a'
}
});
return moment(date).calendar();
}
})
{{yourDate | }}
格式化會出現這樣的後:Today at 10:17 pm
,Yesterday at 10:17 pm
,或Tomorrow at 10:17 pm
。一旦+/- 2天,它將顯示爲數字日期,例如9/13/17 at 10:17 pm
。
您可以將自定義的介詞(默認爲「AT」)與第二個參數。 例子:{{yourDate | formatForTimeAgo : 'by'}}
將改變「在」到「用」等你拿Today by 10:17 pm
,Yesterday by 10:17 pm
等
再次,謝謝。這是一個很好的學習經歷。
有幾個函數可以幫助獲得預期的輸出。
moment.format()
:你可以用它來像你想要的那樣格式化字符串。例如:moment.format("dd/MM/YY at hh:mm:ss")
moment.isSame()
:確定日期是否與今天相同。moment.diff()
:確定日期之間的差異。下面是根據你所期望的輸出是一個解決方案: https://jsfiddle.net/3ruv8aj3/
上面的提琴是一個粗糙的基於預期產出的解決方案可以進一步改進。 –
我不知道你有多少與時刻熟悉爲止,所以這裏的做到這一點的一種方式,從開頭:
// get today's date and time
var now = moment();
// will hold our display string (we'll set this in a minute)
var displayValue = '';
// target date & time in string format (YYYY-MM-DD hh:mm)
// you can also use a JavaScript date object instead of a string
var dateString = '2017-10-10 06:41';
// create a moment wrapper for the past/future date
var targetDate = moment(dateString);
// how many total days difference between then and now?
// calling moment() with no args defaults to current date and time
// first argument to `diff` is the date to subtract
// second argument is the unit of time to measure in
var difference = now.diff(targetDate, 'days');
if (difference > 3 || difference < -3) {
// if the date is more than three days in the past or more than three days into the future...
displayValue = targetDate.format('MM-DD-YYYY') + ' at ' + targetDate.format('hh:mm');
} else {
// otherwise...
displayValue = targetDate.fromNow();
}
console.log(displayValue); // > "10-10-2017 at 06:41"
編輯:如果目標日期恰好是今天,和你想的,而不是一時的默認顯示「今天」 '15小時前」,只是檢查的0
差異if (difference === 0) displayValue = 'Today at ' + targetDate.format('hh:mm');
編輯:爲清晰起見,爲'現在'創建了一個變量。
這裏很綠很抱歉,如果我解釋不正確。看起來你正在設定我上面給出的日期,並顯示差異與那個日期?日期將始終爲「今天」,如創建日期那樣。 – Camitose
你需要一個日期來檢查,但是,對嗎?與它有關的過去或未來日期的東西? – sheminusminus
這一行檢查當前日期:'var difference = moment()。diff(targetDate,'days');'所以,如果你想在今天的任何地方使用,你可以使用'moment()'。或者我誤解了這個問題? – sheminusminus
你應該添加你已經嘗試過的東西。然而,你可以檢查日期是否是今天/昨天/明天,在這種情況下只是做moment.format('字符串格式') – binariedMe
@binariedMe我試過搜索,雖然找不到任何職位尋找做我是這樣做。我目前使用標準格式'{{n.created_at | moment:'format':'M/D/YY [at] h:mm a'}}'將日期顯示爲「10/10/17 at 6:41 pm」,但不顯示「Today」等。 – Camitose
不,你將不得不檢查給定的日期是今天/明天/昨天,以顯示特定的字符串,並且它不會在moment.js中被支持。只需製作一個自定義管道並檢查給定日期是否爲t/t/y – binariedMe