DEV Community

Olatunji Ayodele Abidemi
Olatunji Ayodele Abidemi

Posted on

TimeController in World

class TimeController {
// Current time
private currentTime: Date;

constructor() {
this.currentTime = new Date();
}

// Set the current time
public setCurrentTime(time: Date): void {
this.currentTime = time;
}

// Get the current time
public getCurrentTime(): Date {
return this.currentTime;
}

// Add minutes to the current time
public addMinutes(minutes: number): void {
this.currentTime = new Date(this.currentTime.getTime() + minutes * 60000);
}

// Subtract minutes from the current time
public subtractMinutes(minutes: number): void {
this.currentTime = new Date(this.currentTime.getTime() - minutes * 60000);
}

// Format the current time as a string
public formatCurrentTime(): string {
return this.currentTime.toLocaleTimeString();
}
}

// Example usage:
const timeController = new TimeController();
console.log('Current Time:', timeController.formatCurrentTime());
timeController.addMinutes(30);
console.log('Updated Time:', timeController.formatCurrentTime());

Top comments (0)