PDA

Ver la Versión Completa : Realizar un bucle for con fechas!!


Vales08
21-10-2011, 20:58:33
Hola a todos...
Hoy mi duda es concisa..

Necesito saber si se puede realizar un for con fechas o datos de tipo tDateTime..
Lo que yo quiero realizar es un for que va desde la fecha actual (Now) hasta el ultimo dia del mes (UltimoDia), donde la fecha actual se va incrementando hasta llegar a Ultimodia.


var
Proximafecha, UltimoDia, DiaActual:TdateTime;
hoy:integer;
dia, mes, anio: Word;

...
hoy:=DayOfTheWeek(Date); // guardo el dia de la semana en el que se ubica la fecha actual
DecodeDate(Now, anio, mes, dia);
UltimoDia:=RecodeDate(Date, anio, mes, DaysInMonth(Now));
// lo que realice aqui es guardar en la variable 'UltimoDia' el ultimo dia del mes

for Proximafecha:=Now to UltimoDia do (**)
begin
if hoy=1 then // Lunes
begin
DM.DSET_agen_turFECHA.Value:=Now; //Guardo la fecha en la tabla AGENDA
end;
if hoy=2 then // Martes
begin
ProximaFecha:=IncDay(Date,6);
DM.DSET_agen_turFECHA.Value:=Proximafecha;
end;
if hoy=3 then // Miercoles
begin
ProximaFecha:=IncDay(Date,5);
DM.DSET_agen_turFECHA.Value:=ProximaFecha;
end;
if hoy=4 then // Jueves
begin
ProximaFecha:=IncDay(Date,4);
DM.DSET_agen_turFECHA.Value:=Proximafecha;
end;



Ami me salta el siguiente error al realizar el codigo en la linea (**):
[Error] Form_registro_dia_hora.pas(1016): For loop control variable must have ordinal type

A mi entender dice que la variable para este tipo de bucle debe ser ordinal.

Trabajo con delphi 7, firebird y sql manager..

Espero su ayuda, Desde ya muchas gracias..

ecfisa
21-10-2011, 21:29:12
Hola Vales08.

Si no te entiendo mál, sería:

uses DateUtils;
...
var
i: Integer;
begin
for i:= DayOf(Now) to DaysInMonth(Now) do
begin
...


Saludos.

Vales08
21-10-2011, 21:29:50
Me disculpo por el error, aca les pongo bien el codigo, no me habia dado cuenta de que se me borro la ultima parte:


var
Proximafecha, UltimoDia, DiaActual:TdateTime;
hoy:integer;
dia, mes, anio: Word;

...
hoy:=DayOfTheWeek(Date); // guardo el dia de la semana en el que se ubica la fecha actual
DecodeDate(Now, anio, mes, dia);
UltimoDia:=RecodeDate(Date, anio, mes, DaysInMonth(Now));
// lo que realice aqui es guardar en la variable 'UltimoDia' el ultimo dia del mes

for Proximafecha:=Now to UltimoDia do (**)
begin
if hoy=1 then // Lunes
begin
DM.DSET_agen_turFECHA.Value:=Now; //Guardo la fecha en la tabla AGENDA
end;
if hoy=2 then // Martes
begin
ProximaFecha:=IncDay(Date,6);
DM.DSET_agen_turFECHA.Value:=Proximafecha;
end;
if hoy=3 then // Miercoles
begin
ProximaFecha:=IncDay(Date,5);
DM.DSET_agen_turFECHA.Value:=ProximaFecha;
end;
if hoy=4 then // Jueves
begin
ProximaFecha:=IncDay(Date,4);
DM.DSET_agen_turFECHA.Value:=Proximafecha;
end;
if hoy=5 then // Viernes
begin
ProximaFecha:=IncDay(Date,3);
DM.DSET_agen_turFECHA.Value:=Proximafecha;
end;
if hoy=6 then // Sabado
begin
ProximaFecha:=IncDay(Date,2);
DM.DSET_agen_turFECHA.Value:=Proximafecha;
end;
if hoy=7 then // Domingo
begin
ProximaFecha:=IncDay(Date,1);
DM.DSET_agen_turFECHA.Value:=Proximafecha;
end;
end;


Gracias.

Vales08
21-10-2011, 21:32:23
ecfisa gracias por la respuesta..
Lo que realizas en ese codigo es recorrer desde el dia de hoy(actual) hasta el fin del mes?
Porque si asi es, seria lo que ando buscando..
Ya mismo lo pruebo

ecfisa
21-10-2011, 21:34:10
Lo que realizas en ese codigo es recorrer desde el dia de hoy(actual) hasta el fin del mes?

Si Vales, eso creí entender que buscabas.

Un saludo.

Vales08
21-10-2011, 21:37:22
Muchas gracias, no me salta mas el error, ahora tengo que ver si funciona realmente como quiero.. Primero tengo que solucionar un probemita con los estados de edicion de las tablas pero eso es tema aparte.. Apenas me funcione, informo..
Muchas gracias..

Vales08
21-10-2011, 22:13:53
Tengo una duda mas, como realizo el contador para que se incrementen los dias del for??? por ejemplo de a 7 dias o una semana??

ecfisa
21-10-2011, 22:20:53
Tengo una duda mas, como realizo el contador para que se incrementen los dias del for??? por ejemplo de a 7 dias o una semana??
Hola Vales.

La declaración for en Delphi no permite definir el incremento, vas a tener que usar una variable adicional como contador para ese fín. Pero no entiendo bién cuál es la idea que tenés como para sugerirte algo.

Un saludo.

Vales08
21-10-2011, 22:37:25
Claro, lo que sucede es que yo necesito guardar fechas que correspondan a los dias lunes, en este caso trabajo con este mes, octubre.
Entonces lo que realizo primero es saber en que dia de la semana estoy ubicada hoy.. Para eso trabajo con la funcion DayOfTheWeek.

hoy:=DayOfTheWeek(Date) //Toma valores 1,2,3,4,5,6,7

Hecho esto, yo pregunto lo siguiente:

if hoy=1 then // Lunes
begin
DM.DSET_agen_turFECHA.Value:=Now; //Guardo la fecha en la tabla AGENDA
end;
if hoy=2 then // Martes
begin
ProximaFecha:=IncDay(Date,6);
DM.DSET_agen_turFECHA.Value:=Proximafecha;
end;
if hoy=3 then // Miercoles
begin
ProximaFecha:=IncDay(Date,5);
DM.DSET_agen_turFECHA.Value:=ProximaFecha;
end;
if hoy=4 then // Jueves
begin
ProximaFecha:=IncDay(Date,4);
DM.DSET_agen_turFECHA.Value:=Proximafecha;
end;
if hoy=5 then // Viernes (aca estamos ubicados hoy 21/10/2011)
begin
ProximaFecha:=IncDay(Date,3); //aca a la fecha de hoy se le suman 3 dias que son los que faltan para llegar al proximo lunes (24/10/2011)
DM.DSET_agen_turFECHA.Value:=Proximafecha; // se guarda 24/10/2011
end;
if hoy=6 then // Sabado
begin
ProximaFecha:=IncDay(Date,2);
DM.DSET_agen_turFECHA.Value:=Proximafecha;
end;
if hoy=7 then // Domingo
begin
ProximaFecha:=IncDay(Date,1);
DM.DSET_agen_turFECHA.Value:=Proximafecha;
end;

Hasta aca me funciona barbaro, el problema es que yo no quiero que me guarde solo el lunes proximo, sino todos los del mes y eso lo quiero realizar con el FOR.. que recorre desde el dia de hoy hasta fin de mes 31/10/2011 (ultimo lunes). Para ello necesito algo que incremente la semana sino nunca voy a llegar a fin de mes..

No se si me logras entender?.

Vales08
21-10-2011, 22:40:58
En conclusion necesito que se incremente la variable i, que en primer instancia tiene el valor 24, el dia de hoy, y yo quiero que pase a tener el valor 31.. el ultimo lunes del mes

ecfisa
21-10-2011, 23:21:19
Hola Vales.

A ver si es algo como esto... :rolleyes:

...
var
i: Integer;
T: TDate;
begin
T:= Now;
for i:= DayOf(Now) to DaysInMonth(Now) do
begin
case DayOfTheWeek(T) of
1: ListBox1.Items.Add('Lunes '+IntToSTr(Dayof(T)));
2: ListBox1.Items.Add('Martes '+IntToSTr(Dayof(T)));
3: ListBox1.Items.Add('Miercoles '+IntToSTr(Dayof(T)));
4: ListBox1.Items.Add('Jueves '+IntToSTr(Dayof(T)));
end;
T:= IncDay(T);
end;
...


Un saludo.

Vales08
21-10-2011, 23:48:29
siiiii, muchisimas gracias... ya lo pruebo..
Claro tengo que utilizar otra variable porque i es entero y yo utilizando el incDay necesito variables date, sino son incompatibles..

Saludos.-