date + time to timestamp in angular2

2019-05-11 02:00发布

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

3条回答
啃猪蹄的小仙女
2楼-- · 2019-05-11 02:36

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"

查看更多
【Aperson】
3楼-- · 2019-05-11 02:38

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.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-05-11 02:45

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.

查看更多
登录 后发表回答