js time is converted to a few days ago, a few hours ago, a few minutes ago

use environment

Bilingual Chinese and English | Submit for revision

Moment is designed to work both in the browser and in Node.js.

All code should work in both environments, and all unit tests should run in both environments.

The CI system currently uses the following browsers: Chrome on Windows XP, IE 8, 9 and 10 on Windows 7, IE 11 on Windows 10, latest Firefox on Linux, and latest Safari on OSX 10.8 and 10.11.

If you want to try out the sample code below, just open your browser's console and type.

Node.js

Bilingual Chinese and English | Submit for revision

npm install moment
var moment = require('moment');
moment().format();

Note: In 2.4.0 , the global moment object has been deprecated. It will be removed in the next major release.

browser

Bilingual Chinese and English | Submit for revision

<script src="moment.js"></script>
<script>
    moment().format();
</script>

Moment.js is available on cdnjs.com and jsDelivr .

Bower

Bilingual Chinese and English | Submit for revision

bower

bower install --save moment

Noteworthy files are moment.js, locale/*.jsand min/moment-with-locales.js.

Require.js

Bilingual Chinese and English | Submit for revision

I highly recommend reading this if you plan to use moment with Require.js . Also, please upgrade to 2.14.0 or later for the best experience.

First, you may need to get moment by placing moment.js and the locale directory in the base folder via bower or node_modules or something. You can then use a tool like adapt-pkg-main , or manually use the package configuration .

requirejs.config({
  packages: [{
    name: 'moment',
    // 此位置是相对于 baseUrl 的。 
    // 选择 bower_components 还是 node_modules 取决于具体的安装方式。
    location: '[bower_components|node_modules]/moment'
    main: 'moment'
  }]
});

With the above settings, you can use to momentimport core modules and use moment/locale/deto deimport locales.

// 只需要核心模块。
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// 具有单一语言环境的核心模块。
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// 具有所有语言环境的核心模块。
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// 异步加载语言环境。
define(['require', 'moment'], function(require, moment) {
  // 检测到语言环境之后,在某个模块内部。 
  // 这是在模块加载时间之前不知道语言环境的情况。
  require(['moment/locale/de'], function(localeModule) {
    // 此处已加载语言环境,但尚未使用。
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // 已经正确地设置了语言环境之后使用 moment。
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});

 


 

  const getCommentTime = (createTime) => {
    const interval = (serverTime - createTime) / 1000
    if (interval < 60) {
      return '刚刚'
    } else if (interval < 60 * 60) {
      let tempTime = Math.floor(interval / 60)
      return `${tempTime}分钟前`
    } else if (interval < 60 * 60 * 24) {
      let tempTime = Math.floor(interval / (60 * 60))
      return `${tempTime}小时前`
    } else if (interval < 60 * 60 * 24 * 7) {
      let tempTime = Math.floor(interval / (60 * 60 * 24))
      return `${tempTime}天前`
    } else if (interval < 60 * 60 * 24 * 365) {
      return moment(createTime - 0).format('MM-DD')
    } else {
      return moment(createTime - 0).format('YYYY-MM-DD')
    }
  }

Guess you like

Origin blog.csdn.net/xutongbao/article/details/127844187