How To count age from date of birth - momentjs

I am using moment.js with angular, how to get age in years, month and days. here is my code:
getAge(date) {
let age = moment().diff(date, 'year');
return age;
}
but it can give age only by year. How to get the age in years,days and month?

var m1 = moment();
var m2 = moment(DATE OF BIRTH,'YYYY-MM-DD HH:mm:ss');
var diff = moment.preciseDiff(m1, m2); // '1 month 2 days 3 hours 4 minutes 5 seconds'
This requires moment-precise-range.js to be included. For a detailed example, check this here - https://codebox.org.uk/pages/moment-date-range-plugin

Related

Dart/Flutter get first DateTime of this week [duplicate]

This question already has answers here:
How to get start of or end of week in dart
(8 answers)
Closed 1 year ago.
Using flutter on android I am trying to get the first date of current week. For example today is 13/7/2020, assuming week starts from Saturday the first date of current week will be 11/7/2020.
This for example to get first date of month
DateTime firstDay = new DateTime(
DateTime.now().year,
DateTime.now().month,
1,
); //get first date this month
The end goal is to get timestamp of that date and fetch entries from sqflite database that is higher than that.
If Sunday is your first day of week.
var d = DateTime.now();
var weekDay = d.weekday;
var firstDayOfWeek = d.subtract(Duration(days: weekDay));
According to Flutter docs weekDay returns
The day of the week [monday]..[sunday].
In accordance with ISO 8601
a week starts with Monday, which has the value 1.
if Monday is first day of week then
var firstDayOfWeek = d.subtract(Duration(days: weekDay - 1));
For Sunday:
DateTime today = DateTime.now();
DateTime _firstDayOfTheweek =
today.subtract(new Duration(days: today.weekday));
print(_firstDayOfTheweek.day);
For Monday:
DateTime _firstDayOfTheweek =
today.subtract(new Duration(days: today.weekday - 1));
print(_firstDayOfTheweek.day);
For Saturday:
DateTime _firstDayOfTheweek =
today.subtract(new Duration(days: today.weekday + 1));
print(_firstDayOfTheweek.day);
The DateTime class stores int constants for the day of the week which you could use to calculate the start date you need. Something like this should work:
void main() {
var startOfWeek = DateTime.saturday;
var currentDate = DateTime.now();
var daysSince = currentDate.weekday + (DateTime.sunday - startOfWeek);
var result = currentDate.subtract(Duration(days: daysSince));
print('Date at start of week is $result');
}
To get different dates of different days in a week. We can use this isoweek package for this. We can use like this :-
Week currentWeek = Week.current();
Week weekFromIso = Week.fromISOString(currentWeek.toString());
print('Week from ISO string: $weekFromIso');
DateTime firstDay= weekFromIso.day(0);
String formattedDateFirst = DateFormat('EEE, d MMM').format(firstDay);
DateTime secondDay= weekFromIso.day(1);
String formattedDateSecond = DateFormat('EEE, d MMM').format(secondDay);

Get Week Number from Given Date in ASP.NET [duplicate]

This question already has answers here:
How can I calculate/find the week-number of a given date?
(7 answers)
Closed 8 years ago.
I have date like 08/31/2013 and i want in which week given date falls.So please help me how can i achieved it.For e.g give date falls in 5th week of August.
Try
var currCulture = CultureInfo.CurrentCulture;
var weekNo = currCulture.Calendar.GetWeekOfYear(
new DateTime(2014, 28, 03), //Any date
currCulture.DateTimeFormat.CalendarWeekRule,
currCulture.DateTimeFormat.FirstDayOfWeek);
Found this here. Check if helps.
DateTime dt = DateTime.Now;
int weekOfMonth = (dt.Day + ((int)dt.DayOfWeek)) / 7 + 1;
DateTime dt = DateTime.Today;
CultureInfo ci = CultureInfo.CurrentCulture;
int weekNum = ci.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
weekNum = weekNum / 12;
string weekday = dt.DayOfWeek.ToString();
With this code you will be able to get week number and week day of any date.....

How to display the number of years and month since a date using moment.js

I want to display something like this :
member for 1 year, 8 month
The example on the momment.js web site is :
moment("20120620", "YYYYMMDD").fromNow(); // 2 years ago.
How to display also the number of month since the provided date ?
Like : 2 years, 6 months
Thank you
Ok I found the answer so I post it in case it can be useful for others:
var date = new Date(2011,5,24)
var month = date.getMonth()
var year = date.getFullYear();
var dateString = moment(year, "YYYY").fromNow(true) +
moment(month, "MM").fromNow(true)) + " ago"
console.log(dateString)

c# linq group by fortnight or x number of days?

