5

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 !!

moonshine
  • 799
  • 2
  • 11
  • 24
  • Check out `date` pipe, you can manipulate with dates and time using this pipe: https://angular.io/api/common/DatePipe – Haseoh Jun 27 '17 at 08:55

3 Answers3

8

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.

Igor Janković
  • 5,494
  • 6
  • 32
  • 46
2

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"

Dhyey
  • 4,275
  • 3
  • 26
  • 33
1

You are on the right path as far as i can see. I suggest you use the Javascript Date https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date to create a new Date instead of trying to parse the raw output of primeng.

new Date() can be use with a multitude of parameters. If new Date(datestring) is not working out as I would expect, use split to split your output string into variables that u can use to fill

new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);

This might be a little bit of sweat work but it should do the trick.

This is my first answer so please mods be gentle with my formatting.

Haseoh
  • 910
  • 3
  • 18
  • 38
Berlin_Bro
  • 109
  • 6