Wednesday, March 27, 2013

Save DateTime in dd/MM/yyyy format

  //this.Text = "22/11/2009";
        string CreatedDate = DateTime.Now.ToString("dd/MM/yyyy");
        string LastUpdatedDate = DateTime.Now.ToString("dd/MM/yyyy");
        string CreatedTime = DateTime.Now.ToString("hh:mm:ss");  //txtStartTime.Text
        string FinalDateTime = CreatedDate + " " + CreatedTime;
        DateTime CreatedDates = DateTime.ParseExact(FinalDateTime, "dd/MM/yyyy hh:mm:ss", null);
        DateTime LastUpdatedDates = DateTime.ParseExact(CreatedDate, "dd/MM/yyyy", null);
        objUserDetail.CreateDate = CreatedDates;
        objUserDetail.LastUpdatedDate = LastUpdatedDates;
        Repository.UserProvider.Save(objUserDetail);
Use the following code for dd/MM/yyyy:
---------------------------------------
string format = "dd/MM/yyyy"; 
DateTime dt = DateTime.ParseExact(dateString, format, provider);
Use the following code for MM/dd/yyyy:
-------------------------------------
string format = "MM/dd/yyyy"; 
DateTime dt = DateTime.ParseExact(dateString, format, provider);

0 comments:

Post a Comment