2010-10-25 5 views
2

J'ai ce js:Comment ajouter 15 minutes à minuteur personnalisé - Javascript

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

qui obtient la valeur de hachage de la something.com/diary.php#0800 url par exemple.

La valeur est ensuite utilisée pour remplir automatiquement les heures de début et de fin dans un formulaire de rendez-vous.

J'ai besoin de la seconde fois (#appt_end) pour être incrémenté de 15 minutes? Toutes les idées comment je fais cela, mon js est des ordures ...

Merci!

EDIT

Voici le code de travail, je suis maintenant en utilisant:

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

Répondre

3

Vous devez diviser le temps en heures/minutes puis appliquer une logique de temps pour augmenter ..

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 à http://www.jsfiddle.net/gaby/cnnBc/

+0

Salut Gaby est une excellente réponse, comment ajouterais-je à la # suppression? J'ai essayé de tomber dans le hash.replace ("#", ""); de la réponse de FutureKode (merci FK), mais ne semble pas fonctionner? Merci encore. – Robimp

+0

@Robimp, bizarre .. 'parent.location.hash.replace ('#', '')' devrait fonctionner lors de la récupération .. Notez également une erreur dans mon code (* i copier/coller la question *), à à la fin, vous devriez utiliser la variable 'hash' et non la propriété' location.hash' .. donc ça devrait être '$ ('# appt_start'). val (hash)'. mises à jour en réponse .. –

+0

Ah oui, oui c'était tout! J'utilisais alert() sur la var de chaque côté du replace(), et ça avait l'air bien, ce que je suppose que c'était. Cela m'a vraiment perturbé. Merci pour la réponse, ça fonctionne parfaitement maintenant. Question mise à jour avec ce que j'utilise ... – Robimp

0

Serait-il possible comme ça?

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

Vous utilisez une date fictive afin de résumer les minutes. Si le hachage n'a pas le ":", vous pouvez faire une sous-chaîne pour insérer le ":".

+0

Je ne pouvais pas obtenir ce travail, même si je vois ce que vous faites là. – Robimp

1

essayez ceci:

<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> 
+0

bonne réflexion aveC# enlèvement! aurait passé avec cette réponse, mais il passe de 45 à 60, plutôt que de revenir à 00. – Robimp

Questions connexes