简体   繁体   English

Isuue使用Moment js获得一年中的所有月份并获得一个月中的所有周

[英]Isuue for get all months from a year and get all weeks from a month using Moment js

am a new for moment.js 是moment.js的新功能

i want 3 type of outputs 我想要3种类型的输出

  • Get all months from a year 从一年中获取所有月份

  • get all weeks from a month in Moment js 从Moment js获得一个月的所有星期

  • get all days from a week 从一周中获得所有天数

How can i do this using moment.js? 我如何使用moment.js做到这一点?

I have tried for get all month 我已经尝试了一个月

moment().months(2011);// it's working for my year is 2011

but i have only last 2 digits of year. 但是我只有最后两位数字。

moment().months(11);// It's give wrong values or my year is 11

I also read the document, they said 他们还说我也看过文件

moment().months() Accepts numbers from 0 to 11. If the range is exceeded, it will bubble up to the year. moment().months()接受0到11之间的数字。如果超出范围,它将冒泡至年份。

This problem also accrued when i use get days and weeks 当我使用几天或几周时也会出现此问题

How can i solve this problem in moment.js? 如何在moment.js中解决此问题?

Use a date as parameter (12-12-2011) find year month, month week, and week day numerical values, if you need words (Monday, December) values just use format of moment.js and translations. 使用日期作为参数(12-12-2011)查找年月,月周和星期几的数值,如果您需要单词(星期一,十二月)的值,则仅使用moment.js和翻译的格式。

A bit incorrect what you do with moment().months(2011) - moment() return date time now, months(value) add value months to your moment check and see: 您使用moment()。months(2011)所做的操作有点不正确-现在moment()返回日期时间,months(value)将值month添加到您的矩检查中,然后看到:

moment().months(2011).format("LLLL"); //result Tuesday, August 27, 2182 ...

Now read a bit about year last week here variations between (52/53). 现在,请阅读有关上周的年份, 此处 (52/53)之间的差异。

Now the solution for your problem, 现在解决您的问题,

get all months of the year, dude seriously (12 months) anyway: 得到一年中的所有月份,认真对待(12个月):

 moment("12-26-2011", "MM-DD-YYYY").month() + 1; 

get weeks of the year not of the month (you will be confuzed using week of month) 获得一年中的某周而不是该月中的某周(您将使用一周中的某周被混淆)

 moment("12-26-2011", "MM-DD-YYYY").week();

or try this (month week): 或尝试以下操作(每月一周):

var curr_month = moment("12-26-2011", "MM-DD-YYYY").month(); 
var prev_month_last_week = moment("01-01-2011", "MM-DD-YYYY").add(curr_month -1, "month").week();
var your_week_per_month = moment("12-26-2011", "MM-DD-YYYY").week() - prev_month_last_week; //from 1 to 4:

Day of the week: 一周中的天:

moment("12-26-2011", "MM-DD-YYYY").day();

Hope it's helpful. 希望对您有所帮助。

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

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