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

时间:2021-10-27 21:28:51

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 this one because that asks for a given month that the user already has whereas I am asking for the current month along with asking for a specific format of the date that is not mentioned in the other so called 'duplicate'.

编辑:我的问题与此问题不同,因为它要求用户已经拥有的给定月份,而我要求当前月份以及要求特定格式的日期在另一个中没有提到所谓的'重复”。

4 个解决方案

#1


21  

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

#2


117  

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

#3


18  

There would be another way to do this:

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

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

#4


5  

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')}

#1


21  

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

#2


117  

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

#3


18  

There would be another way to do this:

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

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

#4


5  

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')}