Javascript 时刻 js 获得当月的第一天和最后一天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39267623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:24:00  来源:igfitidea点击:

Moment js get first and last day of current month

javascriptdatemomentjs

提问by user3574492

How do I get the first and last day and time of the current month in the following format in moment.js:

如何在 moment.js 中以以下格式获取当月的第一天和最后一天以及时间:

2016-09-01 00:00

2016-09-01 00:00

I can get the current date and time like this: moment().format('YYYY-MM-DD h:m')which will output in the above format.

我可以像这样获取当前日期和时间: moment().format('YYYY-MM-DD h:m')它将以上述格式输出。

However I need to get the date and time of the first and last day of the current month, any way to do this?

但是我需要获取当月第一天和最后一天的日期和时间,有什么办法吗?

EDIT:My question is different from thisone because that asks for a given month that the user already has whereas I am asking for the currentmonth along with asking for a specific format of the date that is not mentioned in the other so called 'duplicate'.

编辑:我的问题与不同,因为它要求用户已经拥有的给定月份,而我要求的是当前月份以及其他所谓的“重复”中未提及的特定日期格式'。

回答by Ali Yazdani

In case anyone missed the comments on the original question, you can use built-in methods (works as of Moment 1.7):

如果有人错过了对原始问题的评论,您可以使用内置方法(适用于 Moment 1.7):

const startOfMonth = moment().startOf('month').format('YYYY-MM-DD hh:mm');
const endOfMonth   = moment().endOf('month').format('YYYY-MM-DD hh:mm');

回答by Akoya

There would be another way to do this:

还有另一种方法可以做到这一点:

var begin = moment().format("YYYY-MM-01");
var end = moment().format("YYYY-MM-") + moment().daysInMonth();

回答by Kevin Grosgojat

You can do this without moment.js

你可以在没有 moment.js 的情况下做到这一点

A way to do this in native Javascript code :

一种在本机 Javascript 代码中执行此操作的方法:

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

firstDay = moment(firstDay).format(yourFormat);
lastDay = moment(lastDay).format(yourFormat);

回答by Alexander Nana Owusu

Assuming you are using a Date range Picker to retrieve the dates. You could do something like to to get what you want.

假设您使用日期范围选择器来检索日期。你可以做一些类似的事情来得到你想要的。

$('#daterange-btn').daterangepicker({
            ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            },
            startDate: moment().subtract(29, 'days'),
            endDate: moment()
        }, function (start, end) {
      alert( 'Date is between' + start.format('YYYY-MM-DD h:m') + 'and' + end.format('YYYY-MM-DD h:m')}

回答by ifelse.codes

moment startOf()and endOf()is the answer you are searching for.. For Example:-

时刻startOf()endOf()是您正在寻找的答案.. 例如:-

moment().startOf('year');    // set to January 1st, 12:00 am this year
moment().startOf('month');   // set to the first of this month, 12:00 am
moment().startOf('week');    // set to the first day of this week, 12:00 am
moment().startOf('day');     // set to 12:00 am today

回答by Jojo Joseph

First and Last Date of current Month In the moment.js

当月的第一个和最后一个日期在 moment.js

console.log("current month first date");
    const firstdate = moment().startOf('month').format('DD-MM-YYYY');
console.log(firstdate);

console.log("current month last date");
    const lastdate=moment().endOf('month').format("DD-MM-YYYY"); 
console.log(lastdate); 

回答by Mo.

As simple we can use daysInMonth()and endOf()

很简单,我们可以使用daysInMonth()endOf()

const firstDay = moment('2016-09-15 00:00', 'YYYY-MM-DD h:m').startOf('month').format('D')
const lastDay = moment('2016-09-15 00:00', 'YYYY-MM-DD h:m').endOf('month').format('D')