Convert string to time JavaScript (h:m)

后端 未结 2 1809
陌清茗
陌清茗 2020-12-07 00:52

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

相关标签:
2条回答
  • 2020-12-07 01:30
    var 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.

    0 讨论(0)
  • 2020-12-07 01:42

    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.

    0 讨论(0)
提交回复
热议问题