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

What is the difference between width property 100% and auto in css?

 width:auto: The default width value of block-level elements. When set to this value, the browser will automatically select an appropriate width value to adapt to the width of the parent element. When the width is set to 100%, the width of the child element box The value is equal to the parent's content, and as the parent's content automatically changes, after adding the padding and margin of the child element, its width remains unchanged, which is the difference from setting it to auto. But we most often use width:auto, because it is more flexible, width:100% is used less, because when adding padding or margin, this method is easy to make it exceed the parent box and destroy the original layout.

Access to XMLHttpRequest at 'http://localhost:8080/xxx' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 Due to browser same-origin policy restrictions. The Same Origin Policy is a convention. This is the main and essential security feature of the browser. Without the same-origin policy, normal browser functionality may be affected. The web is built on the same-origin policy, and browsers are just one implementation of the same-origin policy. The Same Origin Policy prevents JavaScript scripts from one domain from interacting with content from another domain. The so-called same origin (that is, the same domain) means that two pages have the same protocol, host and port.