Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date + time to timestamp in angular2

Am trying to learn angular2+ and i want to make a GoogleCalendar's like scheduler app. After many research, i decided to use PrimeNG. The output format of the calendar is

2016-01-16T16:00:00

that's seems great and complete. But the api i want to interract with uses timeStamp...

I tried to make a Javascript function who's parsing my date format :

   function toTimestamp(strDate){
   var datum = Date.parse(strDate);
   return datum/1000;
}
alert(toTimestamp('02/13/2009 23:31:30'));

But my problem is that i can't use the format of PrimeNG...

Did anyone know how can i interact corectly with the format i need to convert to a timestamp?

else did anyone know how can i get this date format ( 2016-01-16T16:00:00 ) to a timestamp using angular2+ ? Thanks very much !!

like image 645
moonshine Avatar asked Dec 23 '22 16:12

moonshine


2 Answers

You can use plain javascript:

let time = new Date("2016-01-16T16:00:00");
alert(time.getTime());

This will return you a timestamp. Just be aware of timezones.

like image 174
Igor Janković Avatar answered Dec 28 '22 11:12

Igor Janković


You can use momentjs to get the desired timestamp.

eg: moment("2016-01-16T16:00:00").format("MM/DD/YYYY HH:mm:ss")

The output will be:

"01/16/2016 16:00:00"

like image 27
Dhyey Avatar answered Dec 28 '22 10:12

Dhyey