9

I'm trying to get the first and last date of the current month using Node.js.

Following code is working perfectly in browser (Chrome):

var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);

console.log(firstDay);
console.log(lastDay);

But it is showing a different result in Node.js. How can I fix it?

Badacadabra
  • 8,043
  • 7
  • 28
  • 49
sijo vijayan
  • 1,590
  • 1
  • 21
  • 34
  • 2
    what are the different results? post results for browser/nodejs – shennan Dec 09 '15 at 10:14
  • 1
    [This](http://stackoverflow.com/questions/7481963/how-come-my-javascript-node-js-is-giving-me-the-incorrect-timestamp) may help. – Jacques Marais Dec 09 '15 at 10:17
  • In browser Tue Dec 01 2015 00:00:00 GMT+0530 (IST)Thu Dec 31 2015 00:00:00 GMT+0530 (IST) In node js 2015-11-30T18:30:00.000Z - 2015-12-30T18:30:00.000Z – sijo vijayan Dec 09 '15 at 10:19

4 Answers4

6

Changing the native Date object in the accepted answer is bad practice; don't do that ( https://stackoverflow.com/a/8859896/3929494 )

You should use moment.js to give you a consistent environment for handling dates in JavaScript between node.js and all browsers - see it as an abstraction layer. http://momentjs.com/ - it's quite easy to use.

A very similar example is here: https://stackoverflow.com/a/26131085/3929494

You can try it on-line at https://tonicdev.com/jadaradix/momentjs

Community
  • 1
  • 1
Jxx
  • 1,014
  • 10
  • 9
  • I don't think anybody *needs* to use moment.js to get consistency in Date output between two JS environments. You've not explained here why the OP has a difference in date output. – shennan Dec 09 '15 at 10:51
  • I've changed "need" to "should". Using methods like `endOf` will void a lot of boilerplate. Older browsers also make mistakes with dates which a library like moment.js will take care of. It's a clear win when you have to deal with internationalisation as well. I don't know why there is a difference; I think my point is that using moment.js would remove all possible differences. – Jxx Dec 09 '15 at 10:56
  • 1
    I am not discrediting moment.js as a good abstraction, I'm just saying that moment.js itself is not an answer to the specific issue. It may help as a blanket problem-solver but it doesn't educate anybody on the differences between NodeJS and browser time keeping. I'll +1 as it's better than the current reigning answer. – shennan Dec 09 '15 at 11:03
0

The browser output is showing the date in the current time zone, node.js is showing the date GMT / Zulu time zone.

(Edit: Code added). Something like this

var offset = (new Date().getTimezoneOffset() / 60) * -1;
var d = new Date();
var tmpDate = new Date(d.getTime()+offset);
var y = tmpDate.getFullYear();
var m = tmpDate.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);

console.log(tmpDate.toString());
console.log(firstDay.toString());
console.log(lastDay.toString());
RobertKenny
  • 3,556
  • 2
  • 19
  • 17
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();
Hamfri
  • 1,979
  • 24
  • 28
-2

Look at the code

<html>
<head>
    <title>Please Rate if it helps</title>
    <script>
        Date.prototype.getMonthStartEnd = function (start) {
            var StartDate = new Date(this.getFullYear(), this.getMonth(), 1);
            var EndDate = new Date(this.getFullYear(), this.getMonth() + 1, 0);
            return [StartDate, EndDate];
        }
        window.onload = function () {
            document.write(new Date().getMonthStartEnd());
        }
    </script>
</head>
<body>
</body>
</html>
Parth Patel
  • 774
  • 1
  • 8
  • 15