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

14,865

Solution 1

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();

Solution 2

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/

Share:
14,865

Related videos on Youtube

ananya
Author by

ananya

Updated on May 27, 2022

Comments

  • ananya
    ananya almost 2 years

    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.