Momentjs firstday of week - javascript

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.

Related

Get Day of The Week from Date Range in Javascript

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

How to Get Formatted Date String In React Native

In many apps we can see the date formatting like this.
If it's today, then print hh:mm,
If it's yesterday, then print yesterday,
If it's in this week, then print day of week, (ex, Wednesday)
If it's in this year, then print MM/dd
else print YY/MM/dd
Something like this.
How can I implement in React Native or javascript?
Is there library for this?
Use library momentjs. There is function isSame which compare given date with year,month or day. Like
moment().isSame('2010-01-01', 'year'); // true
moment().isSame('2011-01-01', 'month'); // false, different year
moment().isSame('2010-02-01', 'day'); //
moment().isSame('2010-02-01', 'week'); //
In your case, 1st on
const dateString = moment().isSame('2018-01-01', 'day') ? moment('2018-01-01 5:55').format('hh:mm') : else check for other condition
Better go from year to month to week and week to day.

Momentjs how to get next month last date

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

Check if the input day is Monday with moment.js

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

how can you figure out the week from a single date

I am building a project and in that project the user is going to input a date eg. 8/2/2011. then i am going to show them the information for the week that contains 8/2/2011. How can you figure out which week to show? for this project i am using javascript, jquery, and php.
You should be able to do what you want by getting the first day of the week the given date occurs in.
If your weeks start on Monday:
Date.prototype.lastMonday=function(){
var d= new Date(this), weekstart= 1;
while(d.getDay()!== 1) d.setDate(d.getDate()-1)
return d;
}
alert(new Date().lastMonday())
//returns the current date if does fall on a Monday
If you want to know the days that compose the week you can just find out the day of the week and show the appropriate days before and after. To know the day of the week just use the getDay method of the Date object(see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getDay). For more info on the Date object see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date.

Categories

Resources