Created
May 30, 2019 07:53
-
-
Save mohamedaboelmagd/e67d7cd6afa7b808ef1795a546e20153 to your computer and use it in GitHub Desktop.
analytics on timestamp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cleanPerMonth( | |
data: { | |
counter: number; | |
month: number; | |
year: number; | |
}[], | |
numberOfMonths: number | |
) { | |
const result = []; | |
const everyMonth = []; | |
const months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; | |
const today = new Date(); | |
let date; | |
let month; | |
let year; | |
for (let index = numberOfMonths - 1; index >= 0; index -= 1) { | |
date = new Date(today.getFullYear(), today.getMonth() - index, 1); | |
month = months[date.getMonth()]; | |
year = date.getFullYear(); | |
everyMonth.push({ month: month, year: year }); | |
} | |
everyMonth.forEach(element => { | |
const dataDay = data.map(item => item.month); | |
if (dataDay.indexOf(element.month) === -1) { | |
result.push({ counter: 0, month: element.month, year: element.year }); | |
} else { | |
result.push(data.filter(item => item.month === element.month)[0]); | |
} | |
}); | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cleanMonth( | |
data: { | |
counter: number; | |
week: number; | |
}[] | |
) { | |
const result = []; | |
const lastMonth = [ | |
this.getWeek(3), | |
this.getWeek(2), | |
this.getWeek(1), | |
this.getWeek(0) | |
]; | |
lastMonth.forEach(element => { | |
const dataDay = data.map(item => item.week); | |
if (dataDay.indexOf(element) === -1) { | |
result.push({ counter: 0, week: element }); | |
} else { | |
result.push(data.filter(item => item.week === element)[0]); | |
} | |
}); | |
return result; | |
} | |
getWeek(subtract: number) { | |
const date = new Date(); | |
const selectedDate = new Date(date.setDate(date.getDate() - subtract * 7)); | |
selectedDate.setHours(0, 0, 0, 0); | |
// Thursday in current week decides the year. | |
selectedDate.setDate( | |
selectedDate.getDate() + 3 - ((selectedDate.getDay() + 6) % 7) | |
); | |
// January 4 is always in week 1. | |
const week1 = new Date(selectedDate.getFullYear(), 0, 4); | |
// Adjust to Thursday in week 1 and count number of weeks from date to week1. | |
return ( | |
1 + | |
Math.round( | |
((selectedDate.getTime() - week1.getTime()) / 86400000 - | |
3 + | |
((week1.getDay() + 6) % 7)) / | |
7 | |
) | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cleanWeek( | |
data: { | |
counter: number; | |
day: string; | |
}[] | |
) { | |
// 'Sun' | 'Mon' | 'Tue' | 'Wed' | 'Thu' | 'Fri' | 'Sat' | |
const result = []; | |
const lastWeek = [ | |
this.getDay(6), | |
this.getDay(5), | |
this.getDay(4), | |
this.getDay(3), | |
this.getDay(2), | |
this.getDay(1), | |
this.getDay(0) | |
]; | |
lastWeek.forEach(element => { | |
const dataDay = data.map(item => item.day); | |
if (dataDay.indexOf(element as any) === -1) { | |
result.push({ counter: 0, day: element }); | |
} else { | |
result.push(data.filter(item => item.day === element)[0]); | |
} | |
}); | |
return result; | |
} | |
getDay(subtract: number) { | |
const date = new Date(); | |
return new Date(date.setDate(date.getDate() - subtract)).toLocaleString( | |
'en-us', | |
{ | |
weekday: 'short' | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment