<i id='C0UIl'><tr id='C0UIl'><dt id='C0UIl'><q id='C0UIl'><span id='C0UIl'><b id='C0UIl'><form id='C0UIl'><ins id='C0UIl'></ins><ul id='C0UIl'></ul><sub id='C0UIl'></sub></form><legend id='C0UIl'></legend><bdo id='C0UIl'><pre id='C0UIl'><center id='C0UIl'></center></pre></bdo></b><th id='C0UIl'></th></span></q></dt></tr></i><div id='C0UIl'><tfoot id='C0UIl'></tfoot><dl id='C0UIl'><fieldset id='C0UIl'></fieldset></dl></div>

      <legend id='C0UIl'><style id='C0UIl'><dir id='C0UIl'><q id='C0UIl'></q></dir></style></legend>

        <bdo id='C0UIl'></bdo><ul id='C0UIl'></ul>
    1. <small id='C0UIl'></small><noframes id='C0UIl'>

      <tfoot id='C0UIl'></tfoot>
    2. 用于人类友好的相对日期格式的 Javascript 库

      时间:2023-05-29
        <i id='tzP3z'><tr id='tzP3z'><dt id='tzP3z'><q id='tzP3z'><span id='tzP3z'><b id='tzP3z'><form id='tzP3z'><ins id='tzP3z'></ins><ul id='tzP3z'></ul><sub id='tzP3z'></sub></form><legend id='tzP3z'></legend><bdo id='tzP3z'><pre id='tzP3z'><center id='tzP3z'></center></pre></bdo></b><th id='tzP3z'></th></span></q></dt></tr></i><div id='tzP3z'><tfoot id='tzP3z'></tfoot><dl id='tzP3z'><fieldset id='tzP3z'></fieldset></dl></div>
      1. <tfoot id='tzP3z'></tfoot>
        • <bdo id='tzP3z'></bdo><ul id='tzP3z'></ul>

            <small id='tzP3z'></small><noframes id='tzP3z'>

              <tbody id='tzP3z'></tbody>
            <legend id='tzP3z'><style id='tzP3z'><dir id='tzP3z'><q id='tzP3z'></q></dir></style></legend>
              • 本文介绍了用于人类友好的相对日期格式的 Javascript 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我想以人性化的格式显示一些相对于当前日期的日期.

                I'd like to display some dates as relative to the current date in a human-friendly format.

                人类友好的相对日期示例:

                Examples of human-friendly relative dates:

                • 10 秒前
                • 20 分钟后
                • 1 天前
                • 5 周前
                • 2 个月前

                基本上忠实地保留最高数量级(并且优先考虑,仅在超过其中 2 个单位时上调单位 - 5 周而不是 1 个月).

                Basically faithfully preserving the highest order of magnitude (and by preference, only shifting up units when passing 2 of those units - 5 weeks instead of 1 month).

                虽然我可以接受一个控制较少且日期更友好的图书馆,例如:

                Though I could live with a library that had less control and even more friendly dates like:

                • 昨天
                • 明天
                • 上周
                • 几分钟前
                • 几个小时后

                有任何流行的图书馆吗?

                Any popular libraries for this?

                推荐答案

                自从我写了这个答案,一个众所周知的可用库是 moment.js.

                Since I wrote this answer, a well known library available is moment.js.

                有可用的库,但自己实现它很简单.只需使用少数几个条件.

                There are libraries available, but it is trivial to implement it yourself. Just use a handful of conditions.

                假设 date 是一个实例化的 Date 对象,用于您要与之进行比较的时间.

                Assume date is an instantiated Date object for the time you want to make a comparison against.

                // Make a fuzzy time
                var delta = Math.round((+new Date - date) / 1000);
                
                var minute = 60,
                    hour = minute * 60,
                    day = hour * 24,
                    week = day * 7;
                
                var fuzzy;
                
                if (delta < 30) {
                    fuzzy = 'just then.';
                } else if (delta < minute) {
                    fuzzy = delta + ' seconds ago.';
                } else if (delta < 2 * minute) {
                    fuzzy = 'a minute ago.'
                } else if (delta < hour) {
                    fuzzy = Math.floor(delta / minute) + ' minutes ago.';
                } else if (Math.floor(delta / hour) == 1) {
                    fuzzy = '1 hour ago.'
                } else if (delta < day) {
                    fuzzy = Math.floor(delta / hour) + ' hours ago.';
                } else if (delta < day * 2) {
                    fuzzy = 'yesterday';
                }
                

                您需要调整它以处理未来的日期.

                You would need to adapt this to handle future dates.

                这篇关于用于人类友好的相对日期格式的 Javascript 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何设置 VSCode 以将花括号放在新行上? 下一篇:Javascript:如何直接从对象生成格式化的易于阅读的

                相关文章

                最新文章

                <small id='0Vh02'></small><noframes id='0Vh02'>

                  <bdo id='0Vh02'></bdo><ul id='0Vh02'></ul>
                  <tfoot id='0Vh02'></tfoot>

                  <legend id='0Vh02'><style id='0Vh02'><dir id='0Vh02'><q id='0Vh02'></q></dir></style></legend>
                  1. <i id='0Vh02'><tr id='0Vh02'><dt id='0Vh02'><q id='0Vh02'><span id='0Vh02'><b id='0Vh02'><form id='0Vh02'><ins id='0Vh02'></ins><ul id='0Vh02'></ul><sub id='0Vh02'></sub></form><legend id='0Vh02'></legend><bdo id='0Vh02'><pre id='0Vh02'><center id='0Vh02'></center></pre></bdo></b><th id='0Vh02'></th></span></q></dt></tr></i><div id='0Vh02'><tfoot id='0Vh02'></tfoot><dl id='0Vh02'><fieldset id='0Vh02'></fieldset></dl></div>