Skip to content

Instantly share code, notes, and snippets.

@gulshanzealous
Created October 1, 2017 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gulshanzealous/6486e366800852a1d59ab11af16f9a35 to your computer and use it in GitHub Desktop.
Save gulshanzealous/6486e366800852a1d59ab11af16f9a35 to your computer and use it in GitHub Desktop.
// let now = '2017-10-07 16:00:00'
// // let now = Date.now()
// const timed = moment(now).format('dddd, MMMM DD YYYY HH mm ss A')
// const timeStamp = moment(now).valueOf()
// console.log(timed)
// console.log(timeStamp)
const weekSchedule = {
"data": {
"sun":[{
"start_time": 1506825000000,
"end_time": 1506828600000,
"student": {
"id": 273,
"full_name": "Flurry Makes",
"profile_picture": "some://url",
"type": "student"
}
},{
"start_time": 1506846600000,
"end_time": 1506853800000,
"student": {
"id": 273,
"full_name": "Flurry Makes",
"profile_picture": "some://url",
"type": "student"
}
}],
"mon":[{
"start_time": 1506918600000,
"end_time": 1506922200000,
"student": {
"id": 273,
"full_name": "Flurry Makes",
"profile_picture": "some://url",
"type": "student"
}
},{
"start_time": 1506947400000,
"end_time": 1506958200000,
"student": {
"id": 273,
"full_name": "Flurry Makes",
"profile_picture": "some://url",
"type": "student"
}
}],
"tue":[{
"start_time": 1507012200000,
"end_time": 1507019400000,
"student": {
"id": 273,
"full_name": "Flurry Makes",
"profile_picture": "some://url",
"type": "student"
}
},
// {
// "start_time": 8271628716782,
// "end_time": 2873782687263,
// "student": {
// "id": 273,
// "full_name": "Flurry Makes",
// "profile_picture": "some://url",
// "type": "student"
// }
// }
],
"wed":[{
"start_time": 1507069800000,
"end_time": 1507077000000,
"student": {
"id": 273,
"full_name": "Flurry Makes",
"profile_picture": "some://url",
"type": "student"
}
},{
"start_time": 1507087800000,
"end_time": 1507091400000,
"student": {
"id": 273,
"full_name": "Flurry Makes",
"profile_picture": "some://url",
"type": "student"
}
}],
"thu":[{
"start_time": 1507195800000,
"end_time": 1507206600000,
"student": {
"id": 273,
"full_name": "Flurry Makes",
"profile_picture": "some://url",
"type": "student"
}
},
// {
// "start_time": 8271628716782,
// "end_time": 2873782687263,
// "student": {
// "id": 273,
// "full_name": "Flurry Makes",
// "profile_picture": "some://url",
// "type": "student"
// }
// }
],
"fri":[{
"start_time": 1507257000000,
"end_time": 1507260600000,
"student": {
"id": 273,
"full_name": "Flurry Makes",
"profile_picture": "some://url",
"type": "student"
}
}, {
"start_time": 1507296600000,
"end_time": 1507303800000,
"student": {
"id": 273,
"full_name": "Flurry Makes",
"profile_picture": "some://url",
"type": "student"
}
}
],
"sat":[{
"start_time": 1507354200000,
"end_time": 1507357800000,
"student": {
"id": 273,
"full_name": "Flurry Makes",
"profile_picture": "some://url",
"type": "student"
}
},{
"start_time": 1507365000000,
"end_time": 1507372200000,
"student": {
"id": 273,
"full_name": "Flurry Makes",
"profile_picture": "some://url",
"type": "student"
}
}]
},
}
const getDayStartDayTime = (anyTime) => {
// let anyTime = Date.now()
let hrs = moment(anyTime).format('HH')
let mins = moment(anyTime).format('mm')
let secs = moment(anyTime).format('ss')
let subHrs = parseInt(hrs)
// console.log(subHrs)
let subMins = parseInt(mins)
// console.log(subMins)
let subSecs = parseInt(secs)
// console.log(secs)
let startDayTimeStamp = moment(anyTime)
.add(-subHrs,'hours')
.add(-subMins,'minutes')
.add(-secs,'seconds')
.valueOf()
return startDayTimeStamp
}
const getParticularDayData = (dayFullSchedule) => {
// console.log(dayFullSchedule)
const startTime = getDayStartDayTime(dayFullSchedule[0].start_time)
if(!dayFullSchedule[0].start_time){
return { morningData:[], afternoonData:[], eveningData:[] }
}
const timestamp8AM = moment(startTime).add(8,'hours').valueOf()
const timestamp4PM = moment(timestamp8AM).add(8,'hours').valueOf()
const timestamp12AM = moment(timestamp4PM).add(8,'hours').valueOf()
const morningData = dayFullSchedule.filter(time => time.end_time >= startTime && time.end_time < timestamp8AM )
const afternoonData = dayFullSchedule.filter(time => time.end_time >= timestamp8AM && time.end_time < timestamp4PM )
const eveningData = dayFullSchedule.filter(time => time.end_time >= timestamp4PM && time.end_time < timestamp12AM )
return { morningData:morningData, afternoonData:afternoonData, eveningData:eveningData }
}
const sortSchedule = (weekSchedule) => {
const sunData = getParticularDayData(weekSchedule.data.sun)
const monData = getParticularDayData(weekSchedule.data.mon)
const tueData = getParticularDayData(weekSchedule.data.tue)
const wedData = getParticularDayData(weekSchedule.data.wed)
const thursData = getParticularDayData(weekSchedule.data.thu)
const friData = getParticularDayData(weekSchedule.data.fri)
const satData = getParticularDayData(weekSchedule.data.sat)
return { sunData,monData,tueData,wedData,thursData,friData,satData }
}
const organizedData = sortSchedule(weekSchedule)
// console.log(organizedData)
const allMornings = Object.values(organizedData).reduce((allMorningsData,day)=>{
return [...allMorningsData, ...day.morningData]
},[])
// console.log(allMornings)
const allAfternoons = Object.values(organizedData).reduce((allNoonsData,day)=>{
return [...allNoonsData, ...day.afternoonData]
},[])
// console.log(allAfternoons)
const allEvenings = Object.values(organizedData).reduce((allEveningsData,day)=>{
return [...allEveningsData, ...day.eveningData]
},[])
// console.log(allEvenings)
const printSchedule = (sessions) => {
sessions.forEach(session => {
const start = moment(session.start_time).format('dddd, MMMM DD YYYY HH mm ss A')
const end = moment(session.end_time).format('dddd, MMMM DD YYYY HH mm ss A')
console.log(start)
console.log(end)
console.log('--------------')
})
}
console.log('----------MORNINGS-------------')
printSchedule(allMornings)
console.log('----------NOONS---------------')
printSchedule(allAfternoons)
console.log('--------EVENINGS---------------')
printSchedule(allEvenings)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment