Jump to content


Photo

A Hora Naum Está Aparecendo Completa...


  • Faça o login para participar
11 replies to this topic

#1 Sir Curse

Sir Curse

    Normal

  • Usuários
  • 113 posts
  • Sexo:Não informado
  • Localidade:São Paulo
  • Interesses:Linguagens em geral

Posted 03/11/2003, 01:20

eu tenho esse código:

<html>
<head>
<title>ScriptBrasil</title>
<script language="javascript">
var timerID = null

var timerRunning = false
function stopclock(){
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}
function startclock(){
    stopclock()
    showtime()
}
function showtime(){

    var now = new Date()

    var hours = now.getHours()

    var minutes = now.getMinutes()

    var seconds = now.getSeconds()

    var month = now.getMonth()

    var datum = now.getDate()

    var year = now.getYear()

    var timeValue = ((hours > 12) ? hours - 12 : hours)

    timeValue += ((minutes < 10) ? "0" : "") + minutes

    timeValue += (hours >= 12) ? " pm" : "am"

    document.form1.hora.value = timeValue

    timerID = setTimeout("showtime()",1000)

    timerRunning = true
}
</script>
</head>

<BODY onLoad="startclock()">
<FORM NAME="form1">
<INPUT TYPE="text" NAME="hora" SIZE=8 VALUE ="">
</FORM>
</BODY>

</html>


ele funciona perfeitamente! só menos por um problema, eu estou trabalhando com PHP e preciso que ele armazene SEMPRE horas com 6 algarismos, por exemplo, agora seria (02:19 am) ele teria que armazenar (0219am) sempre com um zero na frente... mas ele esta retirando o zero da frente do 2, issu faz com que de erro no script em PHP, tem algum jeito dele SEMPRE mostrar o zero na frente quando o numero for menor que 10?

#2 Chinello Cybernético

Chinello Cybernético

    Simplesmente "Chinello"

  • Usuários
  • 1892 posts
  • Sexo:Masculino
  • Localidade:Canoas - RS

Posted 03/11/2003, 01:41

Adicionei a seguinte linha ao seu código:

hours = (hours < 10) ? "0"+hours : hours;

Ou seja, se a hora for menor q 10, ele recebe o 0 na frente.. ;)

O código completo ficaria:

<html>
<head>
<title>ScriptBrasil</title>
<script language="javascript">
var timerID = null

var timerRunning = false
function stopclock(){
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}
function startclock(){
    stopclock()
    showtime()
}
function showtime(){

    var now = new Date()

    var hours = now.getHours()
    hours = (hours < 10) ? "0"+hours : hours;

    var minutes = now.getMinutes()

    var seconds = now.getSeconds()

    var month = now.getMonth()

    var datum = now.getDate()

    var year = now.getYear()

    var timeValue = ((hours > 12) ? hours - 12 : hours)

    timeValue += ((minutes < 10) ? "0" : "") + minutes

    timeValue += (hours >= 12) ? " pm" : "am"

    document.form1.hora.value = timeValue

    timerID = setTimeout("showtime()",1000)

    timerRunning = true
}
</script>
</head>

<BODY onLoad="startclock()">
<FORM NAME="form1">
<INPUT TYPE="text" NAME="hora" SIZE=8 VALUE ="">
</FORM>
</BODY>

</html>

Prontinho.. :D

Fui.. :P
Diego Sampaio - kroW - PHP Framework > http://chinelloweb.net/
chinello at gmail dot com

System > Athlon 64 3200+ Mobile | Kubuntu 7.04 Fesity Fawn AMD64 | Kernel 2.6.20-16
Coding @ KDE 3.5.7 | PHP 5.2.1 | Apache 2.2.3 | MySQL 5.0.38

#3 Sir Curse

Sir Curse

    Normal

  • Usuários
  • 113 posts
  • Sexo:Não informado
  • Localidade:São Paulo
  • Interesses:Linguagens em geral

Posted 03/11/2003, 01:49

opa! vlw mesmu! nem sei o que dizer... o squal e vc ajudo muito hoje!!! :D brigadaum!

#4 Sir Curse

Sir Curse

    Normal

  • Usuários
  • 113 posts
  • Sexo:Não informado
  • Localidade:São Paulo
  • Interesses:Linguagens em geral

Posted 03/11/2003, 23:44

ihh rapá! fico legal, mas quando a hora é maior que 12... ou seja... 13 horas é a mesma coisa que 1 hora, ele transforma o 13 em 1 e não coloca o zero na frente ja que 13 é maior que 10... entendeu? ele adiciona o zero na frente, mas só se é REALMENTE horas abaixo de 10... no caso de horas de 13 a 24 ele naum adiciona.... axo que ta confundindu... :|

#5 Chinello Cybernético

Chinello Cybernético

    Simplesmente "Chinello"

  • Usuários
  • 1892 posts
  • Sexo:Masculino
  • Localidade:Canoas - RS

