简体   繁体   English

当前时间最接近半小时,下一小时时间

[英]Current hour nearest to half an hour with next hour time

I have two textbox with value to set start time and end time.我有两个带有值的文本框来设置开始时间和结束时间。 If user has selected these times then it will be display those value.如果用户选择了这些时间,那么它将显示这些值。 But, if user is creating a new appointment then by default it should display nearest half an hour time to current one and end time should be next hour duration.但是,如果用户正在创建一个新约会,那么默认情况下它应该显示最接近当前时间的半小时时间,结束时间应该是下一小时的持续时间。

For example,例如,

The current time is 4:37PM then the start time should be 5:00PM and end time should be 6:00PM If the current time is 7:31AM then the start time should be 8:00AM and end time should be 9:00AM.当前时间为下午 4:37,则开始时间应为下午 5:00,结束时间应为下午 6:00 如果当前时间为上午 7:31,则开始时间应为上午 8:00,结束时间应为上午 9:00。 If the current time is 11:45AM then the start time should be 12:00PM and end time should 01:00PM如果当前时间是上午 11:45,那么开始时间应该是下午 12:00,结束时间应该是下午 01:00

I updated the example a bit, so that when the user sets in 8:00 it stays at 8:00, anything else goes to the next 30 minutes (which you could easily change)我稍微更新了示例,这样当用户在 8:00 设置时,它会停留在 8:00,接下来的 30 分钟(您可以轻松更改)

A downside of this approach is that setting 8:30 makes it 9:00 as well, whereas you could possibly argue that 8:30 is valid as well, as it is at 30 minutes intervals这种方法的缺点是将 8:30 设置为 9:00,而您可能会争辩说 8:30 也是有效的,因为它以 30 分钟为间隔

 window.addEventListener('load', function() { const startElement = document.querySelector('#startTime'); const endElement = document.querySelector('#endTime'); const detailsElement = document.querySelector('#appointment_details'); function createTimeString( hours, minutes ) { if (hours < 0 || minutes < 0) { throw 'argument exception'; } console.log( 'original input ' + hours + ':' + minutes ); while (parseInt(hours) >= 24) hours = +hours - 24; while (parseInt(minutes) >= 60) { minutes = +minutes - 60; hours = +hours + 1; } return hours.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0'); } function handleStartTimeChanged( e ) { const timeValue = e.target.value; let [hours,minutes] = timeValue.split(':'); if (Math.round( parseInt( minutes ) / 60.0) === 1) { hours = parseInt(hours) + 1; minutes = 0; } else if (parseInt( minutes ) !== 0) { // anything else is same hour but 30 minutes past minutes = 30; } setAppointmentDetails( createTimeString( hours, minutes ), createTimeString( +hours + 1, minutes ) ); } function handleEndTimeChanged( e ) { const timeValue = e.target.value; let [hours,minutes] = timeValue.split(':'); if (Math.round( parseInt( minutes ) / 60.0) === 1) { hours = parseInt(hours) + 1; minutes = 0; } else if (parseInt( minutes ) !== 0) { // anything else is same hour but 30 minutes past minutes = 30; } setAppointmentDetails( createTimeString( hours - 1, minutes ), createTimeString( hours, minutes ) ); } function setAppointmentDetails( startTime, endTime ) { detailsElement.innerHTML = `${startTime} till ${endTime}`; } startElement.addEventListener('change', handleStartTimeChanged ); endElement.addEventListener('change', handleEndTimeChanged ); } );
 <div class="line"> <span class="label">Appointment start:</span> <span class="field"><input type="time" id="startTime" /></span> <span class="label">Appointment end:</span> <span class="field"><input type="time" id="endTime" /></span> </div> <div id="appointment_details"> </div>

This involves some fairly simple calculations, and Math.ceil :这涉及一些相当简单的计算和Math.ceil

 const secsPerHalfHour = 1000 * 60 * 30 const nextHalfHour = () => new Date (Math .ceil (new Date() .getTime() / secsPerHalfHour) * secsPerHalfHour) const next90Mins = () => new Date (Math .ceil ((new Date() .getTime() / secsPerHalfHour) + 2) * secsPerHalfHour) console .log (`start: ${nextHalfHour()}`) console .log (`end: ${next90Mins()}`)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM