Wednesday 5 June 2013

Change Date Formate in C#

Convert.ToDateTime(DateTime.ParseExact(''12-03-2012", "dd-MM-yyyy", null).ToString("yyyy-MM-dd"))

protected static DateTime changedateformate(string Date)
    {
        string[] fromdate;
        DateTime cndate = DateTime.Now;
        if (Date != "")
        {
            if (Date.Contains('/'))
            {
                fromdate = Date.Split('/');
                Date = fromdate[2] + "/" + fromdate[1] + "/" + fromdate[0];
                cndate = Convert.ToDateTime(Date).Date;
            }
            else
            {
                fromdate = Date.Split('-');
                Date = fromdate[2] + "-" + fromdate[1] + "-" + fromdate[0];
                cndate = Convert.ToDateTime(Date).Date;
            }
        }
        return cndate;
    }

No comments:

Post a Comment