[英]format time in hours and minutes javascript
I have the following row in my database in rails. 我在Rails的数据库中有以下行。
time: 10:00:00
In my javascript code, I call the data from the database and time is returned in in this format '2000-01-01T10:00:00.000Z'. 在我的JavaScript代码中,我从数据库中调用数据,并且时间以“ 2000-01-01T10:00:00.000Z”格式返回。 Time set in database is 10:00:00. 数据库中设置的时间为10:00:00。
How do i get the time in HH:MM format in javascript. 如何在JavaScript中以HH:MM格式获取时间。
If i do this, 如果我这样做,
var time = 2000-01-01T10:00:00.000Z;
console.log(time.getHours());
i get an error. 我得到一个错误。 any help appreciated. 任何帮助表示赞赏。
Thanks 谢谢
You need to convert your time to a Date. 您需要将时间转换为日期。 Right now, it's just a string, so that is why you're getting "undefined is not a function" as your error. 现在,它只是一个字符串,所以这就是为什么您将“未定义不是函数”作为错误的原因。
var time = new Date('2000-01-01T10:00:00.000Z');
console.log(time.getHours() + ':' + time.getMinutes());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.