Quantcast
Channel: User Hamfri - Stack Overflow
Viewing all articles
Browse latest Browse all 42

Answer by Hamfri for How to get the first date of the current month in Node.js?

$
0
0

Create a new file dates.js and add the following code. To execute this code run the command node dates.js from your terminal. You can use this function to get first and last days dates in a given month. I tested this on node 12

const getDays = () => {    const date = new Date();    const year = date.getFullYear();    let month = date.getMonth() + 1;    let f = new Date(year, month, 1).getDate();    let l = new Date(year, month, 0).getDate();    f = f < 10 ? '0'+f : f;    l = l < 10 ? '0'+l : l;    month = month < 10 ? '0'+month : month;    const firstDay = new Date(`${year}-${month}-${f}`);    const lastDay = new Date(`${year}-${month}-${l}`);    console.log({"firstDay": firstDay,"lastDay": lastDay    });};getDays();

Viewing all articles
Browse latest Browse all 42

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>