Posted 04/11/2003, 00:09

Realmente, não tinha analizado todo o código..

E tem uma linha do código q faz isso mesmo, ou seja, se a hora for mais q 12, ele subtrai 12, e a hora fica normal, e sem o zero.. hehe.. A linha é essa:

var timeValue = ((hours > 12) ? hours - 12 : hours)

E para corrigir isso, eu tirei aquela primeira linha q tinha adicionado, e fiz as mudanças todas nessa própria linha, veja como ficou o código completo:

<html>
<head>
<title>ScriptBrasil</title>
<script language="javascript">
var timerID = null

var timerRunning = false
function stopclock(){
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}
function startclock(){
    stopclock()
    showtime()
}
function showtime(){

    var now = new Date()

    var hours = now.getHours()

    var minutes = now.getMinutes()

    var seconds = now.getSeconds()

    var month = now.getMonth()

    var datum = now.getDate()

    var year = now.getYear()

    var timeValue = ((hours > 12) ? "0"+hours - 12 : "0"+hours)

    timeValue += ((minutes < 10) ? "0" : "") + minutes

    timeValue += (hours >= 12) ? " pm" : "am"

    document.form1.hora.value = timeValue

    timerID = setTimeout("showtime()",1000)

    timerRunning = true
}
</script>
</head>

<BODY onLoad="startclock()">
<FORM NAME="form1">
<INPUT TYPE="text" NAME="hora" SIZE=8 VALUE ="">
</FORM>
</BODY>

</html>

Agora sim.. :D

Fui.. :P
Diego Sampaio - kroW - PHP Framework > http://chinelloweb.net/
chinello at gmail dot com

System > Athlon 64 3200+ Mobile | Kubuntu 7.04 Fesity Fawn AMD64 | Kernel 2.6.20-16
Coding @ KDE 3.5.7 | PHP 5.2.1 | Apache 2.2.3 | MySQL 5.0.38

#6 Sir Curse

Sir Curse

    Normal

  • Usuários
  • 113 posts
  • Sexo:Não informado
  • Localidade:São Paulo
  • Interesses:Linguagens em geral

Posted 04/11/2003, 00:15

funciono naum....

#7 Chinello Cybernético

Chinello Cybernético

    Simplesmente "Chinello"

  • Usuários
  • 1892 posts
  • Sexo:Masculino
  • Localidade:Canoas - RS

Posted 04/11/2003, 00:18

Eitaaa.. O q um parênteses não faz??? :lol: :lol:

Vai lá, agora até testei, inclusive mudei o horário do relógio aqui do PC só pra testar.. :P

<html>
<head>
<title>ScriptBrasil</title>
<script language="javascript">
var timerID = null

var timerRunning = false
function stopclock(){
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}
function startclock(){
    stopclock()
    showtime()
}
function showtime(){

    var now = new Date()

    var hours = now.getHours()

    var minutes = now.getMinutes()

    var seconds = now.getSeconds()

    var month = now.getMonth()

    var datum = now.getDate()

    var year = now.getYear()

    var timeValue = ((hours > 12) ? "0"+(hours - 12) : "0"+hours)

    timeValue += ((minutes < 10) ? "0" : "") + minutes

    timeValue += (hours >= 12) ? " pm" : "am"

    document.form1.hora.value = timeValue

    timerID = setTimeout("showtime()",1000)

    timerRunning = true
}
</script>
</head>

<BODY onLoad="startclock()">
<FORM NAME="form1">
<INPUT TYPE="text" NAME="hora" SIZE=8 VALUE ="">
</FORM>
</BODY>

</html>

Em negrito, os malditos parênteses.. hehe

Fui.. :P
Diego Sampaio - kroW - PHP Framework > http://chinelloweb.net/
chinello at gmail dot com

System > Athlon 64 3200+ Mobile | Kubuntu 7.04 Fesity Fawn AMD64 | Kernel 2.6.20-16
Coding @ KDE 3.5.7 | PHP 5.2.1 | Apache 2.2.3 | MySQL 5.0.38

#8 Sir Curse

Sir Curse

    Normal

  • Usuários
  • 113 posts
  • Sexo:Não informado
  • Localidade:São Paulo
  • Interesses:Linguagens em geral

Posted 04/11/2003, 00:22

huhu! issu ta ficando engraçadu, até pq tava vendu seu blog e vi como o mundo é pequeno.... hehe... tipo, quando é 12 horas ele ta adicionando o zero tambem... ta quase la! :P

#9 Chinello Cybernético

Chinello Cybernético

    Simplesmente "Chinello"

  • Usuários
  • 1892 posts
  • Sexo:Masculino
  • Localidade:Canoas - RS

Posted 04/11/2003, 00:34

Como assim o mundo é pequeno??? :blink:

