JavaScript built-in date and time formatting time example code

JavaScript built-in date and time formatting time example code

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 Dates

Example: 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. Conclusion

This 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:
  • Summary of js formatting time and date functions
  • Various methods of formatting js timestamp into date format
  • javascript format date time function
  • Js gets the current date and time and format code
  • JavaScript date and time formatting method and custom formatting function example
  • Format date and time data function code in js
  • Introduction to various methods of formatting js timestamp into date format
  • js time and date formatting encapsulation function
  • Summary of javascript date and time formatting methods
  • javascript format time date function code 123WORDPRESS.COM revised version

<<:  How to use linux commands to convert and splice audio formats

>>:  Tomcat parses XML and creates objects through reflection

Recommend

Form submission refresh page does not jump source code design

1. Design source code Copy code The code is as fol...

Learn SQL query execution order from scratch

The SQL query statement execution order is as fol...

How to set the width attribute to the style of the span tag

If you directly set the width attribute to the sty...

The role of MySQL 8's new feature window functions

New features in MySQL 8.0 include: Full out-of-th...

How to remove the dotted border when clicking a link in FireFox

I encountered several browser compatibility issue...

Implement full screen and monitor exit full screen in Vue

Table of contents Preface: Implementation steps: ...

How to quickly add columns in MySQL 8.0

Preface: I heard a long time ago that MySQL 8.0 s...

Node uses koa2 to implement a simple JWT authentication method

Introduction to JWT What is JWT The full name is ...

MySQL 8.0 upgrade experience

Table of contents Preface 1. First completely uni...

Linux configuration SSH password-free login "ssh-keygen" basic usage

Table of contents 1 What is SSH 2 Configure SSH p...

Linux unlink function and how to delete files

1. unlink function For hard links, unlink is used...

Should I use Bootstrap or jQuery Mobile for mobile web wap

Solving the problem Bootstrap is a CSS framework ...

JavaScript function call, apply and bind method case study

Summarize 1. Similarities Both can change the int...

How to implement the King of Glory matching personnel loading page with CSS3

Those who have played King of Glory should be fam...