moment.js of Methods

A very useful summary of the date tools moment.js, the date of acquisition, formatting, etc.
Were previously encountered in the work, I will create a new class of tools to use when a js, encountered in the work to try to show anything in this file, so this is what I encountered in the work order and then try js file the order, the order may be a little messy Some, but generally no problem.

The introduction of moment

//require 方式
var moment=require('moment'); //import 方式 import moment from 'moment'; 

Set moment for the Chinese region

//require 方式
require('moment/locale/zh-cn') moment.locale('zh-cn'); //import 方式 import 'moment/locale/zh-cn' moment.locale('zh-cn'); 

Formatting time type

1. Take the time of day, month displayed in the form of DD MM YYYY date of

var now=moment().format("YYYY年MM月DD日"); 

2. any timestamp format to YYYY-MM-DD HH: mm: ss display form

 var t1=moment(1411641720000).format('YYYY-MM-DD HH:mm:ss'); 

The day before the acquisition date format to YYYY-MM-DD form

var t11=moment().day(0).format('YYYY-MM-DD'); 

Gets Friday date format to YYYY-MM-DD form

var t12=moment().weekday(5).format('YYYY-MM-DD'); 

Acquisition date Friday, format to YYYY-MM-DD form

var t13=moment().weekday(-3).format('YYYY-MM-DD'); 

Can be simply understood as the penultimate days last week, last week, the countdown is independent of the third day on Friday, the day and date

Get the current year, month, date

var t14=moment().year() var t15=moment().month()//此处月份从0开始,当前月要+1 var t16=moment().date(); 

Note that this place, date not .day () / days ()

Combined t14, t15, t16 and the current can be output to any date you want, the month of the date of the relevant
example: I want to get a full day's date last year, such as: Today is 2018-7-23, I want to output a 2017- 7-23

console.log(`${t14-1}-${t15+1}-${t16}`) 

Of course, this is not the best way to get today's date last year, but you can spell many combinations you want, will be introduced following last year's acquisition method better today.

Last month, get today's date format to YYYY-MM-DD

var t18=moment().subtract(1, 'months').format('YYYY-MM-DD'); 

Last month acquisition date format to YYYY-MM

var t19=moment().subtract(1, 'months').format('YYYY-MM') 

One day before the acquisition date format to YYYY-MM-DD

var t20 = moment().subtract(1, 'days').format('YYYY-MM-DD'); 

Acquired last year, today's date format to YYYY-MM-DD, that is, simple method to obtain today's date last year

var t21= moment().subtract(1, 'year').format('YYYY-MM-DD'); 

After the acquisition time of two hours

var t22=moment().add(2,'hours').format('YYYY-MM-DD HH:mm:ss'); 

This application is to obtain a time stamp expiration time
comparison is also very simple, just get the current time, with the same format> <= number comparing it

Five days of the date of acquisition

For example: Today 2018-7-23, acquired time 2018-7-18

var t23=moment().subtract(5, 'days').format('YYYY-MM-DD');

Guess you like

Origin www.cnblogs.com/vicky-li/p/11469104.html