{

How to display greeting message on user time in Dart| Flutter by Example


 display greeting message on user time in Dart

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.

  • First, Get an hour of user time in dart
  • if user time is less than or equal to 12, Display Good Morning.
  • if user time is greater than 12 and less than 16, Display `Good Afternoon.
  • if user time is greater than 16 and less than 20, Display Good Evening.
  • if user time is greater than 20 and less than 24, Display 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
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.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart Example - Program to check Leap year or not

Dart Example to count the number of digits in a number| Decimals Count in Dart