admin管理员组文章数量:1194755
My friend and I are doing a school project; the task is to make a room-reservation-site. This is our first year trying JavaScript, and we want a string to be formatted into time (h:m). The user of our room-reservation-site can choose what time of the day he/she wants the room, and the output is: H:m (24hour-clock:minutes). For example 12:15.
Therefor my question is: Is there possible to convert this string (h:m) into time, and easily check if the user typed more than 30minutes of booking-time? So we can for example say:
if(start_of_reservation < start_of_reservation(+30minutes)){
alert("You need to book a room atleast 30minutes"); }
else if(start_of_reservation > start_of_reservation(+6hours)){
alert("You can't book a room longer than 6 hours"); }
else {
// moving on..
}
My friend and I are doing a school project; the task is to make a room-reservation-site. This is our first year trying JavaScript, and we want a string to be formatted into time (h:m). The user of our room-reservation-site can choose what time of the day he/she wants the room, and the output is: H:m (24hour-clock:minutes). For example 12:15.
Therefor my question is: Is there possible to convert this string (h:m) into time, and easily check if the user typed more than 30minutes of booking-time? So we can for example say:
if(start_of_reservation < start_of_reservation(+30minutes)){
alert("You need to book a room atleast 30minutes"); }
else if(start_of_reservation > start_of_reservation(+6hours)){
alert("You can't book a room longer than 6 hours"); }
else {
// moving on..
}
Share
Improve this question
asked Apr 17, 2013 at 23:32
Mr.TurtleMr.Turtle
3,0806 gold badges30 silver badges50 bronze badges
1
- 1 There are many javascript date libs: * momentjs.com * datejs.com * google.com/search?q=javascript+date+library most will handle date parsing and comparison. – Tom Carchrae Commented Apr 17, 2013 at 23:40
2 Answers
Reset to default 17var year = '2013';
var month = '04';
var day = '18';
var hour = '12';
var min = '35';
var reserv = new Date(year,month,day,hour,min)
console.log(reserv);
Those year, month and day values you might want to fetch for yourselves by checking the current date. This is purely to show how to convert the string into a date.
Use reserv.getTime()
to convert to milliseconds time and thus being able to compare two times;
reserv.getTime() - reserv2.getTime()
For more information, check the MDN.
Yes. Use regular expressions and the Date object.
RegExps would be used to extract hours and minutes from the date string,
and the Date object would be used for comparisons.
本文标签: jqueryConvert string to time JavaScript (hm)Stack Overflow
版权声明:本文标题:jquery - Convert string to time JavaScript (h:m) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738505105a2090491.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论