How to get remaining days from dayjs when getting months - javascript

So I'm having trouble finding a solution to this online, and if there's one thing I hate; it's working with dates.
I need to be able to calculate a person's age in months and days. So if they were born July 3rd, 2020 I need something like 2 months, 21 days.
I have the code for the months here: return dayjs(dayjs()).diff(value, 'month'); where value is the date, but am struggling to think of a way to get those days to be accurate.
Anything would be great! Thank you.

I would suggest that you subtract the the numbers that represent the days in your dates from one another and take the absolute value.
In your example, with the dates July 3rd, 2020 and September 24th, 2020, this would mean doing 3-24 = -21, which would be 21 after taking the absolute value.
Note that your problem is sort of ill-posed. It doesn't really make sense to give someone's age in months, as the amount of days change in a given month. But what you can do is define that x months later just means changing the value of the months in a date. I.e., two months after July 3rd would be September 3rd. This way you can just take whatever difference remains and use this as number of days, as shown above.

Related

Moment JS Date Difference as Month

I'm trying to get the difference between current local time and December 1, 2021 at 4 pm. There is a difference of 6 months and 2 hours. In this case I expect the answer to be something similar to 6.02. But 5.98 is coming. How can I get the answer I want?
enter image description here
According to the moment.js docs, the standard way to get the difference between two dates in your example would be now.diff(date, 'months', true), which should return a floating point number greater than 6.
now.diff(date) returns the millisecond difference between those two points in time. Calling moment.duration({milliseconds}).asMonths() is not ideal because some months may be 30 days long and others may be 31 days long. It appears that moment.js uses somewhere in between 30 and 31 days as the duration of one month. To address this issue, moment.js have discussed calendar diffs in the docs:
moment#diff has some special handling for month and year diffs. It is optimized to ensure that two months with the same date are always a whole number apart.
So Jan 15 to Feb 15 should be exactly 1 month.
Feb 28 to Mar 28 should be exactly 1 month.
Feb 28 2011 to Feb 28 2012 should be exactly 1 year.
The definition of "a month" can only be a "fuzzy" one, as the months of the calendar are of different lengths. One way of defining it would be to divide the year into 12 equal parts and use that as a "month-metric":
function monthsUntil(year,month,day,hour=0){
const trg=new Date(year,month-1,day,hour,0,0),
now=new Date(), nxt=new Date();
nxt.setFullYear(nxt.getFullYear()+1);
return (12*(trg-now)/(nxt-now)).toFixed(4);
}
console.log(monthsUntil(2022,12,1,16))

Incorrect date in moment.js getters [duplicate]

This question already has an answer here:
How to get the integer value of month from moment.js
(1 answer)
Closed 3 years ago.
EDIT: NB: As pointed out in the accepted answer from Zohaib Ijaz month gives the 0-11 value for the month but the second problem is day. day gives a numeric representation of day of the week ie 0 = sunday, 1 = tuesday. If you want to get the 1-31 you need date.
OK I think this must be a super dumb question but I just cannot see it. Today is 11th November 2019
var day = moment().get('day');
var month = moment().get('month');
var year = moment().get('year');
var dateSet = month+"/"+day+"/"+year;
console.log(dateSet);
and I get back: 10/01/2019!
I tried the functional getter of var day = moment().day(); etc. and same again.
I am trying to get today's date and the date in three months times for a datepicker. Greatful for an explanation of this and extremely grateful for a pointer as how to do the +3 months which I tried moment().plus(3,"months") which did not work.
I am extremely tired but I am pretty sure at one stage this was giving the right date. What could possibly have changed?
moment().month() or moment().get('month') will return month from 0 as January to 11 as December. So if you want to create date in MM/DD/YYYY format, user moment().format(format_string). Or add 1 in month while creating your date string. I would suggest to use format and go through moment docs first so you have better idea what it provides out of the box.
See docs
https://momentjs.com/docs/#/get-set/get/
moment().format('MM/DD/YYYY')

