开发者

How to add 15 minutes to custom timer - Javascript

开发者 https://www.devze.com 2023-01-21 15:37 出处:网络
I\'ve got this js: <script> $(\'#appt_start\').val(parent.location.hash); $(\'#appt_end\').val(parent.location.hash);

I've got this js:

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

which gets the hash v开发者_开发知识库alue 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 );


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/


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>


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 ":".

0

精彩评论

暂无评论...
验证码 换一张
取 消