-1

I am trying to set a Day.js hour object to an element. I am using dayjs().hour(9), but it is returning too much information. I just want the hour block but when I console.log it comes back with day of the week date time with mins and seconds. In contrast when I console.log(dayjs().hour()), I just get the current hour. What else can I do?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 2
    _"which should log 9 in the console"_ - why? Per [the docs](https://day.js.org/docs/en/get-set/hour) `.hour(9)` sets the hour part to 9 _and returns a new dayjs object_. If you just want the value `9`, you already have it! – jonrsharpe Apr 26 '23 at 14:32
  • Previous comment as code snippet: `console.log(9)`. – jabaa Apr 26 '23 at 14:42
  • I guess I didn't explain it very well. I have variable that I am trying to assign dayjs().hour(9) value too. then I am using the isBefore or isAfter to compare that variable to the current dayjs().hour and then applying different CSS classes based on the results. But instead of hour 9 being compared to the current hour, dayjs is setting, the date, day and time with seconds and minutes. So my code is not working as I want. – Valais Girl Apr 27 '23 at 19:57
  • What exactly is unclear? `dayjs().hour(9)` sets the hour value to 9 and returns the date object. In your question, you ask about `console.log`. If that's not your actual question, ask your actual question. You asked how to set an hour to an object and return the hour. You can use the comma operator: `dayjs().hour(9), 9`. It sets the hour and returns the number `9`. – jabaa Apr 27 '23 at 20:47
  • I'm Console logging the values to show that its not working – Valais Girl Apr 28 '23 at 13:48
  • When I console log the value of dayjs().hour(), I get the current hour logged or 9. When I set the value for hour9 = dayjs().hour(9) it returns M {$L: 'en', $d: Fri Apr 28 2023 09:53:40 GMT-0500 (Central Daylight Time), $x: {…}, $y: 2023, $M: 3, …} $D : 28 $H : 9 $L : "en" $M : 3 $W : 5 $d : Fri Apr 28 2023 09:53:40 GMT-0500 (Central Daylight Time) {} $m : 53 $ms : 417 $s : 40 $x : {} $y : 2023 [[Prototype]] : Object I can't compare those two values in with isAfter, isBefore. they are not equal. – Valais Girl Apr 28 '23 at 13:55
  • Then, set the hour and get the hour. It's nonsense, but it seems to be what you want: `dayjs().hour(9).hour()`. See https://jsfiddle.net/8cgvjryt/ – jabaa Apr 28 '23 at 14:36

1 Answers1

0

dayjs() creates a new dayjs object with the current timestamp.

dayjs().hour() is a getter. It returns the hour value of the object.

dayjs().hour(9) is setter with fluent interface. It sets the hour value of the object and returns the whole dayjs object.

The fluent interface allows method chaining. You can set the value and get the value with dayjs().hour(9).hour().

Example:

console.log(dayjs().hour(9).hour());
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.7/dayjs.min.js"></script>

It's obvious that a code like dayjs().hour(9).hour() doesn't make much sense, because it creates a temporary object, sets the hour value, returns the same value and throws away the object. I assume that this is just a minimal code snippet to illustrate your case.

You can validate that the setter works with

console.log(dayjs().hour(9).isBefore(dayjs().hour(10)));
console.log(dayjs().hour(9).isBefore(dayjs().hour(8)));
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.7/dayjs.min.js"></script>
jabaa
  • 5,844
  • 3
  • 9
  • 30