So i have a json object which displays year, month and week of year:
{
"year": 2021,
"month": 8,
"week": 31,
},
{
"year": 2021,
"month": 8,
"week": 32,
}
My question is how can i iterate through the json object then convert the week of year into day of month where the day is the end of the week and then pass in the year, month and day of month into a DateTime object in flutter like this:
DateTime(2021,8,7)
DateTime(2021,8,14)
Multiply the week with a 7 to get the total number of days. Like
int totaldays = week*7;
final extraDuration = Duration(days: totaldays);
final startDate = DateTime(2021);
final newDateTime = startDate.add(extraDuration);
print(newDateTime);
print(newDateTime.next(DateTime.friday));
Related
I have a table structure like this:
I want to create a JSON out of this table in this form:
{
"EntityId": 100000,
"Years": [
{
"Year": 2008,
"Monthly": [
{
"Month": 1,
"Count1": 49,
"Count2": 2
},
{
"Month": 2,
"Count1": 45,
"Count2": 1
},
.
.
.
]
},
{
"Year": 2009,
"Monthly": [
{
"Month": 1,
"Count1": 36,
"Count2": 1
},
{
"Month": 2,
"Count1": 33,
"Count2": 0
},
.
.
.
]
},
.
.
.
.
]
}
This table will always be queried for a single EntityId.
I am not able to nest my arrays also the years are repeating (the number of times the year gets repeated is the number of months inside that year, so if I have 12 months of data for the year 2008 then this year's data gets repeated 12 times) when I try the below query:
select f.EntityId,
(
select TOP 1 [year] as [year],
(
select [month] as [month],
[count1] as [count1],
[count2] as [count2],
from someTable m
where m.EntityId = f.EntityId and m.Year = y.Year
for json path
) as [months]
from someTable y
where y.EntityId = f.EntityId and y.Year = f.Year
for json path
) AS years
from someTable f
where f.EntityId = f.EntityId
for json path
Try this.
SELECT
OuterData1,
OuterData2,
,(SELECT InnerData1, InnerData2
FROM Table2
FOR JSON PATH) InnerJson
OuterData3,
OuterData4
FROM Table1
FOR JSON PATH
I've got four tables in a PostgreSQL db.
user which holds information about a logged in user.
project which holds information about projects created.
userprojects as a joined table between users and projects (one user can belong to many projects and one project can have many users).
timesheet which is where users log their hours - has relation to user_id and project_id and people log their time and date in duration and date columns.
The timesheet table itself stores data as such:
id, user_id, date, duration, project_id
1, 1, "2019-02-01", 8, 1
2, 1, "2019-02-02", 8, 1
3, 2, "2019-02-01", 10, 1
I wish to find a nice way of returning the sum of values for each month from the timesheet table for easy frontend parsing and loading that data into a chart.
What I'm looking for is something along the lines of:
{
"users": [
{
"user_id": "1",
"projects": [
{
"project_id": 1,
"sum": [
{
"august": 18
},
{
"september": 20
}
]
},
{
"project_id": 2,
"sum": [
{
"august": 25
},
{
"september": 10
}
]
}
]
},
{
"user_id": "2",
"projects": [
{
"project_id": 2,
"sum": [
{
"august": 40
},
{
"september": 100
}
]
},
{
"project_id": 3,
"sum": [
{
"august": 30
},
{
"september": 25
}
]
}
]
},
]
}
I've found a neat query which kinda structures the data a bit, but still not ideally:
SELECT
project.name,
to_char(date_trunc('month', date), 'YYYY') AS year,
to_char(date_trunc('month', date), 'Mon') AS month,
to_char(date_trunc('month', date), 'MM') AS month_number,
sum(duration) AS monthly_sum
FROM timesheet INNER JOIN project ON timesheet.project_id = project.id
GROUP BY year, month, month_number, project.name
This query simply returns a table that looks something like:
name: year: month: month_number: monthly_sum
Project XX 2019 Aug 08 10,
Project YY 2019 Aug 08 30,
Project YY 2019 Sep 09 20
How would you guys go around formatting the timesheet table so I can easily display the summed value on a month by month basis?
i have this following huge object from angular 7. so i need to post this object to spring boot app, which means from spring boot controller i need to save these data into the database. how should i do this? I have no idea at all. please help me
periodrw = [
[
{keyvalue:1, period: 1, day: null , subject :null},
{keyvalue:2, period: 1, day: "Monday" , subject :null},
{keyvalue:3, period: 1, day: "Tuesday" , subject :null},
{keyvalue:4, period: 1, day: "Wednesday" , subject :null},
{keyvalue:5, period: 1, day: "Thursday", subject :null },
{keyvalue:6, period: 1, day: "Friday" , subject :null},
],
[
{keyvalue:1, period: 2, day: null , subject :null},
{keyvalue:2, period: 2, day: "Monday" , subject :null},
{keyvalue:3, period: 2, day: "Tuesday" , subject :null},
{keyvalue:4, period: 2, day: "Wednesday" , subject :null},
{keyvalue:5, period: 2, day: "Thursday" , subject :null},
{keyvalue:6, period: 2, day: "Friday" , subject :null},
]
]
Send the data as JSON form Angular and build a representing java class with the percistence annotation from javax.persistence . Deseriealize the JSON and map it to the class and at the end use the CrudRepository to save it.
here a hole tutorial: https://www.springboottutorial.com/spring-boot-crud-rest-service-with-jpa-hibernate
I am using Angular6.
Below is the ngfor with div. Here the api outputs the meeting date and time with status. Normally the status is upcoming.
If the date time is 09/18/2018. and Time is 5.30 Pm.
<div class="col-sm-12" *ngFor="let todayinfo of todaylist | paginate: { id: 'todaypage', itemsPerPage: itemperpage, currentPage: page, totalItems: todaytotal}">
<div class="meeting-date-time" title="Date Time" >
{{todayinfo.meeting_date | date: 'MMM d, y '}} <br>{{todayinfo.displayTime}}
</div>
<div>{{todayinfo.status}} </div>
</div>
The upcoming status should be changed as Join, before 15 minutes of the meeting time.
i.e at Sep 18 at 5.15pm the content need to change as Join. Here how to check the date time check condition. It is displayed inside the loop. If there is any click action we will check this with separate function. Without any events how to check the date time condition here?
Now the Output is
Sep 18 2018 5.30 PM --- Upcoming
Sep 4 2018 9.00 AM -- Completed
Spe 17 2018 7.15 PM -- Upcoming
These upcoming need to change to Join before 15 min of the corresponding date time.
There is simple solution how you can achieve that:
const meetings = [
{meeting_date: new Date(2018, 8, 17, 11, 44), status: null},
{meeting_date: new Date(2018, 8, 17, 12, 45), status: null}
];
const today = new Date();
meetings.filter(meeting => (meeting.meeting_date.getTime() - today.getTime()) <= 900000).forEach(meeting => meeting.status = 'Join');
It will check for meeting where time difference is less or equal to 900000(15min) and mark their status as 'Join'.
This question already has answers here:
json.Marshal(struct) returns "{}"
(3 answers)
Closed 7 years ago.
I am trying to return a json with a list in each property but I am always obtaining the lists as empty lists. It seems that I have a mistake inside a structure but I cannot found it.
I have two structs:
type CalendarDay struct {
day int `json:"day"`
weekday string `json:"weekday"`
}
type CalendarYear struct {
January []CalendarDay `json:"january"`
February []CalendarDay `json:"february"`
March []CalendarDay `json:"march"`
April []CalendarDay `json:"april"`
May []CalendarDay `json:"may"`
June []CalendarDay `json:"june"`
July []CalendarDay `json:"july"`
August []CalendarDay `json:"august"`
September []CalendarDay `json:"september"`
October []CalendarDay `json:"october"`
November []CalendarDay `json:"november"`
December []CalendarDay `json:"december"`
}
I'm trying to return a json as:
{
"january": [{1 Thursday}, ...]
...
}
but I obatain:
{
"january": [{}, {}, {} ...]
...
}
My API is:
func Calendar(w http.ResponseWriter, r *http.Request) {
fmt.Println("GET /")
year := getYear(2015)
json.NewEncoder(w).Encode(year)
}
func getMonthDays(month time.Month, year int) []CalendarDay {
cdays := []CalendarDay{}
for i := 1; i <= daysIn(month, year); i++ {
date := time.Date(year, month, i, 0, 0, 0, 0, time.UTC)
weekday := date.Weekday()
cday := CalendarDay{
day: date.Day(),
weekday: weekday.String()}
cdays = append(cdays, cday)
}
return cdays
}
func getYear(year int) CalendarYear {
yearCal := CalendarYear{
January: getMonthDays(time.January, year),
February: getMonthDays(time.February, year),
March: getMonthDays(time.March, year),
April: getMonthDays(time.April, year),
May: getMonthDays(time.May, year),
June: getMonthDays(time.June, year),
July: getMonthDays(time.July, year),
August: getMonthDays(time.August, year),
September: getMonthDays(time.September, year),
October: getMonthDays(time.October, year),
November: getMonthDays(time.November, year),
December: getMonthDays(time.December, year)}
return yearCal
}
What am I doing wrong?
Export the fields in CalendarDay by starting the name with an uppercase character.
type CalendarDay struct {
Day int `json:"day"`
Weekday string `json:"weekday"`
}
The encoding/json package and similar packages ignore unexported fields.