Finding what month it is based on the number of the week in a year

I'm using moment.js to store daily/weekly/monthly data.
Every day a function executes and collects all the logs from that day and stores them into a weekly node with moments .isoWeek() method to determine what week of the year it is.
Now i need to find out what month it is based on the information i have(provided in the screenshot)
If you know the year, you can chain moment segments together:
const monthNumber = moment().isoWeekYear(2019).isoWeekday(1).isoWeek(10).month();
It doesn't look like there's an exact (simple) calculation to work this out - according to wikipedia;
The ISO standard does not define any association of weeks to months. A
date is either expressed with a month and day-of-the-month, or with a
week and day-of-the-week, never a mix.
Alternatively, this leaves us with a calculating a rough estimation. For example;
# Divide the week number by an average approximation of weeks within a month
Week 29 / 4.1 = 7 # This gives us july

How to calculate relative date from now in only days using momentjs?

I am trying to get the amount of days relative to the current time, returning only days as units. So if something happened a week ago, it would say 7 days. If it happened 2 months ago, it would return that time in days as well.
I am aware of how to get there but I am having trouble putting the pieces together.
I have my days as a data attribute "data-order" so data-order="2019-4-2 00:00" or "2019-4-2" if it makes calculations easier.
$(".pop-cal").each(function (i, obj) {
moment.relativeTimeThreshold("m", 1);
moment.relativeTimeThreshold("d", 25 * 100);
var date = $(this).attr("data-order");
var momentDate = moment(date).fromNow();
$(this).attr("data-content", momentDate);
});
This is getting me dates, but they are always off.
My expected results would be similar confirmation to using google search and saying "58 days ago" and getting Tuesday February 12th 2019.
What I am currently getting as result are "59 days ago" on a moment time created on "2019-2-12.
The fromNow method isn't suitable if you need the exact number of days, because it works by converting the time to a Duration (measured in months and milliseconds), and then converting the Duration to a human readable form (humanize method).
As the Duration docs state:
It is much better to use moment#diff for calculating days or years
between two moments than to use Durations.
You can see the problem if you perform fromNow on February 28th and March 1st, 1 day apart but giving a fromNow of 4 days apart (due to 28 days in February instead of 31 days).
moment("2019-03-01").fromNow() // "45 days ago"
moment("2019-02-28").fromNow() // "49 days ago"
The moment#diff method can give you the exact difference between two moments in days, e.g. between moments a and b:
a.diff(b, 'days')

Calculate end date from a start date, duration and only certain working days

I am not a coder, not even close. But I need to use some javascript for a project of mine that has users who only work certain days of the week, and I need to calculate the end date for a task for each user based on the start date, duration, and then only counting that certain users working days.
The closest I have found so far is this: add/subtract business days in Javascript
It uses weekend days 0 and 6 as variables but my users may have a non-working day of Monday, or some with Tuesday, Wednesday and Saturday. How do I alter the code, or use something that can take differing numbers of inputs (some one day, others 4 days) to calculate the correct end date, counting only the working days for that user?
So some context:
I have Bob who works on Monday and Tuesday, therefore his non-working days are the delta - Weds, Thu, Fri, Sat & Sun.
I have Mike who works Mon - Fri so his non-working days are Sat & Sun.
I have Karen who only works Sat & Sun so her non-working days are Mon- Fri.
I use Bubble.is as the application interface and the users have a data field for working days and non-working days so I can use either. I have called the Javascript that exists (on the link) and it works fine by changing the start date and duration in the code to my bubble start date and duration.
I have added the user NWD field (non-working days) which can be any number of fields from one NWD to 6 NWD. Using the Javascript code, I extended out the non-working days to reference NWD#1 to NWD#6 and the code falls over for anyone who does not have 6 NWD entries. Therefore the Javascript code needs to cater for someone who as NWD of 1 (Monday) and someone else who has NWD of 1,2,3,4,5 (Mon - Fri) and that is where it fell over.

Categories