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.
Related
I can get start of the week as Monday using method below:
DateTime findFirstDateOfTheWeek(DateTime date) {
return date.subtract(Duration(days: date.weekday - 1));
}
and end of the week, the Sunday, as:
DateTime findLastDateOfTheWeek(DateTime date) {
return date.add(Duration(days: DateTime.daysPerWeek - date.weekday));
}
Now the requirements are, to treat Sunday as week start and Saturday as week end. I tried tweaking above method but could not get it working. Any help would be appreciated.
Thanks
Since DateTime.weekday counts from 1 (Monday) to 7 (Sunday):
date.subtract(Duration(days: date.weekday - 1))
will give us the date of the start of the week using what DateTime considers to be the first day of the week (Monday). To treat Sunday as the first day of the week instead, we can try to get the previous day from that:
date.subtract(Duration(days: date.weekday - 1)).subtract(const Duration(days: 1))
The above is algebraically equivalent to date - (weekday - 1) - 1, which can be simplified to just date - weekday. But that's not quite right because weekday ranges from [1, 7], and we never want to subtract 7 days. In that case, we want to subtract 0. The modulus operator handles that nicely, ultimately giving us:
DateTime findFirstDateOfTheWeek(DateTime date) {
return date.subtract(Duration(days: date.weekday % DateTime.daysPerWeek));
}
To get the last day of the week where Saturday is treated as the last day of the week, you can just get the first day of the week and add 6 days:
DateTime findLastDateOfTheWeek(DateTime date) {
return findFirstDateOfTheWeek(date).add(
const Duration(days: DateTime.daysPerWeek - 1));
}
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 have two date prompts FromDate and Todate in Cognos. FromDate should always be Prior Week first Day (i.e, Monday) and ToDate should be last day of that week(i.e., Sunday). Any help will be appreciated. thanks
FromDate
The following expression will return Monday of the previous week:
_add_days(current_date, (-1 * _day_of_week(current_date,2)) - 7)
ToDate
From there you can get the Sunday following the FromDate value:
_add_days([FromDate],6)
The only issue is what range you want when the report is run on Monday. Using the above expressions the report will go back an extra week when run on Monday. To get around this you can test for Monday and change the FromDate expression accordingly:
CASE _day_of_week(current_date,1)
WHEN 1 THEN _add_days(current_date, -1 * _day_of_week(current_date,2))
ELSE _add_days(current_date, (-1 * _day_of_week(current_date,2)) - 7)
END
When run on a Monday using this expression the previous Monday FromDate will be the previous Monday and ToDate will be the Sunday just before the current date.
The only way to dynamically set prompt defaults for date prompts is to use JavaScript. Thankfully, Cognos has provided a fully-supported JavaScript prompt API in versions 10.2.1 and above.
In order to reference prompt objects via the Cognos JavaScript API you have to give the prompt a unique name. For the following code I've assumed your "from date" prompt has been named "FromDate" and the "to date" prompt has been named "ToDate".
Insert the following code into a new HTML object. Make sure the HTML object appears after any prompt objects.
<script>
var report = cognos.Report.getReport('_THIS_');
var FromDate = report.prompt.getControlByName('FromDate');
var ToDate = report.prompt.getControlByName('ToDate');
var today = new Date();
var fromdate = new Date();
var todate = new Date();
fromdate.setDate(today.getDate() - (today.getDay() + 6));
todate.setDate(today.getDate() - today.getDay());
var fromdatestring = fromdate.getFullYear() + '-' + ("0" + (fromdate.getMonth() + 1)).slice(-2) + '-' + ("0" + fromdate.getDate()).slice(-2);
var todatestring = todate.getFullYear() + '-' + ("0" + (todate.getMonth() + 1)).slice(-2) + '-' + ("0" + todate.getDate()).slice(-2);
FromDate.addValues([{'use':fromdatestring}]);
ToDate.addValues([{'use':todatestring}]);
</script>
I would like to specify a number that specifies the day of week and then have ASP get the upcoming date for that week day specified.
Example:
Dim xWeekDay
xWeekDay=1 ' <-- 1 would be a Monday...and Sunday would be 7
Dim NextDdate
NextDdate= ???? <-- I want to calculate and show the Upcoming Date here
So the above line would look like this when it's populated.
NextDdate=7/1/2013
Try this:
today = Weekday(Date, vbMonday)
If xWeekDay > today Then
NextDate = Date + (xWeekDay - today)
Else
NextDate = Date + (xWeekDay + 7 - today)
End If
Weekday(Date, vbMonday) is the number of the currend day of the week (with Monday being set as the first weekday). If xWeekDay is in the future (xWeekDay > today), then the next occurrence is xWeekDay - today days away. Otherwise it's xWeekDay + 7 - today days away. Add that difference to the current date and you have the date you're looking for.
im looking for mathematical formulas to add and subtract months to/from a date. i only need to know the year and month, therefor days can be ignored.
this is the adding months pseudo code i came up with:
OldYear = 2012 // current year
OldMonth = 3 // current month
AddMonths = 0 // the months to be added
FooBar = OldMonth + AddMonths
NewYear = OldYear + FooBar / 12
NewMonth = FooBar % 12
IF NewMonth = 0
NewYear = NewYear - 1
NewMonth = 12
END IF
// set AddMonths to 0 and the result will be 2012.03
// set AddMonths to 6 and the result will be 2012.09
// set AddMonths to 9 and the result will be 2012.12
// set AddMonths to 11 and the result will be 2013.02
// set AddMonths to 23 and the result will be 2014.02
// set AddMonths to 38 and the result will be 2015.05
and it works really great, but is there an even better way? i dont really like the need for the IF NewMonth = 0 readjustments.
but my actual problem is, that i couldnt come up with a counterpart formula to substract months. i tried various things, but everything failed and its driving me insane. so any help would be much appreciated!
It is a combination of my comment in #Matt's answer, and answer of #Matt
The formula can be greatly reduced if you adopt a 0-based month scheme.
pseudocode:
year = 2012; // year 2012
month = 6; // July (NOT June)
monthToAdd = -20; // +ve/-ve means add/subtract
resultYear = (year * 12 + month + monthToAdd) /12;
resultMonth = (year * 12 + month + monthToAdd) mod 12;
// resultYear == 2010
// resultMonth == 10 , which means November
Edit: The original answer above assumed a zero-based month scheme, which seem being overlooked by some people. To avoid confusion, we can of course use more intuitive 1-based month scheme, and do the 0-base conversion during calculation (though it make the calculation slightly messier):
year = 2012; // year 2012
month = 7; // July
monthToAdd = -20; // +ve/-ve means add/subtract
resultYear = (year * 12 + (month - 1) + monthToAdd) /12;
resultMonth = ((year * 12 + (month - 1) + monthToAdd) mod 12) + 1
// resultYear == 2010
// resultMonth == 11 , which means November
One possible solution would be to multiply OldYear by 12 (thus converting it to months), adding or subtracting AddMonths or SubMonths, respectively, and then converting back to NewYear and NewMonth by using integer and modular division (depending on your programming language, you may be able to simplify this).
Full credit to Adrian Shum for building the main logic but I think there is a edge case which the code is not 100% accurate for cases where we go back the same number of months as the month we are in. For example, if we are in March and we go back 3 months we should end up at Dec (Feb, Jan and Dec) .
here is the modified version:
year = 2020;
month = 4;
monthToAdd = -1;
resultYear = (year * 12 + month + monthToAdd) /12;
resultMonth = (year * 12 + month + monthToAdd) % 12;
if month+monthToAdd == 0:
resultMonth=12
elif (abs(monthToAdd)%month)==0:
resultMonth=month
print(resultMonth)
print(resultYear)