Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
How to retrieve time as time itself rather than date time while reading to datatable from excel sheet via C# Code behind
Posted 24-Apr-13 11:07am
Comments
richcb at 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 at 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
richcb at 24-Apr-13 16:28pm
   
You are right, as usual. Thank you for the lesson.
Sergey Alexandrovich Kryukov at 24-Apr-13 16:29pm
   
You are always very welcome.
—SA
Sergey Alexandrovich Kryukov at 24-Apr-13 16:24pm
   
What does it mean: "reading to database"? Finally, reading or writing?
—SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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#)[^]
 
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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 555
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 359
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 24 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100