PDA

View Full Version : How can I do 20030220 minus 20030101? Date operations


LottaLava
03-20-2003, 02:51 PM
Hello folks do you known how can I do ( 20020220 - 20020101 )? But you must pay attention that both values are Date and itīs result must be 50 days, because this is 02/20/2003 and 01/01/2003.

I donīt known the correct formula to do this.

With best wishes,
LottaLava:D

Spudhead
03-20-2003, 04:48 PM
How about this:


<html>
<head>
<script language="JavaScript">
function getDaysDiff(){
var dt1=document.f1.iptD1.value;
var dt2=document.f1.iptD2.value;
var y1=""+dt1.charAt(0)+dt1.charAt(1)+dt1.charAt(2)+dt1.charAt(3);
var y2=""+dt2.charAt(0)+dt2.charAt(1)+dt2.charAt(2)+dt2.charAt(3);
var m1=""+dt1.charAt(4)+dt1.charAt(5)
var m2=""+dt2.charAt(4)+dt2.charAt(5)
var d1=""+dt1.charAt(6)+dt1.charAt(7)
var d2=""+dt2.charAt(6)+dt2.charAt(7)

var out1=new Date(y1, m1, d1);
var out2=new Date(y2, m2, d2);

var dateDiffDays = parseInt((out2 - out1) / (1000 * 60 * 60 * 24));
alert(dateDiffDays);
}
</script>
</head>

<body>
<form name="f1">
<input type="text" name="iptD1" maxlength=8 value="20030305">
<br>
<input type="text" name="iptD2" maxlength=8 value="20030315">
<br><br>
<input type="button" onClick="getDaysDiff()" value="ok">
</form>
</body>
</html>

LottaLava
03-20-2003, 09:38 PM
No, I thought something like this:

void Date::julian_to_mdy ()
{
long a,b,c,d,e,z,alpha;
z = julian+1;

// dealing with Gregorian calendar reform

if (z < 2299161L)
a = z;
else
{
alpha = (long) ((z-1867216.25) / 36524.25);
a = z + 1 + alpha - alpha/4;
}

b = ( a > 1721423 ? a + 1524 : a + 1158 );
c = (long) ((b - 122.1) / 365.25);
d = (long) (365.25 * c);
e = (long) ((b - d) / 30.6001);

day = (unsigned char)(b - d - (long)(30.6001 * e));
month = (unsigned char)((e < 13.5) ? e - 1 : e - 13);
year = (int)((month > 2.5 ) ? (c - 4716) : c - 4715);
julian_to_wday ();
}

void Date::mdy_to_julian (void)
{
int a,b=0;
int work_month=month, work_day=day, work_year=year;

// correct for negative year

if (work_year < 0)
work_year++;

if (work_month <= 2)
{
work_year--;
work_month +=12;
}

// deal with Gregorian calendar

if (work_year*10000. + work_month*100. + work_day >= 15821015.)
{
a = (int)(work_year/100.);
b = 2 - a + a/4;
}

julian = (long) (365.25*work_year) +
(long) (30.6001 * (work_month+1)) + work_day + 1720994L + b;
julian_to_wday ();
}

glenngv
03-21-2003, 06:59 AM
Take not that this is a Javascript forum not C++

LottaLava
03-21-2003, 01:35 PM
No indeed, but I want to create something that I can add a value to a date, for example:

var myDate = new MyDate( );

for ( var i = 0; i < 12; i++ )
myDate.addMonths( 30 );

My problem is that i donīt have a good Gregorian Calendar Algorithm.

Anyone known how I could implement that?

With best wishes,
LottaLava