[英]Using moment, How to get date and hours as format?
I am working on a project that requires this kind of text response on a day and hours.我正在从事一个需要在一天和几个小时内进行这种文本响应的项目。
"1d 23hrs"-- data as milliseconds, also I am using moment js. “1d 23hrs”——以毫秒为单位的数据,我也在使用时刻 js。 i tried so many times but cannot cath the format days i got only -- 1 23--- without "d hrs".我尝试了很多次,但无法在没有“d hrs”的情况下获得我只得到的格式天数——1 23——。
can anyone help me to do this?谁能帮我做到这一点? here is the function which i have tried这是我试过的 function
const milliSec = 85600000;
const days = moment(moment.duration(milliSec)._data).format('D HH');
You can add text to your date format by adding a set of []
around the text you want to add like this.您可以通过在要添加的文本周围添加一组[]
来将文本添加到日期格式,如下所示。
days2 = moment(moment.duration(milliSec)._data).format('D[d] HH[h]');
To escape characters in format strings, you can wrap the characters in square brackets.要转义格式字符串中的字符,可以将字符括在方括号中。
In your case to add custom text try this:在您的情况下添加自定义文本试试这个:
const milliSec = 85600000; const days = moment(moment.duration(milliSec)._data).format("D[d] HH[hrs]"); console.log(days);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.