boom
boom

Reputation: 11676

How does Date.now() work in javascript?

I'm trying to create a time value I can easily relate to others.

var created = Date.now() * 1000

var day_ago = Date.now() * 1000 - 24*60*60

if(created > day_ago){console.log('it was created in the last day')}

This doesn't seem to work correctly, anyone know what I'm doing wrong

Upvotes: 0

Views: 242

Answers (2)

Anoop
Anoop

Reputation: 23208

Date.now() will return milli seconds devide by 1000 will convert that in seconds.

var created = Date.now() / 1000

var day_ago = Date.now()/ 1000 - 24*60*60

if(created > day_ago){console.log('it was created in the last day')}

Upvotes: 1

BAK
BAK

Reputation: 1005

You must divide by 1000, not multiply

var created = Date.now() / 1000
var day_ago = Date.now() / 1000 - 24*60*60
if(created > day_ago){console.log('it was created in the last day')}

Upvotes: 0

Related Questions