Click here to Skip to main content
11,798,866 members (44,537 online)
Rate this: bad
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 10:07am
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:
Anything, but not working with string representing time instead of time itself. How could it help?
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.
Sergey Alexandrovich Kryukov at 24-Apr-13 16:24pm
What does it mean: "reading to database"? Finally, reading or writing?

1 solution

Rate this: bad
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

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

  Print Answers RSS
0 CPallini 465
1 OriginalGriff 378
2 Maciej Los 305
3 Richard MacCutchan 281
4 phil.o 244
0 OriginalGriff 2,270
1 Maciej Los 1,810
2 KrunalRohit 1,496
3 CPallini 1,425
4 Richard MacCutchan 1,109

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 24 Apr 2013
Copyright © CodeProject, 1999-2015
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