Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a problem with my project ,

I have to convert datetime from excel using eppplus with this format " 10/10/20 6:05"

when i try to convert it ,it shows error like this

"
System.FormatException: String was not recognized as a valid DateTime.
"

how can i solve this?

this is my code

What I have tried:

protected void check(object sender, EventArgs e)
       {
           if (FileUpload1.HasFile)
           {
               string filename = "Hr_Report_" + DateTime.Now.ToString("dddd_dd_MMMM_yyyy") + ".xlsx";
               FileUpload1.SaveAs(temp_file + filename);
               string tempfile = temp_file + filename;
               string c = @tempfile;
               DataTable table = new DataTable();

               FileInfo existingFile = new FileInfo(tempfile); //+ FileUpload1.FileName);
               using (ExcelPackage package = new ExcelPackage(existingFile))
               {
                   ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                   int colCount = worksheet.Dimension.End.Column;
                   int rowCount = worksheet.Dimension.End.Row;
                   table.Columns.Add("Emp_NIK", typeof(string));
                   table.Columns.Add("Date_Finger", typeof(DateTime));
                   table.Columns.Add("Time_Finger", typeof(string));
                   table.Columns.Add("Status", typeof(string));

                   for (int i = 1; i < rowCount; i++)
                   {
                       TableRow row = new TableRow();
                       TableCell cell1 = new TableCell();
                       string tanggal = worksheet.Cells[i + 1, 2].Text.ToString();
                       string employee_id = worksheet.Cells[i + 1, 1].Text.ToString();
                       string status = worksheet.Cells[i + 1, 3].Text.ToString();
                       DateTime y = Convert.ToDateTime(tanggal);
                       finger.InsertData(employee_id, "null", status, y, "test ricki");
                   }
               }
           }
           else
           {
               Response.Write("<script>window.alert('File Belum diupload')</script>");
           }
       }
Posted
Updated 12-May-20 19:39pm

1 solution

Replace this:
C#
DateTime y = Convert.ToDateTime(tanggal);

with this:
C#
DateTime y = DateTime.ParseExact(tanggal, "dd/MM/yy H:mm", CultureInfo.InvariantCulture);


Note: I'm not sure what value corresponds to month and day. Please, change it accordingly.

For further details, please see: DateTime.ParseExact Method (System) | Microsoft Docs[^]
 
Share this answer
 
Comments
Garth J Lancaster 13-May-20 2:04am    
Have a '5' :-)
Maciej Los 13-May-20 2:08am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900