I'm working on a app which I should set messages according to current time based on hours.
For ex if time is: 23:35:2 I should set a string to Good night and if the time goes 4:00:0 I want to set Good morning accordingly.
Currently I'm writing the logic in angular service's BehaviourSubject. I get the current time by using javascript. But after I console logged the value the time is statically there.
let currentDate = new Date();
let time = currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds();
console.log(time);
So I have set a BehaviourSubject and tried getting the time dynamically but the value is still static.
Below is my service file code
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AppService {
private time: BehaviorSubject;
constructor(private http:HttpClient) { }
getDate() {
let currentDate = new Date();
let time = currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds();
console.log(time);
return this.time.next(JSON.stringify(time));
}
}
and tried calling it in my home file like this.
ngOnInit() {
return this.appService.getDate();
}
I'm getting error ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'next')
I'm barely new to angular bear with me. I'll appreciate any help.