Friday, February 18, 2011

Julian Date

I was a little busy over the past week with lots of traveling, It's been a productive week for me personally and my next post would be on my experiences with LINQ to XML definitely. I got fascinated with LINQ for a long time finally my project requirements have necessitated the need to use LINQ but current post is about a nifty little feature which i made use of yesterday and i figured someone would make use of this feature in case if they were searching for it.

I am a lazy guy, laziness has always been my trademark niche and i tend to spend more time exploring the framework functionality to get the job done real quick and it's not that i am against development of custom methodologies, I am an ardent lover of custom applications. I personally like accomplishing things this way rather. :)

The requirement i faced was a minor part of my module which needed the Current time object to be Converted to the Julian date format [YYDDD] for instance today February 18 2011 can be represented as 11049 in string notation. 

11- Last 2 digits of year 2011
049 - Represents the Current Number of this day in the calendar year from 0-365 (31 days in January + 18 days currently)

It's a straightforward simple routine and It's in C#

public string RetJulianDate(DateTime dt)
        {
            DateTime dObj = dt;
            String sYear = dObj.Year.ToString();
            int month = dObj.Month;
            int day = dObj.Day;
            //Current Month difference
            int current = day;
            
            int sum = 0;
            int final = 0;
            if (month > 1)
            {
                for (int i = 1; i < month; i++)
                {
                    sum += DateTime.DaysInMonth(dObj.Year, i);
                }

                final = sum + current;

            }
            else
                final = current;
            return (sYear.Substring(1, 2) + final.ToString("000"));
        } 
 
 
 
Until Next time! 

No comments:

Post a Comment