[英]Uppercase first letter in moment js
有没有办法使用momment js让第一个字母大写?
Current output:
moment([20016, 0, 29]).fromNow(); // 4 years ago
Expected output:
moment([2016, 0, 29]).fromNow(); // 4 Years Ago
.capitalize {
text-transform: capitalize;
}
应用以下 class 会给你想要的效果。
编辑:
如果 CSS 不是你的菜,这里有几个不同的例子说明如何使用 JS 来完成它。
您可以将String.prototype.replace()
与regex
一起使用来实现此目的:
console.log(moment([2016, 0, 29]).fromNow().replace(/\b[az]/, match => match.toUpperCase()));
<script src="https://momentjs.com/downloads/moment.js"></script>
String.prototype.replace()
也得到一个 function 作为第二个参数:
将调用 function 以创建新的 substring 以用于替换给定正则表达式或子字符串的匹配项。
这是另一种方式。
console.log(moment([2016, 0, 29]).fromNow().split(' ').map(word => word[0].toUpperCase() + word.slice(1)).join(' '));
<script src="https://momentjs.com/downloads/moment.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.