我有这个js:

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


例如,它从url something.com/diary.php#0800获取哈希值。

然后,该值用于在约会表单中自动填充开始时间和结束时间。

我需要第二次(#appt_end)增加15分钟吗?任何想法我如何做到这一点,我的js是垃圾...

谢谢!

编辑

这是我现在正在使用的工作代码:

// 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 );

最佳答案

您需要将时间划分为小时/分钟,然后对其应用时间逻辑以增加时间。

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 );


http://www.jsfiddle.net/gaby/cnnBc/签出

关于javascript - 如何将15分钟添加到自定义计时器-Javascript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4013943/

10-14 01:50