Well, I want to check if a day of date is Monday like:
var myDate = new Date();
moment(myDate, 'DD-MM-YYYY').dayIs('monday')
In my country the first day of week is Monday, so, really I want check if the input date is begin of week.
I try using moment(myDate, 'DD-MM-YYYY').startOf('isoweek') but, this don't work for me.
According to the documentation - Moment.js has a locale setting which should solve this for you;
Day of Week (Locale Aware) 2.1.0+
moment().weekday(Number);
moment().weekday(); // Number
Gets or sets the day of the week according to the locale.
If the locale assigns Monday as the first day of the week, moment().weekday(0) will be Monday. If Sunday is the first day of the week, moment().weekday(0) will be Sunday.
Here you can see how to set the locale;
moment.locale('en'); // set to english
Related
how do I save the day of the week to array from a given date range. For example, from 8/23/2021 to 12/11/2021, I would like to get the dates that are Mondays and save these dates to an array? Thanks
So you can use the getDay method.
Given your date:
var someday = new Date('8/23/2021');
using the getDay against the someday will return the day of the week index. 1 in this case.
Edit (Side-note): Looks like Sunday is the start of the week for JS
We can pass some options to get the proper long day and formatting.
var long_day = new Intl.DateTimeFormat('en-US', { weekday: 'long'}).format(someday);
This will return, Monday. You could save it when its 1/Monday, up to you.
JSFiddle
There is some kind of "campaign duration" which means you are able to choose date between first and last day in month.
If you choose date which is not between first and last day in month, for example some date from next month, the Backend API will return an "Input param error".
if I choose the last date inside date selector(30th day in month), the Backend API will return an "Input param error". That means he count 30th day like 1st day in next month.
I should Change date selection with one day less, to be when I choose 30th day I should before send date by api, discount that number for one.
By the way date is in string format.
I need help how to discount string number for one before sending that date on api?
You can subtract one day from your date.
First you need to create a date from your string:
var dateFromString = '2011-04-11T10:20:30Z';
let newDate = new Date(dateFromString);
and then subtract one day from that date:
newDate.setDate(newDate.getDate() - 1);
You can use momentjs to convert string into date and subtract days as:
selectedEndDate = moment(selectedEndDate, 'DD/M/YYYY').subtract(1, 'day');
i am trying to get the last date of the next month, for some reason momentjs returns the wrong date
For Example :
console.log(moment('02/28/2018', "MM/DD/YYYY").add(i, 'M'));
returns :
moment("2018-03-28T00:00:00.000")
where the right date should be :
moment("2018-03-31T00:00:00.000")
Using .add() you are just adding a month to your actual date but you want also go to last day of this month, and for this you can use .endOf():
moment('02/28/2018', "MM/DD/YYYY").add(1, 'M').endOf("month")
// 2018-03-31T23:59:59.999Z
You are adding 1 month to 2/28, so you get 3/28. It only increases the month part.
To get the last day of the month, you can use:
// Should be 12/31/2018 23:59:59
console.log(moment("2018-12-01").endOf("month").toString());
// Should be last day, hour, minute, and second of the current month
console.log(moment().endOf("month").toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
Another variation of guijob's answer, if you want to get based on current day you can use
moment().add(1, "month").endOf("month")
I am using momentjs. I would like to change the first day of week to Monday and somehow it works.
moment.locale('en', {
week : {
dow : 1 // Monday is the first day of the week
}
});
If I type moment().startOf('week').format('dddd DD-MM-YYYY'), it Shows
"Monday 19-01-2015"
But if I type moment().year(2014).week(4).day(0), it will shows as
"Sunday 18-01-2015"
Anyone knows why it got this issue?
The day function is not locale aware. it always uses Sunday as day 0.
Use the weekday function instead - which is locale aware.
See the docs for day and weekday, which explain this pretty well.
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.