Jump to content

verificação de tempo


Recommended Posts

opa tudo bom.

queria fazer uma verificação até que simples porem to tendo alguns problemas , queria colocar pra se for 12 > ser pm e maior que 00 > ser am

pra verificar pm e am . o código que estou utilizando é esse v.

 

	local time = getRealTime()
	local hours = time.hour
	local minutes = time.minute
	if (hours < 10) then
		hours = "0"..hours
	end
	if (minutes < 10) then
		minutes = "0"..minutes
	end	
	if (hours > 12) then
		minutes = minutes.." pm"	
	end
	if (hours < 12) then
		minutes = minutes.." am"	
	end	

 

Link to comment
  local time = getRealTime()
  local hours = time.hour
  local minutes = time.minute
  if (hours < 10) then
     hours = "0"..hours
  end
  if (minutes < 10) then
     minutes = "0"..minutes
  end
  if hours >= 0 and hours <= 12 then --/> Se as horas forem entre 0 a 12 então fazer :
     minutes = minutes.." am" --/> AM
     else --/> Caso não ser entre 0 a 12 então fazer :
     minutes = minutes.." pm" --/> PM
  end

 

Edited by Angelo Pereira
  • Like 1
Link to comment
  • Other Languages Moderators

Pelo que entendi, vc quer converter o horário de 24h para ser 12h (am/pm)

function renderTime ()
	local time = getRealTime()
	local hours = time.hour
	local minutes = time.minute
	local seconds = time.second
	local ampm = " am"
	if (hours > 12) then
		hours = (time.hour - 12)
		ampm = " pm"
	elseif (hours == 0) then
		hours = 12 -- Meia-noite. Em vez de ser 0h. Fica 12h.
	end
	if (hours < 10) then
		hours = "0"..time.hour
	end
	if (minutes < 10) then
		minutes = "0"..time.minute
	end
	if (seconds < 10) then
		seconds = "0"..time.second
	end
-- Escolha o formato desejado:
	dxDrawText (2, hours..":"..minutes..":"..seconds..ampm, 0, 0) -- HH:MM:SS am
	-- dxDrawText (2, hours..":"..minutes..":"..seconds, 0, 0) -- HH:MM:SS
	-- dxDrawText (2, hours..":"..minutes..ampm, 0, 0) -- HH:MM am
	-- dxDrawText (2, hours..":"..minutes, 0, 0) -- HH:MM
end
addEventHandler ("onClientRender", root, renderTime)

 

Edited by Lord Henry
  • Thanks 1
Link to comment
10 hours ago, THG said:

Muito obrigado aos 2 , O do lord deu certinho porem do angelo deu mesmo erro que tava dando mais mesmo assim muito obrigado pela atenção ❤️

porem por algum motivo testei heim um outro horario ele esta duplicando mais um 0 https://imgur.com/a/oMqoxhM

Manda o código pra fica mais fácil a visualização do erro e a correção, mas explique uma coisa, você quer do jeito do lord, apenas adiciona am e pm ?

 

A alteração que eu fiz está funcionando, porem, fiz uma correção : 

function renderTime ()
   local time = getRealTime()
   local hours = time.hour
   local minutes = time.minute
   if (hours < 10) then
      hours = "0"..hours
   end
   if (minutes < 10) then
      minutes = "0"..minutes
   end
   if hours >= 0 and hours < 12 then --/> Se as horas forem entre 0 a 11:59 então fazer :
      minutes = minutes.." am" --/> AM
      else --/> Caso não ser entre 0 a 11:59 então fazer :
      minutes = minutes.." pm" --/> PM
   end
   dxDrawText (""..hours..":"..minutes.."",x*0,y*0,x*1366,y*768,tocolor(255, 255, 255, 255), x*1.2,"arial","center","center",false,false,false,false,false)
end
addEventHandler ("onClientRender", root, renderTime)

 

Edited by Angelo Pereira
  • Thanks 1
Link to comment

Também fiz uma correção no do lord com o tempo duplicado

function renderTime ()
   local time = getRealTime()
   local hours = time.hour
   local minutes = time.minute
   local ampm = ""
   if (hours >= 12 and hours < 24) then
      hours = (time.hour - 12)
      ampm = " pm"
   elseif (hours >= 0 and hours < 12) then
      hours = hours
      ampm = " am"
   end
   if hours == 12 or hours == 0 then
      hours = 12
   end
   if (minutes < 10) then
      minutes = "0"..time.minute
   end
   dxDrawText (""..hours..":"..minutes.." "..ampm.."",x*0,y*0,x*1366,y*768,tocolor(255, 255, 255, 255),x*1.2,"arial","center","center",false,false,false,false,false)
end
addEventHandler ("onClientRender", root, renderTime)

 

Edited by Angelo Pereira
  • Like 1
Link to comment
  • Other Languages Moderators

Corrigido. Era um bug relacionado a variável.

function renderTime ()
	local time = getRealTime()
	local hours = time.hour
	local minutes = time.minute
	local seconds = time.second
	local ampm = " am"
	if (hours > 12) then
		hours = (hours - 12)
		ampm = " pm"
	elseif (hours == 0) then
		hours = 12 -- Meia-noite.
	end
	if (hours < 10) then
		hours = "0"..hours
	end
	if (minutes < 10) then
		minutes = "0"..minutes
	end
	if (seconds < 10) then
		seconds = "0"..seconds
	end
-- Escolha o formato desejado:
	dxDrawText (2, hours..":"..minutes..":"..seconds..ampm, 0, 0) -- HH:MM:SS am
	-- dxDrawText (2, hours..":"..minutes..":"..seconds, 0, 0) -- HH:MM:SS
	-- dxDrawText (2, hours..":"..minutes..ampm, 0, 0) -- HH:MM am
	-- dxDrawText (2, hours..":"..minutes, 0, 0) -- HH:MM
end
addEventHandler ("onClientRender", root, renderTime)

 

@Angelo Pereira a condição if hours >= 0 é desnecessária. Pois ela sempre será verdadeira.

  • Like 1
  • Thanks 1
Link to comment
  • Moderators

Não precisa dessas condições ;p. Simplificando:

local sFormat = string.format
function renderTime ()
	local time = getRealTime()
	local minutes = time.minute
	local seconds = time.second
	local hours = (time.hour == 0) and 12 or time.hour % 12
	local ampm = (time.hour > 12) and " pm" or " am"
	
	local theTime = sFormat( "%02d:%02d:%02d", hours, minutes, seconds )
	dxDrawText ( theTime..ampm, left, top, right, bottom ) -- HH:MM:SS am
end
addEventHandler ("onClientRender", root, renderTime)

 

  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...