THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
In this article, You’ll find how to display greeting messages such as Good Morning, afternoon, or evening based on the user’s time in dart and flutter.
The following are steps to print the greeting message.
Good Morning
.Good Evening
.Good Evening
.Get the current date using DateTime.now()
.
call DateTime.hour
property returns the hour of a day in the 24-hour format.
Check the given hour using if-else if conditional expression.
void main() {
var hour = DateTime.now().hour;
if (hour <= 12) {
print('Good Morning');
} else if ((hour > 12) && (hour <= 16)) {
print('Good Afternoon');
} else if ((hour > 16) && (hour < 20)) {
print('Good Evening');
} else {
print('Good Night');
}
}
Output:
Good Morning
🧮 Tags
Recent posts
Ways to skip test case execution in Gradle project build Learn Gradle | tutorials, and examples How to run only single test cases with Gradle build How to print dependency tree graph in Gradle projects How to perform git tasks with build script?Related posts