2011-09-13 is today (GMT+0)
Date.UTC(<%= effort.week_commencing.strftime("%Y,%m,%d") %>)
Outputs
Date.UTC(2011,09,12)
This is right because it is getting the 12Th which is the start of the month.
But in high charts / stock charts it is displaying 1 month ahead "12Th of October"
What could be the problem?
Months for Date are enumerated from 0. So Date.UTC(2011,09,12) is really 12th of October and Date.UTC(2011,08,12) will be 12th of September. Just subtract 1 from month value.
try new Date().getMonth() you will see that it returns 8 instead of 9 (September), this is because in javascript months are zero-based-numbered
Related
This should return the last week of the year:
moment().year('2021').week(51).day('monday').format('YYYY-MM-DD');
But instead it is returning 2022-12-12. I think there is a bug in moment.js.
Here is codepen: https://jsfiddle.net/5402bkmp/
You should post your code here, not elsewhere.
var now = moment().year('2021').week(51).day('monday').format('YYYY-MM-DD');
console.log(now.toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Breaking down the code, if run on Monday 27 December:
moment()
Creates a moment object for 27 Dec 2021
.year('2021')
Sets the year to 2021, which changes nothing because it's already set to 2021. It also handles cases like 2020-02-29 + 1 year, which becomes 2021-02-28.
.week(51)
Sets to "localised" start of week 51. The problem is, how does the user know how moment.js localises things? For me it seems to be Sun 12 Dec 2021. That numbering seems to be based on the first week starting on the first Sunday on or before 1 Jan 2021 (i.e. Sun 27 Dec 2020), e.g. new Date(2020, 11, 27 + 50*7) gives 12 Dec 2021.
.day('monday')
Sets the date to Monday of the same localised week, again it's hard for users to know what their "localised" week is. For me, it just keeps it as Monday because it seems the localised week starts on Sunday (my PC is set to start weeks on Monday).
.format('YYYY-MM-DD')
So I think it's clear that a problem with using the week method is that neither the programmer nor user know what the result will be because they don't know what moment.js is using to localise things (possibly navigator.language). And results can be very different to what is expected.
One fix, as Sergiu suggested, is to use isoWeek so at least the result is consistent and predictable. ISO weeks start on Monday, with the first week being the one with the most days in the subject year. It's also expressed as the week of the first Thursday, or the week of 4 January, they all work to give the same Monday as the start of week 1 of any particular year. Some years have 52 weeks, some 53, and usually a couple of days near the end of the year are part the first week of the following year or last week of the previous year.
You might also like to see Get week of year in JavaScript like in PHP.
You need to use .isoWeek instead of .week (documented here, though it's unclear to me why).
That's works really good to me!
moment.locale("myLanguage", { week: { dow: 0 }});
momentExt.updateLocale("myLanguage", { week: { dow: 0 }});
Example here: https://jsfiddle.net/naqr7upL/
If I'm executing this:
var date = new Date("10.31");
date.setFullYear(-125);
the output of date is Sun Oct 31 -125 00:00:00 GMT+0200 (W. Europe Summer Time)
If I check this on wolframalpha the day seems to be tuesday.
Can someone explain why not the same day is displayed by both source?
The reason for the difference between JavaScript and wolframalpha website is that JavaScript is calculating the years mathematically, so it includes the year zero. Try to set the year to zero in JavaScript and you will see that it works. However, there is no such a thing as year zero, and the year before year 1 is year 1 BC. Try to set the year to zero on wolframalpha website and you get an error, while it automatically converts all negative years to years BC. This is the correct behavior.
To get the BC years in JavaScript, add 1 to every year below 1. So year 0 becomes 1BC, and year -125 becomes 126BC. In JavaScript this gives you Sunday, and 126BC on wolframalpha website gives you Sunday too. 125BC gives you Tuesday on wolframalpha website, and -124 gives you the same in JavaScript.
var date = new Date();
date.setFullYear(-124);
date.setMonth(9);
date.setDate(31);
console.log(date.toString());
date.setFullYear(-125);
console.log(date.toString());
negative years in javascript do produce a BC date but it's kind of a poor design. Wolfram Alpha is probably correct. See this answer for more: Why does Date accept negative values?
Javascript dates start in 1970.
Let's do a quick count.
(new Date()).setYear(-125); //returns -66085584766591 (milliseconds from time 0)
//Let's convert those milliseconds in years...
//-66085584766591 = -2095,56 (years???)
As you can see, you can't rely on negative dates in Javascript.
This question already has answers here:
`date.setMonth` causes the month to be set too high if `date` is at the end of the month
(8 answers)
Closed 5 years ago.
Please someone tell me why the month gets increased by 1 if its value is higher than 9. The code is below
var dd = new Date();
dd.setFullYear(2017);
dd.setMonth(7);
console.log("Month(Expecting 7, and received 7) = " + dd.getMonth());
dd.setMonth(10);
console.log("Month(Expecting 10, and received 11) = " + dd.getMonth());
Fiddler code at here - https://jsfiddle.net/vzmtp3ua/
Because 31 days do not exist in every month so the extra days are added to the next month.
Set Date as Oct 31, add a month so it would be November 31st since there are 30 days the date is moved to December 1st.
In some months there are less days then 31 , So extra days are added to the following month
It has to do with the number of days in that month making the date shift to the first day in the next month.
The documentation mentions this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setMonth
The current day of month will have an impact on the behaviour of this method. Conceptually it will add the number of days given by the current day of the month to the 1st day of the new month specified as the parameter, to return the new date. For example, if the current value is 31st August 2016, calling setMonth with a value of 1 will return 2nd March 2016. This is because in 2016 February had 29 days.
Not all the months have same number of days (31) so next one will be moved to next month
When you create the Date object on 31th of October and then set the month to November (= 10!), which does not have 31 days, it will switch to the 1st of next month (December 1st).
It's dependent on current date. You initialize the date to "now" (new Date()), and today is Oct 31st.
If the day on your Date object is 31st and you call setMonth(), extra days will get carried on to the next month for months shorter than 31 days. If you try to setMonth(1) it will mean Feb 31st, so you get Mar 3rd.
To avoid the problem you can pass second argument the setMonth, which is the day to be set, e.g. dd.setMonth(10,30) (Nov 30).
am a new for moment.js
i want 3 type of outputs
Get all months from a year
get all weeks from a month in Moment js
get all days from a week
How can i do this using 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.
This problem also accrued when i use get days and weeks
How can i solve this problem in 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.
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).format("LLLL"); //result Tuesday, August 27, 2182 ...
Now read a bit about year last week here variations between (52/53).
Now the solution for your problem,
get all months of the year, dude seriously (12 months) anyway:
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.
I'm working on a jQuery credit card expiration date validation script. Credit cards expire after the last day of the expiration month. For instance, if the card expires on 8/2013 then it's good through 8/31/2013.
In the past on the server side I've determined the last day of the month by adding 1 to the current month, then subtracting 1 day.
Today I noticed that when creating a new date, if 0 is applied to the 3rd parameter of the JavaScript Date() object, the resulting date will be the end-of-month day. But I've been unable to locate any online documentation to affirm this observation.
Here is some sample code.
var month = 10;
var year = 2013;
var expires = new Date(year, month, 0);
alert(expires);
And here is a jsFiddle example that I created.
This is a bit confusing, because I thought in JavaScript months were zero based. I've tested this in Chrome, Firefox, IE, and Safari, and the behavior appears consistent. The returned date consistently displays the last day of the month. This looks like a lucky find, but I'd really like to understand what is happening here.
Am I safe to run with this approach to assigning an end of month date, and if so is there some online documentation that I can point to which affirms this? Thanks.
Months are zero-based. That creates an end-of-month date in the previous month. Month 10 is November, so creating a date with day 0 in November gives you the end of October (month 9).
That is, day 0 in November means "the day before 1 November", which is the last day of October. Day -1 in November would be 30 October.