Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
How to retrieve time as time itself rather than date time while reading to datatable from excel sheet via C# Code behind
Posted
Comments
Richard C Bishop 24-Apr-13 16:16pm    
DateTime has a .ToString() method where you can specify the display. Use "hh:mm:ss" to get only the time.
Sergey Alexandrovich Kryukov 24-Apr-13 16:24pm    
Sorry, a bad idea. One could take TimeOfDay, for example:
http://msdn.microsoft.com/en-us/library/system.datetime.timeofday.aspx
Anything, but not working with string representing time instead of time itself. How could it help?
—SA
Richard C Bishop 24-Apr-13 16:28pm    
You are right, as usual. Thank you for the lesson.
Sergey Alexandrovich Kryukov 24-Apr-13 16:29pm    
You are always very welcome.
—SA
Sergey Alexandrovich Kryukov 24-Apr-13 16:24pm    
What does it mean: "reading to database"? Finally, reading or writing?
—SA

1 solution

Excel stores time values as Date[^] (DateTime[^] in C#). See here: Data types in Data Models (Excel)[^]

I think you need to format date as "time": Custom Date and Time Format Strings[^], String Format For DateTime (C#)[^]

C#
DateTime date1 = new DateTime(2013, 4, 24, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;

Console.WriteLine(date1.ToString("hh:mm:ss.f", ci));
// Displays 07:27:15.0
Console.WriteLine(date1.ToString("hh:mm:ss.F", ci));
// Displays 07:27:15
 
Share this answer
 

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