Skip to main content

The use of date functions in Js and the formatting of dates

 In daily coding, date data is often formatted, and Date objects are used to process dates and times. This article first introduces the Date object, and then deepens its understanding and practical application through a small case of formatting a date, including function use cases such as year, month, time, etc., friends who need it can read and refer to it.

Several ways to create Date objects

   let date=new Date();
        //
        date=new Date("may 18,2022 11:12:12");
        //
        date=new Date("may 19,2022 11")//Thu May 19 2022 00:00:00
        //
        date=new Date("may 19,2022 11:")//Thu May 19 2022 11:00:00
        //
        date=new Date(2022,12,30,11,20,20)//Mon Jan 30 2023 11:20:20
        //
        date=new Date(2022,08,05,20)//Mon Sep 05 2022 20:00:00
        //
        date=new Date(100002220000)
        console.log(date);
Common methods of Date objects
  let date=new Date(2333333333333);//Thu Dec 10 2043 12:08:53
        //
        date=date.getYear();//143
        //
        date=date.getFullYear();//2043
        //
        date=date.getMonth();//11
        //
        date=date.getDate();//10
        //
        date=date.getDay();//4
        //
        date=date.getHours();//12
        //
        date=date.getMinutes();//8
        //
        date=date.getSeconds();//53
        //
        date=date.getMilliseconds();//333
        //
        date=date.toLocaleDateString();//2043/12/10
        //
        date=date.toLocaleString();//2043/12/10 PM12:08:53
        //
        date=date.toLocaleTimeString();//PM12:08:53
       
        console.log(date);
format Date
let date=new Date(2333333333333);
        function formatDate(d){
            let year=date.getFullYear();
            let month=date.getMonth()+1;
            let day=date.getDate();
            let hour=date.getHours();
            let minute=date.getMinutes();
            let second=date.getSeconds();
            return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
        }
        console.log(formatDate(date));//2043-12-10 12:8:53
Note: The return value for the month is 0-11.

Comments

Popular posts from this blog

Use of js array filter() method

 Arrays are frequently used in development, and processing data in arrays is one of the more common and important operations. Therefore, processing data in arrays during development is an important skill. Every developer Everyone should master the operations of arrays, especially for junior developers who have just entered the industry, so be sure to master the relevant skills. This article mainly shares some operations to filter the data in the array, about the use of the filter() method.

vue routing global guard beforeEach and afterEach

 Global routing front guard (beforeEach) This function is used the most. Its function is to perform permission-related verification before routing jumps. This function contains three parameters: to: the object of the target route that is about to enter; from: the route that the current route is leaving; next: confirm the release. It can be used to log in and register, to determine whether there is a token before logging in, and release if it exists. , if it does not exist, it will not be released. The post routing guard (afterEach), its role is to trigger after the routing jump.