pourmesomecode
pourmesomecode

Reputation: 4318

MomentJS return 0 when formatting time in seconds

Also did a codepen so you can see console log: https://codepen.io/anon/pen/LBKbmZ?editors=0012

So maybe i'm misunderstanding. I have this, moment({ seconds: 0 }).set('hour', 7).seconds().

I create a moment() object. I set the seconds to 0. https://momentjs.com/docs/#/parsing/object/.

I then do set('hour', 7) to set 7 hours. https://momentjs.com/docs/#/get-set/set/.

I then do seconds() to convert the final value to into seconds. https://momentjs.com/docs/#/durations/seconds/.

Based on the docs this should work? I set the seconds to 0, I add 7 hours, i then convert the final time back to seconds. But I'm getting 0 back?

Am I using momentjs wrong or misunderstanding time?

Thank you

Upvotes: 2

Views: 1351

Answers (3)

pourmesomecode
pourmesomecode

Reputation: 4318

So the two suggested answers were helpful but I did a few things different and didn't use the duration method.

Issue: With a time picker which returns an object of {hour: 3} or {minute: 3} depending on what the user selected. Return that value in seconds. That way, internally, i'm always working with seconds and it's just easier.

Now if I use the above methods of duration, I convert the first user input into seconds, I save that, the user then makes a change in minutes, I can add that to the initial value i have saved, what if the user does a lower time? I need to do some logic to figure out if I need to do add or subtraction.

What I want is, a simple; I accept a time of hour or minutes. And I get that time in seconds from the start of the day. This is what I have:

let t = moment({ hour: 0, minute: 0, seconds: 0 }).add(savedTime, 'seconds');

t.set({ [hourOrMinute]: userinputTime }).diff(moment().startOf('day'), 'seconds')

So I set the initial time to 0. I then add the current saved time in seconds to that. I then set the time from what i previously had. I then do a diff, from the start of the day, to current time inputted and return in seconds.

Hopefully not too much text and it helps someone.

Upvotes: -2

Alexander van Oostenrijk
Alexander van Oostenrijk

Reputation: 4754

For an instance of moment, the seconds() method gets you the "seconds" part of a date and time. That value will be between 0 and 59. The documentation for the seconds() method you are referring to belongs to a duration instance, which has a different implementation for this method.

It looks like you are trying to get the number of seconds since a certain starting point. You can create a duration instance and call its seconds() method:

moment.duration({hours: 7}).asSeconds();

The moment instance itself also offers the unix() method, which gets you the number of seconds elapsed since the UNIX epoch (Thursday, 1 January 1970 UTC).

Upvotes: 3

JavaScript
JavaScript

Reputation: 539

As it states in the document you linked asSeconds() returns a duration whereas seconds() returns a timestamp.

So just try this one instead: console.log(moment.duration({hours: 70}).asSeconds(), '<<<----');

Upvotes: 2

Related Questions