<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="row">
<h2>Get Data for according to months , day, week year , per minutes</h2>
</div>
</div>
let now = new Date();
let todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate())
let todayEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1)
let startMonthDate: any = new Date(now.getFullYear(), now.getMonth(), 1);
let endMonthDate: any = new Date(
now.getFullYear(),
now.getMonth() + 1,
0
);
let startWeekDay = now.getDate() - now.getDay();
let startWeekDate = new Date(
now.getFullYear(),
now.getMonth(),
startWeekDay
);
let endWeekDate = new Date(
now.getFullYear(),
now.getMonth(),
startWeekDay + 6
);
console.log(startWeekDate, endWeekDate, "?????????")
let startYearDate: any = new Date(now.getFullYear(), 0, 1);
let endYearDate: any = new Date(now.getFullYear() + 1, 0, 0);
let start_month_week = Math.ceil(
Math.floor((startMonthDate - startYearDate) / (24 * 60 * 60 * 1000)) / 7
);
let end_month_week = Math.ceil(
Math.floor((endMonthDate - startYearDate) / (24 * 60 * 60 * 1000)) / 7
);
let query = {
...(selected == "year"
? {
createdAt: {
$gte: startYearDate,
$lte: endYearDate,
},
}
: selected == "month"
? {
createdAt: {
$gte: startMonthDate,
$lte: endMonthDate,
},
}
: selected == "week"
? {
createdAt: {
$gte: startWeekDate,
$lte: endWeekDate
},
}
: selected == "day"
? {
createdAt: {
$gte: todayStart,
$lt: todayEnd,
},
}
:
selected == "custom"
? {
createdAt: {
$gte: new Date(`${fromDate}T00:00:00Z`),
$lte: new Date(`${toDate}T00:00:00Z`),
},
}
: null),
};
let graphData = await transactionModel.aggregate([
{
$match: query,
},
{
$group: {
_id: {
min: { $minute: "$createdAt" },
hour: { $hour: "$createdAt" },
day: { $dayOfMonth: "$createdAt" },
month: { $month: "$createdAt" },
year: { $year: "$createdAt" },
},
total: { $sum: "$amount" },
},
},
{
$project: {
minutes: "$_id.min",
hours: "$_id.hour",
day: "$_id.day",
month: "$_id.month",
year: "$_id.year",
total: 1,
_id: 0,
},
},
{
$sort: {
year: 1,
month: 1,
day: 1,
hours: 1,
minutes: 1,
},
},
]);