1. Basic knowledge (methods of date objects)π getFullYear() is used to return a 4-digit number representing the year π€£ getMonth() returns a number representing the month, but the return value is an integer between 0 (January) and 11 (December) π getDate() returns a certain day π getHours() returns the hour field of the time π getMinutes() returns the minutes field of the time π getSeconds() returns the seconds of the time. The return value is an integer between 0 and 59 2. Formatting DatesExample: Format the current time (because time is passing, the result will be different!) The code is as follows: Effect presentation: 3. Encapsulation function to format date (for our later use)The code is as follows: Effect presentation: Although this case is relatively simple, there are still some things that need attention! For example, when we get the month, we must add 1, otherwise the returned month will be 1 less than the actual month. This also reminds us to be careful, careful and careful when typing code! Attached is a popular js encapsulation function for formatting date and time:Date.prototype.format = function(fmt){ var o = { "M+" : this.getMonth()+1, //Month "d+" : this.getDate(), //Day "h+" : this.getHours(), //Hours "m+" : this.getMinutes(), //Minutes "s+" : this.getSeconds(), //Seconds "q+" : Math.floor((this.getMonth()+3)/3), //Quarter "S" : this.getMilliseconds() //Milliseconds}; if(/(y+)/.test(fmt)){ fmt = fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); } for(var k in o){ if(new RegExp("("+ k +")").test(fmt)){ fmt = fmt.replace( RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); } } return fmt; } Hereβs how to use it: var now = new Date(); // Usually pass in millisecond timestamp for initialization var nowStr = now.format("yyyy-MM-dd hh:mm:ss"); IV. ConclusionThis concludes this article about JavaScript built-in date and time formatting. For more information about JavaScript built-in time formatting, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to use linux commands to convert and splice audio formats
>>: Tomcat parses XML and creates objects through reflection
This article attempts to write a demo to simulate...
Preface When using the Deepin user interface, it ...
1. Background Although I have read many blogs or ...
This article uses the "Attribution 4.0 Inter...
Start a new project This article mainly records t...
1: Tag selector The tag selector is used for all ...
Background of the problem The server monitoring s...
The database enables slow query logs Modify the c...
Table of contents The first step is to download M...
1.device-width Definition: Defines the screen vis...
Find the problem When we display the contents in ...
In the pages of WEB applications, tables are ofte...
Just pass in custom parameters HTML <div id=&q...
Table of contents 1. Concept of array flattening ...
1. Introduction By enabling the slow query log, M...