date + time to timestamp in angular2

醉酒当歌 提交于 2019-12-22 08:17:45

问题


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


回答1:


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.




回答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"




回答3:


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.



来源:https://stackoverflow.com/questions/44775904/date-time-to-timestamp-in-angular2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!