Damon
Damon

Reputation: 4524

Get current time in seconds using moment.js

I am trying to use moment.js to get the current time in seconds. I am really struggling, and apologize for asking what is probably a simple problem.

I have this so far:

const time = await moment().format("hh:mm:ss");
// current time: 11:17:21

expires_in:'3599' (string)

The expires_in is provided from an api call that is given to me in seconds. For example, 3599 (which is about an hour when converted from seconds to minuets).

I want to get the current time (from the client) convert that to seconds. Then I'd like to add the expires_in to the current time. If that time is > then the current time, I know I need to request a new token.

Thank you for any suggestions!

Upvotes: 1

Views: 1239

Answers (1)

Hassan Imam
Hassan Imam

Reputation: 22574

You can use add method to add expires_in seconds to your time.

const date = moment(),
      curr = +moment(),
      time = date.format("hh:mm:ss"),
      expires_in = '3599',
      newTime = date.add(expires_in, 'seconds').format("hh:mm:ss"),
      expires = +date.add(expires_in, 'seconds');
console.log(time, newTime);
console.log(curr, expires, curr > expires);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>

Upvotes: 2

Related Questions