# \[Dataviewjs\] Get an automatic humanized, "Morning", "Afternoon", "Evening". \*\*Great for user greetings!

**URL:** <https://forum.obsidian.md/t/dataviewjs-get-an-automatic-humanized-morning-afternoon-evening-great-for-user-greetings/33351>\
**Category:** Workflows & Templates\
**Tags:** dataview\
**Created:** [March 2, 2022, 7:41pm UTC](https://forum.obsidian.md/t/dataviewjs-get-an-automatic-humanized-morning-afternoon-evening-great-for-user-greetings/33351 "2022-03-02T19:41:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Farouk](https://forum.obsidian.md/user_avatar/forum.obsidian.md/farouk/32/42472_2.png) [@Farouk](https://forum.obsidian.md/u/Farouk)\
**Post date:** [March 2, 2022, 7:41pm UTC](https://forum.obsidian.md/t/dataviewjs-get-an-automatic-humanized-morning-afternoon-evening-great-for-user-greetings/33351/1 "2022-03-02T19:41:18Z")

</div>

You don’t need to do anything, it’s automatic. The code will directly by your system write your greeting according to your local time with a time-aware.

You can edit the greeting there to suit your needs or even add your name after it.

Personally I’m using this for my Daily Note, but you can use it for your homepage or anythings you want.

I’m sharing this because I think it’s nice to have a welcome message in our homepage :).

# Copy

````auto
```dataviewjs
const currentHour = moment().format('HH');
console.log(currentHour)
let greeting;
if (currentHour >= 18 || currentHour < 5) {
greeting = '🌙 Good Evening'
} else if (currentHour >= 5 && currentHour < 12) {
greeting = '🌞 Good Morning'
} else {
greeting = '🌞 Good Afternoon'
}
dv.header(2, greeting)
```

````

---

<div class="post-metadata">

**Author:** ![Craig](https://forum.obsidian.md/user_avatar/forum.obsidian.md/craig/32/8831_2.png) [@Craig](https://forum.obsidian.md/u/Craig)\
**Post date:** [March 2, 2022, 8:04pm UTC](https://forum.obsidian.md/t/dataviewjs-get-an-automatic-humanized-morning-afternoon-evening-great-for-user-greetings/33351/2 "2022-03-02T20:04:30Z")

</div>

I love this! Thanks for posting it.

Just for fun, I added a small random element to it:

```javascript

let morning = [
    "Let's do great work today!",
    "It's a new day!"
];

let afternoon = [
    "Let's finish the day well!",
    "Let's keep up our focus!",
    "Need another cup of coffee? ☕"
];

let evening = [
    "Time to go home!",
    "Your family misses you!"
];

const currentHour = moment().format('HH'); 
let greeting; 

if (currentHour >= 17 || currentHour < 5) { 
    greeting = "🌙 Good Evening! "
        + evening[Math.floor(Math.random()*evening.length)];
} else if (currentHour >= 5 && currentHour < 12) { 
    greeting = "🌞 Good Morning! " +
        + morning[Math.floor(Math.random()*morning.length)];
} else { 
    greeting = "🌞 Good Afternoon! "
        + afternoon[Math.floor(Math.random()*afternoon.length)];
} 
dv.paragraph(greeting);

```

---

<div class="post-metadata">

**Author:** ![ianlhayes](https://forum.obsidian.md/user_avatar/forum.obsidian.md/ianlhayes/32/86059_2.png) [@ianlhayes](https://forum.obsidian.md/u/ianlhayes)\
**Post date:** [May 18, 2022, 4:10pm UTC](https://forum.obsidian.md/t/dataviewjs-get-an-automatic-humanized-morning-afternoon-evening-great-for-user-greetings/33351/3 "2022-05-18T16:10:25Z")

</div>

Thanks for this, I love the addition of an element of randomness! Is there any way to get Dataview to return a random greeting from an inline field, i.e. if I were to add `evening:: Take a deep breath` or `morning:: Pour another cup` to some of my daily note pages, could this snippet be modified to pull a sample from one of those?

Really, I’ve been trying to get a [daily gratitude reminder](https://forum.obsidian.md/t/return-random-daily-gratitude-from-daily-note-pages/), but I keep banging my head against the limitations of my scripting knowledge.

---

<div class="post-metadata">

**Author:** ![Craig](https://forum.obsidian.md/user_avatar/forum.obsidian.md/craig/32/8831_2.png) [@Craig](https://forum.obsidian.md/u/Craig)\
**Post date:** [May 18, 2022, 6:18pm UTC](https://forum.obsidian.md/t/dataviewjs-get-an-automatic-humanized-morning-afternoon-evening-great-for-user-greetings/33351/4 "2022-05-18T18:18:18Z")

</div>

Sure, you can pull every instance of a field from all your pages. It requires a little more code to do it. Here’s code that pulls the value of every page that has a “message” field into an array called “messages”:

````auto
```dataviewjs
// List of messages
let messages = [];

// Extract messages from pages that have them
dv.pages()
	.where(page => page.message)
	.forEach(page => {
		dv.array(page.message).forEach(message => {
			messages.push(message); })});

let greeting = messages[Math.floor(Math.random()*messages.length)]

dv.paragraph(greeting);
```

````

---

<div class="post-metadata">

**Author:** ![ianlhayes](https://forum.obsidian.md/user_avatar/forum.obsidian.md/ianlhayes/32/86059_2.png) [@ianlhayes](https://forum.obsidian.md/u/ianlhayes)\
**Post date:** [May 18, 2022, 9:21pm UTC](https://forum.obsidian.md/t/dataviewjs-get-an-automatic-humanized-morning-afternoon-evening-great-for-user-greetings/33351/5 "2022-05-18T21:21:39Z")

</div>

That’s lovely, and works perfectly for my purposes—thank you so much!
