片刻js获得当月的第一天和最后一天 [英] Moment js get first and last day of current month

查看:1026
本文介绍了片刻js获得当月的第一天和最后一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


2016 -09-01 00:00

2016-09-01 00:00

我可以得到这样的当前日期和时间:
moment()。格式('YYYY-MM-DD h:m')将以上述格式输出。

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

但是我需要获取当月第一天和最后一天的日期和时间,是否可以这样做?

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

编辑:
我的问题与这个不同,因为要求对于用户已经拥有的给定月份,我要求当前月份,同时要求提供另一个未提及的日期的特定格式,即所谓的重复。

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'.

推荐答案

如果有人错过了对原始问题的评论,y你可以使用内置方法(从时刻1.7开始工作):

In case anyone missed the comments on the original question, you can use built-in methods (works as of 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');

这篇关于片刻js获得当月的第一天和最后一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