Thursday 27 March 2014

[Solved ] DateTime Conversion Problem in Dot Net C Sharp

Today, i am providing a custom Date Time Class that can be used to solve the issues related to Date Time Conversion problem in Dot Net. The code provided here is in C#. You can directly use this class in your project and call the related methods which is required.

Why Date Time Conversion problem occurs?


Different PCs have different Date and Time format based on choice and locale. In sql server, [datetime] type datatype always store Date and Time in yyyy-MM-dd HH:MM:SS.000 format. So if we try to insert Date other then this standard format, then sql server shows the error which says ,"Conversion from nvarchar/varchar data type to datetime data type failed." . So if you already use this standard format then you don't require Date Conversion class. Some we don't know what will be format of inpu date and time. In this case, use of following class code is very helpful:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;

/// <summary>
/// Summary description for DateTimeClass
/// </summary>
public class DateTimeClass
{
    //METHOD 1
    // use only when you want to convert any input time format to system time format
    // Both method parse only date seperator like [ - , / ] etc
    // IT NOT CONVERT MM/dd/yyyy to dd-MM-yyyy . it only convert dd/MM/yyyy to dd-MM-yyyy 
   
    DateTime dateTime;
   
    DateTime dtime;
    //get current date format
    string dateformat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
    //CALL WHEN YOU WANT TO CONVET INPUT DATE STRING INTO SYSTEM DATE STRING
    public string DTC(string input)
    {
     
         

        DateTime.TryParse(input , out dateTime);

        return dateTime.ToShortDateString();
       
    }

    public DateTime DTCC(string input)
    {



        DateTime.TryParse(input, out dateTime);

        return dateTime;

    }
    // CALL WHEN U WANT SYSTEM DATE TIME FORMAT
    public string DTF()
    {
        string input = DateTime.Now.ToString();

        DateTime.TryParse(input, out dateTime);


        return dateTime.ToShortDateString();

    }

    public string DTPE(string input)
    {
        string dtformat = "dd/MM/yyyy";

        CultureInfo cprovider = CultureInfo.InvariantCulture;

        return DateTime.ParseExact(input, dtformat, cprovider).ToString("dd/MM/yyyy");
     
     
    }


    //METHOD 2
    // use this function when you want to convert fixed input time format to system time format
    // this method parse date seperator like [ - , / ] etc
    // IT CAN CONVERT MM/dd/yyyy to SYSTEM DATE WHICH MAY BE IN dd-MM-yyyy TYPE FORMAT.
    // DATE TIME CONVERT FIXED INPUT DATE FORMAT
    public string DTCFF(string input)
    {

        dateTime = DateTime.ParseExact(input, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);

        return dateTime.ToShortDateString();

    }

    // DATE TIME CONVERT FIXED INPUT DATE FORMAT
    public string DTCF()
    {
        string input = DateTime.Now.ToShortDateString();
        dateTime = DateTime.ParseExact(input, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);

        return dateTime.ToShortDateString();

    }


    //DATE TIME  FIXED INPUT FORMAT 

    public string DTFF(DateTime sql)
    {

        sql = DateTime.ParseExact(sql.ToString(), "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);

        return sql.ToShortDateString();
    }


    //BREAK SYSTEM DATE INTO DESIRED FORMAT
    public string DTN()
    {
        DateTime dt = DateTime.Now;
        string d1, m1, y1;
        d1 = dt.Day.ToString();
        m1 = dt.Month.ToString();
        y1 = dt.Year.ToString();


        return d1 + "/" + m1 + "/" + y1;
    }

    public string DTNN(DateTime dt)
    {

        DateTime dt1 = new DateTime();
        dt1 = dt.AddYears(1);
        DateTime dt3 = new DateTime();
        dt3 = dt1.AddDays(-1);
        string d1, m1, y1;
        d1 = dt3.Day.ToString();
        m1 = dt3.Month.ToString();
        y1 = dt3.Year.ToString();
        return d1 + "/" + m1 + "/" + y1;

    }

}


No comments:

Post a Comment

Blogger