在asp.net中转换date格式c#

我正在上传一个excel文件,其中有一个格式为“dd-mm-yyyy”的date列。 我正试图将其转换为这种格式的mm / dd / yyyy。

string[] dsplit = row[6].ToString().Split('-'); obj.ExpiryDate = string.Format("{0}/{1}/{2}", dsplit[1], dsplit[0], dsplit[2]).ToDate(); 

但它会抛出错误,一些隐藏的错误。

这是整个代码。 我已经尝试了很多,没有预期的作品

 protected void SaveEmployeefrom_Click(object sender, EventArgs e) { uploadExcelfile(); } public List<ClsEmployee> uploadExcelfile() { List<ClsEmployee> list = new List<ClsEmployee>(); DataTable tb = new DataTable(); try { if (employeeregistration.HasFile) { string name = DateTime.Now.ToString("hhmmss_ddmmyy"); name = name + employeeregistration.FileName; employeeregistration.SaveAs(Server.MapPath("~/ExcelFiles/") + name); string path = System.IO.Path.GetFullPath(Server.MapPath("~/ExcelFiles/") + name); string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; if (Path.GetExtension(path) == ".xls") { excelconnection = new OleDbConnection(connString); excelconnection.Open(); } else if (Path.GetExtension(path) == ".xlsx") { excelconnection = new OleDbConnection(connString); excelconnection.Open(); } OleDbCommand cmd = new OleDbCommand("SELECT [Name],[ID],[Mobile No],[Phone],[Emirates],[Nationality],[ExpiryDate],[Address] FROM [sheet1$]", excelconnection); OleDbDataAdapter oleda = new OleDbDataAdapter(cmd); oleda.Fill(tb); foreach (DataRow row in tb.Rows) { if (!string.IsNullOrEmpty(row[6].ToString())) { ClsEmployee obj = new ClsEmployee(); obj.ID = 0; // obj.Employer_ID=row[0].ToInt32(); obj.EmployeeName = row[0].ToString(); obj.EmployeeUniqueID = row[1].ToString(); obj.MobileNumber = row[2].ToString(); obj.PhoneNumber = row[3].ToString(); obj.Emirates = row[4].ToString(); obj.Nationality = row[5].ToString(); //from excel its dd-mm-yyyy string[] dsplit = row[6].ToString().Split('-'); obj.ExpiryDate = string.Format("{0}/{1}/{2}", dsplit[1], dsplit[0], dsplit[2]).ToDate(); // obj.ExpiryDate = row[6].ToDate(); //mm-dd-yyyy obj.Address = row[7].ToString(); list.Add(obj); } } excelconnection.Dispose(); if (File.Exists(path)) { File.Delete(path); } int total = tb.Rows.Count; if (total>0) { GV_Employee.DataSource = null; GV_Employee.DataSource = list; GV_Employee.DataBind(); GV_Employee.Visible = true; ResultLabel.ResultLabelAttributes("Uploaded successfull !!!", ProjectUserControls.Enums.ResultLabel_Color.Yellow); ResultPanel.Controls.Add(ResultLabel); } else { ResultLabel.ResultLabelAttributes("No Record In Excel Sheet !!!", ProjectUserControls.Enums.ResultLabel_Color.Red); ResultPanel.Controls.Add(ResultLabel); } //txtSerialQuantity.Text = total.ToString(); ////// trbtnCheckAll.Visible = true; ////div_automatic.Visible = true; ////lbl_totalSelected.Text = "Total Selected = " + total.ToString(); } } catch (Exception x) { x.ToString(); //ResultLabel.ResultLabelAttributes(x.Message, ProjectUserControls.Enums.ResultLabel_Color.Red); ResultLabel.ResultLabelAttributes(x.Message, ProjectUserControls.Enums.ResultLabel_Color.Red); ResultPanel.Controls.Add(ResultLabel); } return list; } protected void Button1_Click(object sender, EventArgs e) { ClsEmployee obj1 = new ClsEmployee(); List<ClsEmployee> list = new List<ClsEmployee>(); try { foreach (GridViewRow item in GV_Employee.Rows) { ClsEmployee obj = new ClsEmployee(); obj.ID = 0; obj.Employer_ID = cmbEmployer.SelectedValue.ToInt32();// ((Literal)GV_Employee.Rows[1].FindControl("Ltrl_EmployerID")).Text.ToInt32(); obj.EmployeeName = ((Literal)item.FindControl("Ltrl_Name")).Text.ToString(); obj.EmployeeUniqueID = ((Literal)item.FindControl("Ltrl_EmployeeUniqueID")).Text.ToString(); obj.MobileNumber = ((Literal)item.FindControl("Ltrl_Mobile")).Text.ToString(); obj.PhoneNumber = ((Literal)item.FindControl("Ltrl_PhoneNo")).Text.ToString(); obj.Emirates_ID = ((Literal)GV_Employee.Rows[1].FindControl("Ltrl_Emirates")).Text.ToInt32(); obj.Nationality_ID = ((Literal)GV_Employee.Rows[1].FindControl("Ltrl_Nationality")).Text.ToInt32(); obj.ExpiryDate = ((Literal)item.FindControl("Ltrl_Expiry")).Text.ToDate(); obj.Address = ((Literal)item.FindControl("Ltrl_Address")).Text.ToString(); obj.LFMD = ""; obj.RFMD = ""; obj.PinCode = ""; obj.IsFingerAuth = false; obj.IsActive = true; list.Add(obj); } obj1.SaveEmployeefromExcelFile(list); ResultLabel.ResultLabelAttributes("Save successfull !!!", ProjectUserControls.Enums.ResultLabel_Color.Yellow); ResultPanel.Controls.Add(ResultLabel); GV_Employee.DataSource = null; GV_Employee.Visible = false; } catch (Exception ex) { ResultLabel.ResultLabelAttributes(ex.ToString(), ProjectUserControls.Enums.ResultLabel_Color.Red); ResultPanel.Controls.Add(ResultLabel); } } 

试试这个代码。

 DateTime dt = DateTime.Parse(row[6].toString()); string date = dt.ToString("dd/MM/yyyy"); 

我认为row[6]是从Excel中获得的DateTime时间,你只需要;

 row[6].ToString("MM/dd/yyyy", CultureInfo.InvariantCulture); 

DateTime时间转换为string并将其分解为通常不好的想法。 mm说明符是分钟, MM是几个月的方式。

尝试这个..

  String MyString = "12-30-2014"; // get value from text field DateTime MyDateTime = new DateTime(); MyDateTime = DateTime.ParseExact(MyString, "MM-dd-yyyy",null); String MyString_new = MyDateTime.ToString("dd-MM-yyyy");