How to calculate no of days based on dates - javascript

so this is regarding the picture below where the flights are flying from origin and destination. What I am unable to understand is how the days are calculated, meaning how we are saying that a flight is flying on Monday or Tuesday or Web etc based on the picture below.

Have you even tried googling?
The DateTime class has a format setting for getting the Day of the Week:
(Google Search: "get the weekday from date c#")
https://learn.microsoft.com/en-us/dotnet/standard/base-types/how-to-extract-the-day-of-the-week-from-a-specific-date
If its because you mean to calculate it in general, then:
(Google Search: "calculate the day of the week for any date")
https://www.almanac.com/how-find-day-week

You can use the following SQL query to return the day of the week:
SELECT DATENAME(WEEKDAY, GETDATE());
Syntax: DATENAME(interval, date)
It will return the day of the week written out.

If you are simply trying to determine how many days between the dates you can use the SQL DATEDIFF function:
SELECT DATEDIFF(day, '2017/08/25', '2011/08/25');
Syntax: DATEDIFF(interval, date1, date2)

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

Issue with date-picker in angular

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');

Moment.js week number differs from MySQL

From the docs for Moment.js, it says moment().startOf('isoWeek') sets to the first day of the week according to ISO 8601.
From the wikipedia page for ISO 8601, it says the first week is (among other equivalent definitions): the first week with the majority (four or more) of its days in the starting year.
From the docs for MySQL, it says WEEK(date, 3) gives a week (1-53) with Monday as the first day of the first week with 4 or more days this year.
From what I can tell, these are all equivalent definitions of a week, yet they don't mesh.
My MySQL database (v5.6.17) gives me 201616 for SELECT YEARWEEK('2016-4-20',3)
Using Moment.js (v2.10.3) in the browser gives me Mon Apr 11 2016 for moment().year(2016).week(16).startOf("isoweek")
Those aren't the same week, and I can't figure out why.
When you call .week() you are setting the "locale" week (I have no idea how moment.js works that out), but likely the first day will be Sunday. When you then call .startOf('isoweek') you are then getting the ISO start of that week, which is the previous Monday.
If you want to use ISO, you have to use ISO methods, so:
moment().year(2016).isoWeek(16).startOf('isoweek');
creates a moment object for 2016-04-18T00:00:00
Or use a string:
moment('2016-W16') // 2016-04-18T00:00:00
which defaults to ISO week numbering and start of week.

Bootstrap datetime picker picking day before selected

I am using this bootstrap datetime picker. I noticed that when I choose a day and convert the milliseconds using var d1 = new Date(milliseconds); it is converted into the day before my selected day. Is there a particular reason for this?
Example:
I select Tuesday, October 1st:
I log the date object after it is converted:
You must convert it into a Unix timestamp , which is a better way of tracking date/time.
Use new Date('your_date_string').getTime() / 1000 which gives you the timestamp or using PHP (strtotime) .
The date object that is being logged for you is probably coming from your system/browser settings(local).
Do not use JavaScript date and time calculations in web applications unless you ABSOLUTELY have to.
While you have the timestamp, cross-check if you are getting the correct time.

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