Detailed explanation of examples of implementing moment.js
The time and date processing method is used in the project, and only some methods are used. In order not to introduce third-party libraries (moment.js), the methods used in the project are encapsulated here.
To achieve the following functions:
new Moment() // 返回当前的时间对象 new Moment().unix() // 返回当前时间的秒数Moment.unix(timestamp) // 传入秒数,返回传入秒数的时间对象 new Moment().format('YYYY-MM-DD dd HH:mm:ss') // 返回值 2017-06-22 四 19:46:14Moment.unix(1498132062000).format('YYYY-MM-DD dd HH:mm:ss') // 返回值 2017-06-22 四 19:46:14
1. Basic code:
1 class Moment { 2 constructor(arg = new Date().getTime()) { 3 this.date = new Date(arg) 4 } 5 }
arg = new Date().getTime(): Use destructuring here to add a default value to arg
2. Implement the unix method
1 class Moment { 2 constructor(arg = new Date().getTime()) { 3 this.date = new Date(arg) 4 } 5 6 // getTime()返回的是毫秒数,需要转成秒数并取整 7 unix() { 8 return Math.round(this.date.getTime() / 1000) 9 } 10 }
unixMethod: Return the number of seconds of the current time
The getTime() method returns the number of milliseconds that needs to be divided by 1000 and rounded up
3. Implement 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 unixMethod: Parameter timestamp milliseconds Return a Date object
new Date( ) Parameters that can only accept milliseconds need to be multiplied by 1000
IV. Implement format method
1 class Moment { 2 constructor(arg = new Date().getTime()) { 3 this.date = new Date(arg) 4 } 5 6 format(formatStr) { 7 const date = this.date 8 const year = date.getFullYear() 9 const month = date.getMonth() + 1 10 const day = date.getDate() 11 const week = date.getDay() 12 const hour = date.getHours() 13 const minute = date.getMinutes() 14 const second = date.getSeconds() 15 16 return formatStr.replace(/Y{2,4}|M{1,2}|D{1,2}|d{1,4}|H{1,2}|m{1,2}|s{1,2}/g, (match) => { 17 switch (match) { 18 case 'YY': 19 return String(year).slice(-2) 20 case 'YYY': 21 case 'YYYY': 22 return String(year) 23 case 'M': 24 return String(month) 25 case 'MM': 26 return String(month).padStart(2, '0') 27 case 'D': 28 return String(day) 29 case 'DD': 30 return String(day).padStart(2, '0') 31 case 'd': 32 return String(week) 33 case 'dd': 34 return weeks[week] 35 case 'ddd': 36 return '周' + weeks[week] 37 case 'dddd': 38 return '星期' + weeks[week] 39 case 'H': 40 return String(hour) 41 case 'HH': 42 return String(hour).padStart(2, '0') 43 case 'm': 44 return String(minute) 45 case 'mm': 46 return String(minute).padStart(2, '0') 47 case 's': 48 return String(second) 49 case 'ss': 50 return String(second).padStart(2, '0') 51 default: 52 return match 53 } 54 }) 55 } 56 }
format method: Pass Enter 'YYY-MM-DD HH: mm:ss', 'YYY-MM-DD', etc., according to the format you want to obtain (for details, please refer to the format method of moment.js)
The padStart() method adds 0
before the month or date. At this point we have implemented three methods: unix, static unix, and format. You can check github to complete the code. If you need to implement other You can leave me a message about the date and time processing method, and I will slowly improve and add it later.
The above is the detailed content of Detailed explanation of examples of implementing moment.js. For more information, please follow other related articles on the PHP Chinese website!

JavaandJavaScriptaredistinctlanguages:Javaisusedforenterpriseandmobileapps,whileJavaScriptisforinteractivewebpages.1)Javaiscompiled,staticallytyped,andrunsonJVM.2)JavaScriptisinterpreted,dynamicallytyped,andrunsinbrowsersorNode.js.3)JavausesOOPwithcl

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools
