简体   繁体   English

如何在 Node.js 中获取当月的第一个日期?

[英]How to get the first date of the current month in Node.js?

I'm trying to get the first and last date of the current month using Node.js.我正在尝试使用 Node.js 获取当月的第一个和最后一个日期。

Following code is working perfectly in browser (Chrome):以下代码在浏览器(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.但它在 Node.js 中显示出不同的结果。 How can I fix it?我该如何解决?

Changing the native Date object in the accepted answer is bad practice;在接受的答案中更改本机Date对象是不好的做法; don't do that ( https://stackoverflow.com/a/8859896/3929494 )不要这样做( 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.您应该使用 moment.js 为您提供一个一致的环境,用于在 node.js 和所有浏览器之间处理 JavaScript 中的日期 - 将其视为一个抽象层。 http://momentjs.com/ - it's quite easy to use. http://momentjs.com/ - 它很容易使用。

A very similar example is here: https://stackoverflow.com/a/26131085/3929494一个非常相似的例子在这里: https : //stackoverflow.com/a/26131085/3929494

You can try it on-line at https://tonicdev.com/jadaradix/momentjs您可以在https://tonicdev.com/jadaradix/momentjs在线试用

The browser output is showing the date in the current time zone, node.js is showing the date GMT / Zulu time zone.浏览器输出显示当前时区的日期,node.js 显示日期 GMT / Zulu 时区。

(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());

Create a new file dates.js and add the following code.创建一个新文件dates.js并添加以下代码。 To execute this code run the command node dates.js from your terminal.要执行此代码,从终端运行命令节点dates.js You can use this function to get first and last days dates in a given month.您可以使用此函数获取给定月份的第一天和最后一天的日期。 I tested this on node 12我在节点 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();

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM