Logo 
Search:

C# Articles

Submit Article
Home » Articles » C# » DateTime FunctionsRSS Feeds

Convert Number to Date

Posted By: Vivek Patel     Category: C#     Views: 12599

This is a utility function which converts date in number format to date datatype in C#.net. This function can also be used to convert string format date to date datatype in C#.net

//Purpose : This method would convert number format date data into date.
//Parameters : Number format string would be passed. Eg: 12281990
//Return Value : Date. Eg: 12/28/1990Public 
private DateTime Number2Date(long lNum)
{
   string strNum = lNum.ToString();
   int iDay, iMon, iYear;
   
   DateTime ValidDate = DateTime.Now;
   
   try
   {
      //If date is not in valid format 
      if (strNum.Length == 7)
         strNum = "0" + strNum;
      
      iMon = Convert.ToInt32(strNum.Substring(0, 2));
      iDay = Convert.ToInt32(strNum.Substring(2, 2));
      iYear = Convert.ToInt32(strNum.Substring(4, 4));
      ValidDate = new DateTime(iYear, iMon, iDay);
   }

   catch(Exception Ex)
   {
      //Ex.Message
   }

   return ValidDate;
}
  
Share: 


Didn't find what you were looking for? Find more on Convert Number to Date Or get search suggestion and latest updates.

Vivek Patel
Vivek Patel author of Convert Number to Date is from United States. Vivek Patel says

I have started working in .Net Technology since its beta release and lucky to got chance to work on .Net 1.1, 2.0 and now working on .Net 3.5. I have worked in both C# and VB.Net for Asp.net Projects. I can also provide Sharepoint development needs. I can efficiently switch role from Tech Lead and Developer. I have comprehensive knowledge of Asp.net Development. I have been award Microsoft Most Valuable Award twice in Asp.net Technology.

Blog: http://dotnetguts.blogspot.com

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!