简体   繁体   English

如何正确使用两次moment.updateLocale?

[英]How to use moment.updateLocale twice correctly?

I try to moment.updateLocale twice in different functions. 我尝试在不同的函数中两次使用moment.updateLocale。

I expect the code to print 我希望代码可以打印

Today1 12:00 am
Today2 12:00 am

But right now it prints 但是现在它打印

Today at 12:00 AM
Today1 12:00 am

jsbin jsbin

function showTime1(dateTime) {
  moment.updateLocale('en', {
    calendar : {
      sameDay : '[Today1] h:mm a'
    }
  });

  return dateTime.calendar();
}

function showTime2(dateTime) {
  moment.updateLocale('en', {
    calendar : {
      sameDay : '[Today2] h:mm a'
    }
  });

  return dateTime.calendar();
}



console.log(showTime1(moment().startOf('day')));
console.log(showTime2(moment().startOf('day')));

I found that I should use calendar directly in this case. 我发现在这种情况下我应该直接使用calendar The API document is here . API文档在这里

jsbin jsbin

function showTime1(dateTime) {
  return dateTime.calendar(null, {
    sameDay: '[Today1] h:mm a'
  });
}

function showTime2(dateTime) {
  return dateTime.calendar(null, {
    sameDay: '[Today2] h:mm a'
  });
}



console.log(showTime1(moment().startOf('day')));
console.log(showTime2(moment().startOf('day')));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM