moment.js time library

First, the concept:     https://www.cnblogs.com/Jimc/p/10591580.html     or     http://momentjs.cn/ (official website)

  1, Moment.js time is a lightweight JavaScript library that facilitates the development of daily operations on time and improve development efficiency.

Two, API: daily development usually have time to following these operations: for example, acquisition time, set the time, time format, more time and more.

  1, the introduction of moment.js file:

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

// import 方式
import moment from 'moment'; 

  2, the time zone settings moment: It seems that China's default time zone. It is generally not alone to set.

import 'time / local / zh-cn' 
moment.locale ( 'zh-cn');   

  3, create a moment of time objects: moment is like the Date object. A moment of time the object is a target . However, unlike native Date object, the object is the time elapsed package.

    a, time objects are a complete time stamp, the time stamp of native Date object's time is generally fixed, it will not change, but the moment the timestamp time object by calling the API will change (ie, change the time) .

Moment ()    // which can pass parameters to create objects specified time period. Note that a moment of time objects, can only be performed once moment (). Like native Date, can only be new once. Otherwise, a new one is executed each time the object.

    b, moment target time, a time point the object is variable. This Date and time the object is not the same. So do not put a moment object assigned to a variable, the variable is set at this time.

let test = moment()   // 创建moment时间对象。
console.log(test.format('YYYY-MM-DD hh:mm:ss a'))  //  2019-11-26 09:34:19 pm
test.add(1,'days')
console.log(test.format('YYYY-MM-DD hh:mm:ss a'))  //  2019-11-27 09:34:19 pm

    Therefore, by using the moment of time of the object, generally called directly moment () generated in time of the object, followed by the corresponding method. Such as: The following are date1 with respect to time, but can not operate directly on date1, otherwise date1 time will be changed.

date1 = Moment the let ()   // present time target 
the let DATE2 = Moment (). Subtract (. 1, 'Days')   // time subject the day before 
the let DATE2 = Moment (). the Add (. 1, 'Days')     / / time the object after day

  4, acquisition time information of the object: moment of time object provides a variety of objects to get the next time this time. Such as: object when obtaining a time, minutes, seconds and so on.

     Providing access time moment information of the object find many interfaces, specific reference may be public or  https://www.cnblogs.com/Jimc/p/10591580.html

  5, set the time: the object is based on a time, a time into another object, the relative time. For example, the time an object (relative to the current time) one day before, a time of day and other objects.

= Moment DATE2 the let (). Subtract (. 1, 'Days')   // time of the object before the day than Moment () the target time 
the let DATE2 = Moment (). the Add (. 1, 'Days')        // day later time target

  6、

 

 

Guess you like

Origin www.cnblogs.com/wfblog/p/11938657.html