hehe.. Tá engraçado mesmo.. Tipo, eu queria preservar ao máximo o código, aí continuei usando o ternário (esse comando q tem o ? no meio).. Só q ele tem um problema, ele só tem o caso verdadeiro e o caso falso.. E como o teste tava sendo feito assim: horas > 12, quando isso era veradeiro, a hora ía de 13 até 23, e quando era false, a hora ía de 12 até 1.. Ou seja, quando falso, ele acrescenta o zero na frente, e como 12 horas tb tá no caso falso, o zero fica na frente do 12 tb.. hehe

Mas agora com o IF acabou com essa choradeira toda.. hehe

<html>
<head>
<title>ScriptBrasil</title>
<script language="javascript">
var timerID = null

var timerRunning = false
function stopclock(){
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}
function startclock(){
    stopclock()
    showtime()
}
function showtime(){

    var now = new Date()

    var hours = now.getHours()

    var minutes = now.getMinutes()

    var seconds = now.getSeconds()

    var month = now.getMonth()

    var datum = now.getDate()

    var year = now.getYear()
   
    if(hours>12)
        var timeValue = "0"+(hours-12);
    else if(hours<12)
        var timeValue = "0"+hours;
    else
        var timeValue = hours;

    timeValue += ((minutes < 10) ? "0" : "") + minutes

    timeValue += (hours >= 12) ? " pm" : "am"

    document.form1.hora.value = timeValue

    timerID = setTimeout("showtime()",1000)

    timerRunning = true
}
</script>
</head>

<BODY onLoad="startclock()">
<FORM NAME="form1">
<INPUT TYPE="text" NAME="hora" SIZE=8 VALUE ="">
</FORM>
</BODY>

</html>

Pronto caramba.. :lol:

Hey, e agora me manda uma PM contando essa história ae de mundo pequeno.. hehe ;)

Fui.. :P
Diego Sampaio - kroW - PHP Framework > http://chinelloweb.net/
chinello at gmail dot com

System > Athlon 64 3200+ Mobile | Kubuntu 7.04 Fesity Fawn AMD64 | Kernel 2.6.20-16
Coding @ KDE 3.5.7 | PHP 5.2.1 | Apache 2.2.3 | MySQL 5.0.38

#10 Sir Curse

Sir Curse

    Normal

  • Usuários
  • 113 posts
  • Sexo:Não informado
  • Localidade:São Paulo
  • Interesses:Linguagens em geral

Posted 04/11/2003, 00:47

rapá! eu tentei colocando mais IFs mas num consegui... agora só falta tirar o zero da frente do 10 e do 11 horas.... haha :P

#11 Chinello Cybernético

Chinello Cybernético

    Simplesmente "Chinello"

  • Usuários
  • 1892 posts
  • Sexo:Masculino
  • Localidade:Canoas - RS

Posted 04/11/2003, 01:09

Tá véio, agora chega.. hehe.. Testei em todas as horas.. Tá tudo ok.. (y)

<html>
<head>
<title>ScriptBrasil</title>
<script language="javascript">
var timerID = null

var timerRunning = false
function stopclock(){
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}
function startclock(){
    stopclock()
    showtime()
}
function showtime(){

    var now = new Date()

    var hours = now.getHours()

    var minutes = now.getMinutes()

    var seconds = now.getSeconds()

    var month = now.getMonth()

    var datum = now.getDate()

    var year = now.getYear()
   
    if(hours>12 && hours<22)
        var timeValue = "0"+(hours-12);
    else if(hours>=22)
        var timeValue = hours-12;
    else if(hours<12 && hours<10)
        var timeValue = "0"+hours;
    else
        var timeValue = hours;

    timeValue += ((minutes < 10) ? "0" : "") + minutes

    timeValue += (hours >= 12) ? " pm" : "am"

    document.form1.hora.value = timeValue

    timerID = setTimeout("showtime()",1000)

    timerRunning = true
}
</script>
</head>

<BODY onLoad="startclock()">
<FORM NAME="form1">
<INPUT TYPE="text" NAME="hora" SIZE=8 VALUE ="">
</FORM>
</BODY>

</html>

Fui.. :P
Diego Sampaio - kroW - PHP Framework > http://chinelloweb.net/
chinello at gmail dot com

System > Athlon 64 3200+ Mobile | Kubuntu 7.04 Fesity Fawn AMD64 | Kernel 2.6.20-16
Coding @ KDE 3.5.7 | PHP 5.2.1 | Apache 2.2.3 | MySQL 5.0.38

#12 Sir Curse

Sir Curse

    Normal

  • Usuários
  • 113 posts
  • Sexo:Não informado
  • Localidade:São Paulo
  • Interesses:Linguagens em geral

Posted 04/11/2003, 01:17

haha! agora foi! vlw de novo! :P




2 user(s) are reading this topic

0 membro(s), 2 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq