How to get previous 5 days date from given date? - flutter

How to get previous 5 days date(30/11/2020) from given date?
How to get previous 5 months from given date?

Simply add a negative Duration:
DateTime nowFiveDaysAgo = DateTime.now().add(Duration(days: -5));

Related

how to add weeks,month, year to a date in flutter

I have a date string in flutter which i am trying to add week, month and year but not sure how to do it.
in android java, we can add weeks, month, year in the following way
Calendar cal = Calendar.getInstance();
cal.setTime('2019-09-04');
cal.add(Calendar.WEEK_OF_MONTH,1); // add one week
cal.add(Calendar.WEEK_OF_MONTH, 2 ); // add two weeks
cal.add(Calendar.MONTH, 1); // add # of month
cal.add(Calendar.MONTH, 3); // add 3 months
cal.add(Calendar.YEAR, 1); // add 1 year
however, i dont know how to add this in flutter. i know i can use duration but it only accept number of days cal.add(new Duration(days: 30));
if i use duration, then i need to figure out how many number of days in a month, week, or year and it is more math. with the above code in android java, adding weeks , months or year will be taken by android. is there a way for flutter to add weeks, months or year to a date?
for example: if i have 01/31/2019 and i want to add a month then new date should be 02/28/2019. if i want add a month to 02/28/2019 then output should be 03/31/2019
same goes for weeks and years. flutter should add a week to a date if specify or a year and so on. thanks in advance
You can find everything you need on this medium article:
Top 7 Date methods
Let's suppose you have '2019-09-04', in Dart you have:
var myDate = DateTime.parse("2019-09-04 20:18:04Z");
myDate.add(Duration(days: 10, hours: 5)));
then print it out:
print(myDate.toLocal());
EDIT:
because of your comment below, to add years or month do as follow:
var date = new DateTime(2019, 9, 4);
var newDate = new DateTime(date.year + 1, date.month + 2, date.day);
You can find similar answer here: Add/Substract months/years to date in dart?

Calculate next pay day in Swift

I am trying to do some math calculations in Swift and getting a bit stuck.
Essentially we get paid every 4th Friday, and I want to do a calculation that based on today's date, works out how many days until our next Pay Day.
I've got a reference day of "28/03/2008",
In order to calculate the next 4th Friday of the month, you need a combination of the right DateComponents and Calendar.
// The 4th Friday at noon
let payDayComps = DateComponents(hour: 12, weekday: 6, weekdayOrdinal: 4)
// Given the current date, calculate the next 4th Friday at noon
let nextPayDay = Calendar.current.nextDate(after: Date(), matching: payDayComps, matchingPolicy: .nextTime)
You don't need a past reference date for this if your goal is to get the next 4th Friday based on today's date. Change the value of the after parameter if you need to calculate the 4th Friday based on some other specific date.
If this is run on the 4th Friday of a month then it will return today's date if run before noon and it will return the 4th Friday of the next month if run after noon. Change the hour value in payDayComps if you want different results (such as 0 for midnight).
To calculate the number of days until that next pay day you can do:
let daysToPayDay = Calendar.current.dateComponents([.day], from: Date(), to: nextPayDay).day!

How do I Determine The Number Of Days In A Month from a particular date in angularjs 2?

I am using angularjs 2 and type script.
I want the number of days in a month from a particular date given by user.
How do I Determine that?
Please help. Thanks in Advance.
As pointed out by Evgeny Shmanev, this answer is incorrect. Please see the correct answer by Zvi Tarem
You don't need angular, you can call this function:
function daysRemainingInMonth(date) {
var year = date.getYear();
var month = date.getMonth();
return new Date(year, month, 0).getDate()
}
Edit: Updated now that I know you have the date
It may be a bit late, but the right answer is a follows:
In order to find the number of days in a month, you need to get the 'date' of the 0th day of the next month:
let daysInMonth = new Date(year, month + 1, 0).getDate();

How to create a specific date in Google Script

I have a spreadsheet that asks people to enter in a day of the month when we need to send out a bill. What I want to do is create a calendar event based on that. So, essentially what I need is an event that starts at the current month, day from the spreadsheet, and continues to a specified point in time.
var monthlyDate = row[6]; // Seventh column, monthly date of payment
var curDate = new Date();
var curMonth = curDate.getMonth();
var curYear = curDate.getYear();
curDate.setDate(curMonth, monthlyDate, curYear);
Logger.log("Day of month: %s", monthlyDate);
Logger.log("Current Date: %s", curDate);
Logger.log("Current Date: %s", Date());
What I'm seeing is that the monthly date is coming in as a float "6.0" for example, and no matter what I enter in for monthlyDate in the setDate line, it keeps setting the date to 10/9/15 (Today is 10/15/15). I've hard-coded that value to many different numbers, but for some reason it's just not working.
How can I create a date (in any format) that follows the scheme "Current Month / Day from Speadsheet / Current Year" ?
The getMonth() method returns a "zero-indexed" number. So, it returns the number 9 for the 10th month. setDate() doesn't set the date, it sets the "Day of the Month". The name of that method is misleading.
Documentation - setDate()
So, the last two parameters that you are using in setDate() are doing nothing. You are setting the day of the month to 9.
If you want to set multiple date parameters at the same time, you need to use the new Date() method:
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
The month parameter accept values from 0 to 11, 0 is Jan and 11 is Dec
Date Reference

Aggregation query in MongoDB

I'm looking to write a pretty straightforward aggregation query in MongoDB, but struggling with a certain part.
What I would like to do is pull the sum of all records within the last 7 days grouped by day. It's easy enough to define the date 7 days ago as UTC, but I'd like to do it programmatically so I don't need to work out the UTC date everytime. For instance instead of 1341964800 i'd like to specify something like date() - 7 days.
Here's the current aggregation function I have which works:
db.visits_calc.group(
{ key:{date:true},
cond:{date:{$gt:1341964800}},
reduce:function(obj,prev) {prev.csum += obj.total_imp},
initial:{csum:0}
});
Thanks in advance!
You can perform arithmetic on the milliseconds timestamp returned by Date.now() to find the appropriate timestamp for 7 days ago. You need to subtract the number of milliseconds in 7 days (1000ms/s, 60 s/min, 60min/hr, 24 hrs/dy, 7dys/wk).
var weekAgo = Date.now() - (1000*60*60*24*7);
db.visits_calc.group(
{ key:{date:true},
cond:{date:{$gt:weekAgo}},
reduce:function(obj,prev) {prev.csum += obj.total_imp},
initial:{csum:0}
});