Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- display a message based on the time of the day in javascript?
- You can display a message based on the time of the day in JavaScript using the `Date` object to get the current hour. Here's how you can do it:
- ```javascript
- function getMessageByTime() {
- const hour = new Date().getHours();
- if (hour >= 5 && hour < 12) {
- return "Good morning!";
- } else if (hour >= 12 && hour < 18) {
- return "Good afternoon!";
- } else {
- return "Good evening!";
- }
- }
- // Example usage
- const message = getMessageByTime();
- console.log(message); // Output depends on the current time of the day
- ```
- In this code:
- - We create a function `getMessageByTime` that determines the current hour using `new Date().getHours()`.
- - Based on the hour, we return different messages for morning, afternoon, and evening.
- - We then call the function and store the message in a variable, and finally log the message to the console.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement