1. Computing & Technology

Discuss in my forum

Calculating the Zodiac Sign from the Birth Date

By , About.com Guide

In astrology *myths*, zodiac is a belt of twelve constellations (sun, moon, and major planets) through which the Sun's path in the sky, the ecliptic, passes. The word zodiac comes from the Greek 'zodiakos,' literally meaning 'circle of animals.'

Calculating the Zodiac Sign from the Birth Date

The ZodiacSign function will return the name of the zodiac sign for a given date value.
 //Returns the zodiac sign from a birth date
 function ZodiacSign(const dateOfBirth: TDate): string;
 type
   TZodiacSign = record
     Name : string;
     Day : integer;
   end;
 
 const
   ZodiacSigns : array[1..12] of TZodiacSign =
   (
     (Name : 'Aquarius'; Day : 50), // "Water-bear" ends 02/19
     (Name : 'Pisces'; Day : 79), // "Fish" ends 03/20
     (Name : 'Aries'; Day : 110), // "Ram" ends 04/20
     (Name : 'Taurus'; Day : 131), // "Bull, ends 05/21
     (Name : 'Gemini'; Day : 172), // "Twins" ends 06/21
     (Name : 'Cancer'; Day : 203), // "Crab" ends 07/22
     (Name : 'Leo'; Day : 233), // "Lion" ends 08/21
     (Name : 'Virgo'; Day : 266), // "Virgin" ends 09/23
     (Name : 'Libra'; Day : 296), // "Scale", ends 10/23
     (Name : 'Scorpio'; Day : 326), // "Scorpion" ends 11/22
     (Name : 'Sagittarius'; Day : 357), // "Archer" ends 12/22
     (Name : 'Capricorn'; Day : 385)  // "Sea-goat" ends 01/20
   ) ;
 var
   d : word;
   idx : integer;
 begin
   d := DayOfTheYear(dateOfBirth) ;
 
   if (d > 59) AND IsLeapYear(YearOf(dateOfBirth)) then Inc(d) ;
   if (d < 21) then d := d + DaysInYear(dateOfBirth) ;
 
   for idx := 1 to 12 do
   begin
     if d <= ZodiacSigns[idx].Day then
     begin
       result := ZodiacSigns[idx].Name;
       exit;
     end;
   end;
 end; (*ZodiacSign*)
 
Note: The key functions used here are: IsLeapYear, DaysInYear and DayOfTheYear. When you figure out what is their job in the "ZodiacSign" function, do let me know ;)

A hint ... for example: for "Leo", Day 233 is the day of the year (a non leap year) for August 21 - ending day for the Leo sign.

Usage:

 var
   dob : TDateTime;
 begin
   dob := EncodeDate(1973, 1, 29) ;
 
   //will return aquarius
   ShowMessage(ZodiacSign(dob)) ;
 end;
 
Note:

1. I have tested the code 99% :) Fell free to send any fixes you might find.

2. If you have some shorter code to get the zodiac sign out of a TDate value ... I would like to see it.

Delphi tips navigator:
» Hide a Delphi 2007 Application Button from the TaskBar (with MainFormOnTaskBar)
« TSpeedButton Left Align Caption - Left Justify Delphi's TSpeedButton Caption Text

©2012 About.com. All rights reserved.

A part of The New York Times Company.