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.....
Related
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);
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
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)
I want to get the Sunday & Saturday of the week from which a date is provided.
I have access to the following functions only:
getDate() returns a number from 0-6 (0 being sunday)
getDay() returns a number from 1-31
getMonth() returns a number from 0-11
getFullYear() returns the current year
I am doing this on titanium.
Per your description above, I came up with:
var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDate() + getDay());
var sun = new Date(input.getFullYear(), input.getMonth(), getDay() + (input.getDate() - 6));
If I follow the MDN doc, I come up with (works in Ti too):
var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDay() + input.getDate());
var sun = new Date(input.getFullYear(), input.getMonth(), input.getDate() + (input.getDay() - 6));
Where input is a javascript Date object.
The date object will take care or changing the month and/or year if necessary.
Hope this helps.
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