How to display different greetings (morning, afternoon, evening) based on the current time? Answered

Post author
Jonathan Gallegos

Anyone know of a way to set conditional logic for a greeting message, like "Good [Morning] , [Afternoon], [Evening], [USER]"?

Comments

5 comments

  • Comment author
    Duncan from Memberstack

    Hey Jonathan 👋 That would take some custom code. Adding to my list for #memberscript-updates

    Well, the date/time thing would require code. You can use attributes to display the user info.

    https://docs.memberstack.com/hc/en-us/articles/11384437115803

    0
  • Comment author
    Jonathan Gallegos

    Perfect, thank you!

    0
  • Comment author
    Julian Galluzzo

    Chatgpt could definitely crank that one out!!

    0
  • Comment author
    Chukwudi

    Here's a code snippet that should work.

    <script>
    window.$memberstackDom.getCurrentMember().then((member) => {
    if (member.data) {
    const now = new Date();
    const currentHour = now.getHours(); let greeting; if (currentHour >= 0 && currentHour < 12) {
    greeting = 'Good morning';
    } else if (currentHour >= 12 && currentHour < 18) {
    greeting = 'Good afternoon';
    } else {
    greeting = 'Good evening';
    } const greetingElement = document.getElementById('greeting'); if (greetingElement) {
    greetingElement.innerText = greeting;
    }
    }
    });
    </script>

    Ensure you have an HTML element with the id of 'greeting' where you want the greeting to appear. The script will update the text content of that element with the appropriate greeting based on the current time of the day.

    0
  • Comment author
    Duncan from Memberstack

    Jonathan Gallegos checkout #memberscript-updates

    0

Please sign in to leave a comment.