Skip to main content

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

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

5 comments

  • Duncan from Memberstack
    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
  • Jonathan Gallegos
    Jonathan Gallegos OP

    Perfect, thank you!

    0
  • Julian Galluzzo
    Julian Galluzzo

    Chatgpt could definitely crank that one out!!

    0
  • Chukwudi
    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
  • Duncan from Memberstack
    Duncan from Memberstack

    Jonathan Gallegos checkout #memberscript-updates

    0

Please sign in to leave a comment.

Sitemap