Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Sign in to follow this  
N3lson

[Resolvido] Data Inválida

Recommended Posts

N3lson

Boas tenho este método para recalcular as datas,

Consiste ao user inserir o valor (intervalo de dias) depois pego nesse valor e a função calcula as datas, mas so me esta a calcular valore acima de 30 e algumas datas podem calhar inválidas como melhorar isto ?

public string DataUpdateForm(int freq, string valor)
    {
		    var dia = Convert.ToInt32(valor.Substring(8, 2));
		    var mes = Convert.ToInt32(valor.Substring(5, 2));
		    var ano = Convert.ToInt32(valor.Substring(0, 4));


		    var temp_freq_ano = (freq / 365);


		    var temp_freq_mes = ((freq - (temp_freq_ano * 365)) / 30);

		    ano = ano + temp_freq_ano;
		    mes = mes + temp_freq_mes;

		    if (mes > 12)
		    {
			    ano = ano + 1;
			    mes = mes - 12;
		    }

		    string resultado = ano + "-" + mes + "-" + dia;

		    return resultado;
    }


You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

Share this post


Link to post
Share on other sites
N3lson

Esqueçam já resolvi

var frequencia = Convert.ToInt32(this.txtFreq.Text);

			    DateTime dt = DateTime.Now;

			    dt = dt.AddDays(frequencia);


			    this.txtData_fim.Text = dt.ToString() ;

Depois se o user alterar a data de inicio

 private void txtData_inicio_ValueChanged(object sender, EventArgs e)
    {
	    if (this.txtFreq.Text.Length > 0)
	    {
		    //this.txtData_fim.Text = funcao.DataUpdateForm(Convert.ToInt32(this.txtFreq.Text), txtData_inicio.Text);

		    DateTime dt = Convert.ToDateTime(txtData_inicio.Text);

		    dt = dt.AddDays(Convert.ToInt32(this.txtFreq.Text));

		    this.txtData_fim.Text = dt.ToString();
	    }
    }

Obrigado


You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.