Click here to Skip to main content
6,290,721 members and growing! (16,966 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Gregorian Date To ISO Date Converter

By Siva Ram Mateti

An article on converting Gregorian calendar date to ISO 8601 calendar date
C#, Windows, .NET 1.0, Visual Studio, Dev
Posted:8 Aug 2002
Views:123,948
Bookmarked:25 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
9 votes for this article.
Popularity: 2.58 Rating: 2.71 out of 5
4 votes, 66.7%
1
1 vote, 16.7%
2
1 vote, 16.7%
3

4

5

Introduction

In .NET the DateTime class is implemented using Gregorian calendar. Sometimes, we need to extend the capabilities of this DateClass class. For example, if we wanted to know the ISO 8601 week number of the year, we need to create a new class for this implementation, as we can't extend the DateTime class. The following class DateEx written in C# is used to convert a date from Gregorian Calendar to ISO formatted date.

ISO-8601 defines the week as starting with Monday and ending with Sunday, i.e., Monday is weekday 1 and Sunday is weekday 7. The first week of a year is the week, which contains the first Thursday of the Gregorian year; and the week containing January 4th. Week numbers in ISO 8601 are integers from 1 to 52 or 53; parts of week 1 may be in the previous calendar year; parts of week 52 may be in the following calendar year; and if a year has a week 53, parts of it must be in the following calendar year. The correct way to represent a week in ISO 8601 format is YYYY-WW.

For example, in the year 2002, the ISO week 1 began on Monday December 31st, 2001; and the last ISO week (week 53) ended on Sunday January 2nd, 2003. Therefore, December 31st, 2001, is in the ISO week 2002-01; and January 2nd, 2003, is in the ISO week 2002-53.

Following is the code for DateEx. It contains four static functions; ISOWeekNumber, WeekDay, IsLeapYear, DisplayISODate.

// DateEx.cs 

using System;

public class DateEx {
    
    // Static Method to check Leap Year

    public static bool IsLeapYear(int yyyy) {
        
        if ((yyyy % 4 == 0 && yyyy % 100 != 0) || (yyyy % 400 == 0))
            return true;
        else
            return false;
    }    


    // Static Method to return ISO WeekNumber (1-53) for a given year

    public static int ISOWeekNumber(DateTime dt) {
        
        // Set Year

        int yyyy=dt.Year;

        // Set Month

        int mm=dt.Month;
        
        // Set Day

        int dd=dt.Day;

        // Declare other required variables

        int DayOfYearNumber;
        int Jan1WeekDay;
        int WeekNumber=0, WeekDay;

    
        int i,j,k,l,m,n;
        int[] Mnth = new int[12] {0,31,59,90,120,151,181,212,243,273,304,334};

        int YearNumber;

        
        // Set DayofYear Number for yyyy mm dd

        DayOfYearNumber = dd + Mnth[mm-1];

        // Increase of Dayof Year Number by 1, if year is leapyear and month is february

        if ((IsLeapYear(yyyy) == true) && (mm == 2))
            DayOfYearNumber += 1;

        // Find the Jan1WeekDay for year 

        i = (yyyy - 1) % 100;
        j = (yyyy - 1) - i;
        k = i + i/4;
        Jan1WeekDay = 1 + (((((j / 100) % 4) * 5) + k) % 7);

        // Calcuate the WeekDay for the given date

        l= DayOfYearNumber + (Jan1WeekDay - 1);
        WeekDay = 1 + ((l - 1) % 7);

        // Find if the date falls in YearNumber set WeekNumber to 52 or 53

        if ((DayOfYearNumber <= (8 - Jan1WeekDay)) && (Jan1WeekDay > 4))
          {
            YearNumber = yyyy - 1;
            if ((Jan1WeekDay == 5) || ((Jan1WeekDay == 6) && (Jan1WeekDay > 4)))
                WeekNumber = 53;
            else
                WeekNumber = 52;
          }
        else
            YearNumber = yyyy;
        

        // Set WeekNumber to 1 to 53 if date falls in YearNumber

        if (YearNumber == yyyy)
          {
            if (IsLeapYear(yyyy)==true)
                m = 366;
            else
                m = 365;
            if ((m - DayOfYearNumber) < (4-WeekDay))
            {
                YearNumber = yyyy + 1;
                WeekNumber = 1;
            }
          }
        
        if (YearNumber==yyyy) {
            n=DayOfYearNumber + (7 - WeekDay) + (Jan1WeekDay -1);
            WeekNumber = n / 7;
            if (Jan1WeekDay > 4)
                WeekNumber -= 1;
        }

        return (WeekNumber);
    }


    // Static Method to Calculate WeekDay (Monday=1...Sunday=7)

    public static int WeekDay(DateTime dt) {

        // Set Year

        int yyyy=dt.Year;

        // Set Month

        int mm=dt.Month;
        
        // Set Day

        int dd=dt.Day;

        // Declare other required variables

        int DayOfYearNumber;
        int Jan1WeekDay;
        int WeekDay;


        int i,j,k,l;
        int[] Mnth = new int[12] {0,31,59,90,120,151,181,212,243,273,304,334};


        // Set DayofYear Number for yyyy mm dd

        DayOfYearNumber = dd + Mnth[mm-1];

        // Increase of Dayof Year Number by 1, if year is leapyear and month is february

        if ((IsLeapYear(yyyy) == true) && (mm == 2))
            DayOfYearNumber += 1;

        // Find the Jan1WeekDay for year 

        i = (yyyy - 1) % 100;
        j = (yyyy - 1) - i;
        k = i + i/4;
        Jan1WeekDay = 1 + (((((j / 100) % 4) * 5) + k) % 7);

        // Calcuate the WeekDay for the given date

        l= DayOfYearNumber + (Jan1WeekDay - 1);
        WeekDay = 1 + ((l - 1) % 7);

        return WeekDay;
    }


    // Static Method to Display date in ISO Format (Year - WeekNumber - WeekDay)

    public static string DisplayISODate(DateTime dt) {
        string str;
        int year,weekday,weeknumber;
        
        year=dt.Year;
        weeknumber = ISOWeek(dt);
        weekday = WeekDay(dt);

        str = year.ToString("d0") + "-" + weeknumber.ToString("d0")
                                  + "-" + weekday.ToString("d0");
        return str;
    }

}

Now converting Gregorian Date to ISO Date is very easy.

using System;
using System.Globalization; 

public class DateImpl {
    
    public static void Main() {
        try {
            DateTime dt=new DateTime(2002,12,1);

            Console.WriteLine("Gregorian Date : {0}",
                  dt.ToString("d", DateTimeFormatInfo.InvariantInfo));

            Console.WriteLine("ISO Date : {0}",DateEx.DisplayISODate(dt));
        }
        catch(Exception e) {
            Console.WriteLine("Error \n\n {0}", e.ToString());
        }
    }
}

This class can be easily extended to handle needs for other calendars such as Julian, Indian Civil Calendar etc.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Siva Ram Mateti


Member

Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
GeneralThis feature is already implemented PinmemberFrancisko7:26 6 Mar '07  
GeneralWhy not use the .Net Framework? PinmemberMattias Olgerfelt6:41 27 Sep '04  
GeneralJan1WeekDay PinsussAnonymous11:39 25 Aug '04  
GeneralA better implementation PinmemberJulian Bucknall [MSFT]18:41 20 Aug '03  
GeneralErroneous weekdays PinsussAnonymous6:17 15 May '03  
GeneralAbove has bugs!!! Pinmemberjbkosman6:56 26 Mar '03  
Generaljust a thought PinsussAnonymous12:24 23 Jan '03  
GeneralYou can't extend DateTime but... PineditorJames T. Johnson18:21 9 Aug '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Aug 2002
Editor: Chris Maunder
Copyright 2002 by Siva Ram Mateti
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project