Mendes
Mendes

Reputation: 18451

JavaScript get start of current minute

I'm trying to get the value (elapsed milliseconds) equivalent of the start of the current minute using JavaScript:

let now = Date.now().setSeconds(0);

That is leading to an error:

Date.now(...).setSeconds is not a function

The same error appears if I try""

let now = Date.now();
now.setSeconds(0);

What is the correct way to make it work? I expect to return, for example, 1543484520000 for Thu Nov 29 2018 07:42:00 GMT-0200

Upvotes: 2

Views: 784

Answers (4)

Taha Paksu
Taha Paksu

Reputation: 15616

You can use the timestamp to remove the seconds.

var timestamp = new Date(+new Date() - ((+new Date()) % 60000));
console.log(+timestamp);

Upvotes: 0

Lovro
Lovro

Reputation: 185

Try this:

let now = new Date();
now.setSeconds(0);
console.log(now.getMilliseconds());
console.log(now.getTime());

Once you have the date object, you can get out what you need. See documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Upvotes: 0

Mr. Roshan
Mr. Roshan

Reputation: 1805

Try this

To get Seconds :-

date = new Date();
milliseconds = date.getTime();
seconds = milliseconds / 1000;
console.log("milliseconds : "+milliseconds + " seconds : "+seconds );

To set seconds :-

let date = new Date();
date.setSeconds(0);
milliseconds = date.getTime();
seconds = milliseconds / 1000;
console.log("milliseconds : "+milliseconds + " seconds : "+seconds );

Upvotes: 0

BambiOurLord
BambiOurLord

Reputation: 372

This is what you want I think

let now = new Date();
now.setSeconds(0);
console.log(now.getTime());

Upvotes: 3

Related Questions