2012-07-02 10 views

Trả lời

7

tôi đang tìm kiếm tại cùng một vấn đề và có vẻ như không có kế hoạch hỗ trợ này trong tương lai ...

Mặc dù một workaround đề xuất là làm cho một định nghĩa ngôn ngữ mà ghi đè thực hiện mặc định các thông điệp nhân bản :

https://github.com/timrwood/moment/issues/348

Kind của một quá mức cần thiết nếu bạn hỏi tôi ...

11

Moment.js đang cung cấp các chức năng fromNow để có thời lượng trong con người có thể đọc được của con người, hãy xem http://momentjs.com/docs/#/displaying/fromnow/

Ví dụ:

moment([2007, 0, 29]).fromNow(); // 4 years ago 
moment().subtract(375, 'days').fromNow(); // a year ago 

Bạn cần phải sử dụng lib bên thứ ba theo đề nghị của @Fluffy

+1

Không cần bên thứ ba. Bạn có thể sử dụng ['moment.relativeTimeThreshold ('y', 365)'] (https://github.com/moment/momentjs.com/blob/master/docs/moment/07-customization/13-relative-time- threshold.md) để đặt làm tròn. – RJFalconer

0

Đây là giải pháp của tôi trên CoffeeScript:

humanizeDuration = (eventDuration)-> 
    eventMDuration = Moment.duration(eventDuration, 'seconds'); 
    eventDurationString = "" 
    if (eventMDuration.days() > 0) 
     eventDurationString += " " + Moment.duration(eventMDuration.days(), 'days').humanize() 
    if (eventMDuration.hours() > 0) 
     eventDurationString += " " + Moment.duration(eventMDuration.hours(), 'hours').humanize() 
    if (eventMDuration.minutes() > 0) 
     eventDurationString += " " + Moment.duration(eventMDuration.minutes(), 'minutes').humanize() 

    eventDurationString.trim() 
0

This issue trên Github chứa rất nhiều cuộc thảo luận về chính xác điều đó. Nhiều người đang yêu cầu một lựa chọn nhân bản chính xác hơn.

Chime với lý do tại sao bạn cần nó, trường hợp sử dụng vv

https://github.com/moment/moment/issues/348

1

tôi đã viết javascript này code để nhân đạo thời gian,

function humanizeDuration(timeInMillisecond) { 
    var result = ""; 
    if (timeInMillisecond) { 
     if ((result = Math.round(timeInMillisecond/(1000 * 60 * 60 * 24 * 30 * 12))) > 0) {//year 
      result = result === 1 ? result + " Year" : result + " Years"; 
     } else if ((result = Math.round(timeInMillisecond/(1000 * 60 * 60 * 24 * 30))) > 0) {//months 
      result = result === 1 ? result + " Month" : result + " Months"; 
     } else if ((result = Math.round(timeInMillisecond/(1000 * 60 * 60 * 24))) > 0) {//days 
      result = result === 1 ? result + " Day" : result + " Days"; 
     } else if ((result = Math.round(timeInMillisecond/(1000 * 60 * 60))) > 0) {//Hours 
      result = result === 1 ? result + " Hours" : result + " Hours"; 
     } else if ((result = Math.round(timeInMillisecond/(1000 * 60))) > 0) {//minute 
      result = result === 1 ? result + " Minute" : result + " Minutes"; 
     } else if ((result = Math.round(timeInMillisecond/1000)) > 0) {//second 
      result = result === 1 ? result + " Second" : result + " Seconds"; 
     } else { 
      result = timeInMillisecond + " Millisec"; 
     } 
    } 
    return result; 
} 
3

Đây là giải pháp của tôi, tôi như nó tốt hơn những người khác ở đây:

val moment1 = moment(); 
val moment2 = mement(); 
console.log(moment.duration(moment1.diff(moment2)).humanize()); 
2

Sử dụng moment.relativeTimeThreshold('y', 365) để đặt làm tròn.

moment.relativeTimeThreshold('s', 60); 
moment.relativeTimeThreshold('m', 60); 
moment.relativeTimeThreshold('h', 24); 
moment.relativeTimeThreshold('d', 31); 
moment.relativeTimeThreshold('M', 12); 
moment.relativeTimeThreshold('y', 365);