ananya
ananya

Reputation: 1041

How to convert current date to timestamp in Angular 2/4

I want to convert current date to timestamp.Suppose todays date is 26/4/2019 , then i am doing like this.

const current = new Date();
const timestamp = current.getTime();

but this gives me 1556532767266. When i am getting date from this , it shows me 'Mon Apr 26 2019 10:12:47' in UTC and 'Mon Apr 26 2019 15:42:47' in local. but i want in this 'Mon Apr 26 2019 00:00:00', which is night 12Am

Can anyone please help me how to get like this.

Upvotes: 3

Views: 30599

Answers (2)

Michael Oosthuizen
Michael Oosthuizen

Reputation: 144

To solve your problem, and many others when dealing with date/times, use Moment.js.

var moment = require("moment");
var current_timestamp = moment().format("ddd MMM D YYYY 00:00:00");

A moment() call initializes to the current time, but Moment objects can also accept JavaScript Date objects:

var current_timestamp = moment(new Date());

Check out all the formats available for Moment to convert timestamps any way you want. https://momentjs.com/docs/#/displaying/format/

Upvotes: 1

Hien Nguyen
Hien Nguyen

Reputation: 18975

You can set Hours/Min/Seconds to 0 and getTime()

const current = new Date();

current.setHours(0)

current.setMinutes(0)

current.setSeconds(0)

current.setMilliseconds(0)

const timestamp = current.getTime();

Upvotes: 5

Related Questions