views:

76

answers:

3

Hello,

I've got this js:

<script>
$('#appt_start').val(parent.location.hash);
$('#appt_end').val(parent.location.hash);
</script>

which gets the hash value from the url something.com/diary.php#0800 for example.

The value is then used to autofill the start and end times in an appointment form.

I need the second time (#appt_end) to be incremented by 15 minutes? Any ideas how I do this, my js is rubbish...

Thanks!

EDIT

Here's the working code I'm now using:

// add the time into the form
var hashraw = parent.location.hash;
var minIncrement = 15; // how many minutes to increase

hash = hashraw.replace("#", ""); // remove the hash

// first we split the time in hours and mins
var hours = parseInt(hash.substring(0, 2),10); // get hours (first 2 chars)
var mins = parseInt(hash.substring(2, 4),10); // get mins (last 2 chars)

// add the new minutes, and enforce it to fit 60 min hours 
var newMins = (mins + minIncrement )%60; 
// check if the added mins changed thehour 
var newHours = Math.floor( (mins + minIncrement ) / 60 );

// create the new time string (check if hours exceed 24 and restart it
// first we create the hour string
var endTime = ('0' + ((hours+newHours)%24).toString()).substr(-2);
// then we add the min string
endTime += ('0'+ newMins.toString()).substr(-2);

$('#appt_start').val(hash);
$('#appt_end').val( endTime );
A: 

Would it be possible like this?

Working Demo

var hash="08:00";
$('#appt_start').val(hash);

var d = new Date("October 13, 1975 "+hash) 
d.setMinutes(d.getMinutes()+15);

$('#appt_end').val(d.getHours()+":"+d.getMinutes());

You use a fictitious date so to sum the minutes. If the hash has not the ":", you can do a substring to insert the ":".

netadictos
i couldn't get this to work, though i see what you're doing there.
Robimp
+1  A: 

try this:

    <script>
        $(function() {
$('#appt_start').val(parent.location.hash);
var end = parent.location.hash;
end = end.replace("#", "");
end = (end * 1) + 15;
if (end < 1000) {end = '0' + end};
$('#appt_end').val(end);
});
</script>
FutureKode
good thinking with # removal! would've gone with this answer, but it increments from 45 to 60, rather than back to 00.
Robimp
+3  A: 

You need to split the time in hours / mins and then apply time logic to it to increase it..

var hash = parent.location.hash.replace('#','');
var minIncrement = 15; // how many minutes to increase

// first we split the time in hours and mins
var hours = parseInt(hash.substring(0, 2),10); // get hours (first 2 chars)
var mins = parseInt(hash.substring(2, 4),10); // get mins (last 2 chars)

// add the new minutes, and enforce it to fit 60 min hours 
var newMins = (mins + minIncrement )%60; 
// check if the added mins changed thehour 
var newHours = Math.floor( (mins + minIncrement ) / 60 );

// create the new time string (check if hours exceed 24 and restart it
// first we create the hour string
var endTime = ('0' + ((hours+newHours)%24).toString()).substr(-2);
// then we add the min string
endTime += ('0'+ newMins.toString()).substr(-2);

$('#appt_start').val( hash );
$('#appt_end').val( endTime );

Check it out at http://www.jsfiddle.net/gaby/cnnBc/

Gaby
Hi Gaby is a great answer, how would I add in the # removal? I've tried dropping in the hash.replace("#", ""); from FutureKode's answer (thanks FK), but doesn't seem to work? Thanks again.
Robimp
@Robimp, weird.. `parent.location.hash.replace('#','')` should work when retrieving it .. Also note a mistake in my code (*i copy/pasted the question*), at the end you should use the `hash` variable and not the `location.hash` property .. so it should be `$('#appt_start').val( hash )`. updates in answer..
Gaby
Ah right, yeah that was it! I was using alert() on the var either side of the replace(), and it looked fine, which I guess it was. That really confused me. Thanks for the answer, it works perfect now. Question updated with what I'm using...
Robimp