Showing Morning, afternoon, evening, night message based on Time in java
You should be doing something like:
Calendar c = Calendar.getInstance();
int timeOfDay = c.get(Calendar.HOUR_OF_DAY);
if(timeOfDay >= 0 && timeOfDay < 12){
Toast.makeText(this, "Good Morning", Toast.LENGTH_SHORT).show();
}else if(timeOfDay >= 12 && timeOfDay < 16){
Toast.makeText(this, "Good Afternoon", Toast.LENGTH_SHORT).show();
}else if(timeOfDay >= 16 && timeOfDay < 21){
Toast.makeText(this, "Good Evening", Toast.LENGTH_SHORT).show();
}else if(timeOfDay >= 21 && timeOfDay < 24){
Toast.makeText(this, "Good Night", Toast.LENGTH_SHORT).show();
}
Display greeting (morning/afternoon/evening) using both hours and minutes
First, change date.getMinutes()
to myDate.getMinutes()
.
Then, you can compare them like this:
if (hrs >= 5 && ((hrs == 5 && mins >= 30) || (hrs > 5 && hrs < 12)))
greet = 'Good Morning';
else if (hrs >= 12 && hrs < 18)
greet = 'Good Afternoon';
else if ((hrs >= 18 && hrs < 24) || hrs > 0)
greet = 'Good Evening';
else
greet = 'Error';
Note that I added an else at the end for weird cases.
The snippet below shows this in action. Don't worry about the other code; it's just there to make the tests output readable.
function doATest(timeIn) { var myDate = new Date(timeIn ? "0 " + timeIn : null); var hrs = myDate.getHours(); var mins = myDate.getMinutes(); // changed date to myDate var greet;
// morning | 5:30-11:59 // afternoon | 12:00-17:59 // evening | 18:00-05:29 if (hrs >= 5 && ((hrs == 5 && mins >= 30) || (hrs > 5 && hrs < 12))) greet = 'Good Morning'; else if (hrs >= 12 && hrs < 18) greet = 'Good Afternoon'; else if ((hrs >= 18 && hrs < 24) || hrs > 0) greet = 'Good Evening'; else greet = 'Error';
document.getElementById('timeTable').innerHTML += '<tr>' + '<td>' + (timeIn ? timeIn : "Right Now") + '</td>' + '<td>' + greet + '</td>' + '</tr>';}
doATest("1:00");doATest("1:37");doATest("5:00");doATest("5:15");doATest("5:29");doATest("5:29:59");doATest("5:30");doATest("5:45");doATest("6:00");doATest("6:15");doATest("6:30");doATest("6:45");doATest("6:45");doATest("7:00");doATest("waffles"); // not validdoATest("9:45");doATest("11:00");doATest("11:30");doATest("11:45");doATest("11:59");doATest("11:59:59");doATest("12:00");doATest("12:34");doATest("12:48");doATest("13:00");doATest("13:57");doATest("15:00");doATest("17:00");doATest("17:20");doATest("17:40");doATest("17:59");doATest("17:59:59.999");doATest("18:00");doATest("17:76"); // not validdoATest("18:36");doATest("18:63"); // not validdoATest("19:00");doATest("20:00");doATest("23:00");doATest("24:00"); // really 0:00 the next daydoATest("27:00"); // not valid
<html>
<head> <title>ITP1</title></head>
<body> <table> <thead> <tr> <th>Time</th> <th>Greeting</th> </tr> </thead> <tbody id="timeTable"></tbody> </table></body>
</html>
How can I show greetings like Good Moring, afternoon or evening base on users time in flutter
Try using DateTime.now()
, for example:
String greeting() {
var hour = DateTime.now().hour;
if (hour < 12) {
return 'Morning';
}
if (hour < 17) {
return 'Afternoon';
}
return 'Evening';
}
Displaying morning, afternoon, or evening to user based on time using variables
If you are linking the javascript file before the span, thenUncaught TypeError: Cannot set property 'innerHTML' of null at master.js:13
will occur since it can't find the span yet. Try:
window.onload = function(e){
document.getElementById("time").innerHTML = time;
}
Greeting According to time of day (Android)
You can try like this, you missed less than 6 AM.
if(hour>= 12 && hour < 17){
greeting = "Good Afternoon";
} else if(hour >= 17 && hour < 21){
greeting = "Good Evening";
} else if(hour >= 21 && hour < 24){
greeting = "Good Night";
} else {
greeting = "Good Morning";
}
How to display Good Morning/Afternoon/Evening using javascript in 12hours format
The issue is because you are using the modulo operator. This means that your h > 12
check will never be hit as the remainder of the division cannot be greater than 12
. It's because of this your logic always believes it's still the morning. To fix this, just use a simple <
check when comparing the hour figure.
Also note that you have some issues with the formatting of the date, such as appending extra zeroes so you end up with 011
as hour values. You can fix this by using slice()
.
With all that said, try this:
$(document).ready(function() { function dateTime() { var ndate = new Date(); var hours = ndate.getHours(); var message = hours < 12 ? 'Good Morning' : hours < 18 ? 'Good Afternoon' : 'Good Evening'; $("h3.day-message").text(message);
$('.date').html(hours.leadingZeroes(2) + ":" + ndate.getMinutes().leadingZeroes(2) + ":" + ndate.getSeconds().leadingZeroes(2) + (hours < 12 ? 'AM' : 'PM')); }
setInterval(dateTime, 1000);});
Number.prototype.leadingZeroes = function(len) { return (new Array(len).fill('0', 0).join('') + this).slice(-Math.abs(len));}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><h3 class="day-message"></h3><span class="date"></span>
JavaScript to Output Text Based on User's Current Time
Try following piece of Javascript code:
var today = new Date()
var curHr = today.getHours()
if (curHr < 12) {
console.log('good morning')
} else if (curHr < 18) {
console.log('good afternoon')
} else {
console.log('good evening')
}
Related Topics
Regular Expression to Remove Everything But Characters and Numbers
Add Secure Flag to Jsessionid Cookie in Spring Automatically
Issue With Java 8 Collectors Type Mismatch: Cannot Convert from List<Object> to List<String>
Print Array Without Brackets and Commas
How a Jar File Can Read an External Properties File
Save Child Objects Automatically Using JPA Hibernate
How to Post a Json Payload to a @Requestparam in Spring MVC
Spring Boot Unsupported Media Type With @Requestbody
Write to Text File from Multiple Threads
Looping Within Multiple Arrays in Java Stream
Remove Trailing Comma from Comma-Separated String
Mockito Mock Objects Returns Null
Better Way to Detect If a String Contains Multiple Words
Java Jackson Deserialization of Nested Objects
How to Pass a List and a Separate String Using @Requestbody in Springboot