-1

I have three input controls in a form as,

  1. date (which display the current date but user can select any date from a calendar)
  2. start time(current time)
  3. end time(start time + 30 minutes)
    on page load

HTML:

Date:<input type="text" name="txtdate" id="txtdate" />
Start time:<input type="time" name="txtstart" id="txtstart" />
End time:<input type="time" name="txtend" id="txtend" />

I use jquery to get this done and the code is as follows

jquery:

$(document).ready(function() {

$("#txtdate").datepick({dateFormat:'yyyy-mm-dd',minDate: 0});

var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var year1 = d.getFullYear();

var hour = d.getHours();
var mins = d.getMinutes();

var today = year1 + '/' + month + '/' + day;
$("#txtdate").val(today);

var time = hour + ":" + mins;
$("#txtstart").val(time);

var a = time.split(':');
var totmins = (+a[0])* 60 + (+a[1])+30;
var nhour = Math.floor(totmins/60);
var nmins = totmins%60;

var ntime = nhour + ":" + nmins;    
$("#txtend").val(ntime);

});

in the above code it displays the current date, time and adds up 30 minutes to the current time correctly but sometimes when assigning values, the start time and end time fields don't display the time! for an example,

  • when current time is 12.00 AM, no value is displayed in start time and end time input fields

I don't see any error in the code but since i'm new to jquery i need to know if i had done anything wrong here (may be when assigning values) or is there another way to do this?

Note: It should be noted that I have use HTML 5 input type time control to input start time and end time

4
  • When this fails, are you getting any errors in your console? Commented Jan 6, 2015 at 18:12
  • You need to be more specific than "sometimes". Find out when those times are. Commented Jan 6, 2015 at 18:12
  • can you create a jsfiddle that shows your problem? Commented Jan 6, 2015 at 18:17
  • yes in the console it says "failed to load resource: net::ERR_CACHE_MISS". i didn't get this cos i'm new to programming Commented Jan 6, 2015 at 18:25

2 Answers 2

0

My guess to your question is that after adding 30 minute to your Start Time, End Time is already the next day.

Take this for example.

Start Time: 23.50 Based on your calculation, +30min gives you End Time: 24.20

24.20 is an invalid value for input[type='time']

Edit: Jacob's answer solves the problem to your flawed algorithm.

Edit 2: Modifying Jacob's answer, this will get you the endDate

var d = new Date(); //get current time.
var thirtyMinutes = 30*60*1000; //30 minutes in milliseconds
var futureDate = new Date(d.getTime() + thirtyMinutes); //current time in milliseconds + 30 minutes in milliseconds.
var endDateHour = futureDate.getHours();
var endDateMinutes = futureDate.getMinutes();
if (endDateMinutes<10) {
endDateMinutes = "0"+endDateMinutes;
}
var endDate = endDateHour + ':' + endDateMinutes;
Sign up to request clarification or add additional context in comments.

3 Comments

ya i guess this is the problem but even the end time falls in the same day, for an example if start time is 8.30 PM again no value display in end time field. @jcob
I have tested that single digit minute is not accepted. Eg. 12:1 is not accepted, but 12:01 is. That is one exception. Another one is hour value > 23. I have appended the solution to your problem
your answer solved the single digit exception and the code works fine except when the current time is 12.00 AM (mid night). i think its because 12.00 AM throws the value 0. [link]stackoverflow.com/questions/9497724/… this has a solution in javascript
0

Don't bother with this complicated "add 30 minutes" logic when you can simply use Date which is already good at date/time math.

var d = new Date();

// ...

var thirtyMinutes = 30*60*1000;
var futureDate = new Date(d.getTime() + thirtyMinutes);
var ntime = futureDate.getHours() + ':' + futureDate.getMinutes();

2 Comments

I abbreviated to only show the change to calculating ntime.
changed the code like u said, when current time is 12.30 PM no data displayed in end time but when the current time is 12.42 PM the end time displayed correctly as 1.12 PM. i'm sorry but can u give a solution for this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.