Hi I'm trying to work out how would you group items into the first and 2nd half of each month for a given year and month?
i.e.
Say for example I have to list the name of item on the 6th of every month and the 15th of every month.
say for example I have
Flight Name Flight Date
Flight 1 01/07/2012
Flight 2 12/07/2012
Flight 3 18/07/2012
Flight 4 28/07/2012
how would i split it up so I'd like to group flights by fortnights within a year/month
i.e
Flights For July week 1 and 2 - 2012
Flights for July week 3 and 4 - 2012
so this is what I have so far..
eventually it'll have to be some kind of view model using automapper etc i am not sure as of yet how it'd get that neatly into some kind of ViewModel form...
var flightEntities = from f in flightsAsViewModels
select new
{
YearGroups = from flightYearGroup in context.Flights
group flightYearGroup by flightYearGroup.FlightDateTime.Year
into yearGroup
orderby yearGroup.Key descending
select new
{
Year = yearGroup.Key,
MonthGroups = from flightMonthGroup in yearGroup
group flightMonthGroup by flightMonthGroup.FlightDateTime.Month
into monthGroup
orderby monthGroup.Key ascending
select new {
Month = monthGroup.Key,
HalfMonthGroups = from months in monthGroup
group months by (months.FlightDateTime.Day <= 15 ? 1 : 2) into splitMonthFlights
orderby splitMonthFlights.Key
select new { WhichHalfOfMonth = splitMonthFlights.Key, Flights = splitMonthFlights }
}
}
};
I hereby pasted Linq code for the requirement. But not sure, this still can be achievable in shortest way. Please let me know if so.
var monthGroupedDates = from d in dates
group d by d.Month into fd
select new { Month = fd.Key, Dates = fd };
foreach (var g in monthGroupedDates)
{
var groupByHalf = from n in g.Dates
group n by (n.Day <= 15 ? 1 : 2) into gd
orderby gd.Key
select new { WhichHalf = gd.Key, Dates = gd };
foreach (var gh in groupByHalf)
{
string printString = (gh.WhichHalf == 1 ? "First" : "Second") + " week dates are:";
foreach (var h in gh.Dates)
{
printString += h.ToString("dd/MM/yyyy") + ";";
}
Console.WriteLine(printString);
}
}
Please note, dates are one which is mentioned in your requirement. If its Linq with SQL, this may need slight alterations.
Raj

How to calculate the number of weeks in a month

I want to calculate the total no of weeks in current month. Starting from Sunday or Monday.
Is it possible to do in Qt
I would say this problem is not specific to Qt, but Qt can help you with the QDate class.
With this class you can get the current month :
QDate CurrentDate = QDate::currentDate();
The number of days of a given month :
CurrentDate.daysInMonth();
For the number of week calculation, it depends if you only want the number of full weeks in a month, or the number of weeks, taking partial weeks into account.
For the latter, here is how I would do it (considering the week starts on monday) :
const DAYS_IN_WEEK = 7;
QDate CurrentDate = QDate::currentDate();
int DaysInMonth = CurrentDate.daysInMonth();
QDate FirstDayOfMonth = CurrentDate;
FirstDayOfMonth.setDate(CurrentDate.year(), CurrentDate.month(), 1);
int WeekCount = DaysInMonth / DAYS_IN_WEEK;
int DaysLeft = DaysInMonth % DAYS_IN_WEEK;
if (DaysLeft > 0) {
WeekCount++;
// Check if the remaining days are split on two weeks
if (FirstDayOfMonth.dayOfWeek() + DaysLeft - 1 > DAYS_IN_WEEK)
WeekCount++;
}
This code has not been fully tested and is not garanteed to work !
floor(Number of Days / 7)
QDate::weekNumber can give you the number of the week in the year.
Here is an example of how to use it to obtain the number of weeks in a month, including those shorter than seven days:
QDate dateCurrent = QDate::currentDate();
int year = dateCurrent.year(), month = dateCurrent.month(),
daysInMonth = dateCurrent.daysInMonth(), weeksInMonth;
weeksInMonth = QDate(year, month, daysInMonth).weekNumber()
- QDate(year, month, 1).weekNumber() + 1;
Some months have 4 weeks and others have 5. Qt states that:
In accordance with ISO 8601, weeks start on Monday and the first Thursday of a year is always in week 1 of that year. Most years have 52 weeks, but some have 53.
For that matter my first Thursday in a month is the starting point when counting the number of weeks in a month. The function below works for me:
int myClass::weeksInMonth(QDate cdate)
{
QDate sDte = QDate(cdate.year(),cdate.month(),1);
QDate eDte = QDate(cdate.year(),cdate.month(),cdate.daysInMonth());
int wks = 0;
for(QDate stD = sDte; stD <= eDte; stD = stD.addDays(1)){
if(stD.dayOfWeek() == Qt::Thursday)++wks;
}
return wks;
}
Here's a function that wraps the top answer for PyQt5. However the ISO 8601 definition for week 01 is the week with the first Thursday of the Gregorian year; this solution will give a negative number of weeks on dec and jan on certain years..
def weeksInMonth(date: QDate):
year = date.year(), month = date.month()
daysInMonth = date.daysInMonth()
return QDate(year, month, daysInMonth).weekNumber()[0] - QDate(year, month, 1).weekNumber()[0] + 1

Resources