I have three text boxes. (1) Start time | (2) End Time | (3) Shift - Read Only
I want to automatically set the value of txtShift to DAY or NIGHT only depending on txtStart and txtEnd.

Am not requesting of a specific/perfect code for this problem, just want you guys to help me out on how should i solve this.

Please help. Thank you in advance.

Dani AI

Generated

Following 's high-level If/Else and 's time bands, pick one classification rule and make inputs reliable. Easiest and most user-friendly: use HTML5 time fields (type="time") or a time picker so you always get "HH:MM". Then decide the rule you want:

  • Simple (recommended): classify by the start time only — predictable and cheap to implement.
  • Stricter: require the whole interval to lie inside the day/night window.
  • Robust: classify by majority of minutes in the interval (handles crossings of midnight).

Below is a compact JavaScript example that assumes day = 08:00-20:00. It includes both the start-based and majority-based classifiers and handles intervals that cross midnight. Attach updateShift() to your start/end inputs' change events and keep the Shift field readOnly.

// assumes <input type="time" id="startTime">, <input type="time" id="endTime">, <input id="shift" readonly>
function toMinutes(t){ var p=t.split(':'); return parseInt(p[0],10)*60 + parseInt(p[1],10); }
function overlap(a1,a2,b1,b2){ var s=Math.max(a1,b1), e=Math.min(a2,b2); return Math.max(0,e-s); }

function classifyByStart(sMin){
  var dayStart=8*60, dayEnd=20*60;
  return (sMin >= dayStart && sMin < dayEnd) ? 'DAY' : 'NIGHT';
}

function classifyByMajority(sMin,eMin){
  var DAY_START=8*60, DAY_END=20*60, DAY_MINUTES=DAY_END-DAY_START;
  var total = (eMin - sMin + 24*60) % (24*60);
  if(total === 0) return classifyByStart(sMin); // treat equal times as "by start" or choose your policy
  var dayOverlap = 0;
  if(sMin < eMin){
    dayOverlap = overlap(sMin,eMin,DAY_START,DAY_END);
  } else { // crosses midnight
    dayOverlap = overlap(sMin,24*60,DAY_START,DAY_END) + overlap(0,eMin,DAY_START,DAY_END);
  }
  return (dayOverlap * 2 >= total) ? 'DAY' : 'NIGHT';
}

function updateShift(useMajority){
  var s=document.getElementById('startTime').value;
  var e=document.getElementById('endTime').value;
  if(!s || !e) return;
  var sMin=toMinutes(s), eMin=toMinutes(e);
  document.getElementById('shift').value = useMajority ? classifyByMajority(sMin,eMin) : classifyByStart(sMin);
}

Quick tips: validate input formats, decide how to treat identical start/end (zero vs 24h), and prefer the start-based rule unless you really need the more complex behavior. If users will mistype, a dropdown or picker reduces bugs.

Recommended Answers

All 2 Replies

Generically it would be

If SOME CONDITION Then
    txtShift.Text = "DAY"
Else
    txtShift.Text = "NIGHT"
End If

I can't be more specific than that because I don't know what range of values defines DAY or NIGHT. If there are a restricted set of possible values then you would be better off using a drop down list to select the values. Can you provide more info?

Shift Range: Start Time ~ End Time

DAY -- 0800H ~ 2000H
NIGHT -- 2000H ~ 0800H

User is allowed to input any time within the range.
Example:
[Start time] 13:20
[End Time] 14:25
Therefore, shift will be DAY

Do you recommend using drop down list for this case? or text box perhaps?
Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.