Moment.js for a simple version

Source: Internet
Author: User

GitHub Source Address: www.baidu.com

Easy to pity White

The project uses a time-date approach, using only a subset of methods, in order not to introduce a third-party library (Moment.js), which encapsulates the methods used in the project.

To implement the following features:

New Moment () // returns the current time object New moment (). UNIX () // returns the number of seconds in the current time Moment.unix (timestamp) // The number of seconds passed in, returning the time object passed in seconds New Moment (). Format (' Yyyy-mm-dd DD HH:mm:ss ')//  return value 2017-06-22 four 19:46:14  Moment.unix (1498132062000). Format (' Yyyy-mm-dd DD HH:mm:ss ')//  return value 2017-06-224 19:46:14

 

First, the base code:

1 class Moment {2     New Date (). GetTime ()) {3This         new  Date (ARG)4    }5 }

  arg = new Date (). GetTime () : Use deconstruction here to add a default value to arg

Second, the realization of UNIX methods

class Moment {    new  date (). GetTime ()) {        thisnew  Date (ARG)    }     // GetTime () returns the number of milliseconds that need to be converted into seconds and rounded     Unix () {        return math.round (this. Date.gettime ()/+)    }} 

Unix method: Returns the number of seconds in the current time

Third, implement the static Unix method

 1  class moment { 2  constructor (arg = new   Date (). GetTime ()) { 3  this . Date = new   Date (ARG)  4  }  5   6   static UNIX (timestamp) { 7  
   
    return  
    new  Moment (timestamp * 1000
    "  
    8  
    }  
    9 } 
   

Static Unix Method: parameter timestamp number of milliseconds returns a Date object

  

Iv. implementation of the Format method

class Moment {constructor (arg=NewDate (). GetTime ()) {         This. Date =NewDate (ARG)} format (FORMATSTR) {const Date= This. Date Const Year=date.getfullyear () const month= Date.getmonth () + 1Const Day=date.getdate () const Week=date.getday () const hour=date.gethours () const minute=date.getminutes () const second=date.getseconds ()returnFormatstr.replace (/y{2,4}| m{1,2}| d{1,2}|d{1,4}| H{1,2}|m{1,2}|s{1,2}/g, (match) = {            Switch(match) { Case' YY ':                returnString (year). Slice (-2)             Case' YYY ':             Case' YYYY ':                returnString (year) CaseM:                returnString (month) Case' MM ':                returnString (month). Padstart (2, ' 0 ')             CaseD:                returnString (Day) Case' DD ':                returnString (Day). Padstart (2, ' 0 ')             Case' d ':                returnString (week) Case' DD ':                returnWeeks[week] Case' DDD ':                return' Week ' +Weeks[week] Case' dddd ':                return' Week ' +Weeks[week] CaseH:                returnString (hour) Case' HH ':                returnString (Hour). Padstart (2, ' 0 ')             Case' m ':                returnString (minute) Case' mm ':                returnString (minute). Padstart (2, ' 0 ')             Case' s ':                returnString (second) Case' SS ':                returnString (second). Padstart (2, ' 0 ')            default:                returnMatch}}) }}

Moment.js for a simple version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.