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
Julia examples - Variable Type Nim example - Convert String to/from the Int How to get length of an array and sequence in Nim? Nim environment variables - read, set, delete, exists, and iterate examples? How to convert from single character to/from string in Nim?Related posts