Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2007

Tip: Looking for answers? Try searching our database.

Merge Time String into a DateTime?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lucius - 28 Jun 2007 20:40 GMT
I have a string[] of time values that look like this:
  "16:00:00"
  "18:00:00"
  "22:30:30"
I need to convert/merge them to a List<DateTime> so they will be
  {1753-01-01 16:00:00}
  {1753-01-01 18:00:00}  
  {1753-01-01 23:30:30}  

What is the best way to do that date/time math?

Thanks.
Mattias Sjögren - 28 Jun 2007 21:03 GMT
>I have a string[] of time values that look like this:
>   "16:00:00"
[quoted text clipped - 6 lines]
>
>What is the best way to do that date/time math?

Something like this ought to do it

string[] times = {"16:00:00", "18:00:00", "22:30:30"};
List<DateTime> dates = new List<DateTime>(times.Length);
foreach (string t in times)
{
    dates.Add(new DateTime(1735, 1, 1) + TimeSpan.Parse(t));
}

I assume it was a typo that turned 22:30 into 23:30.

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Peter Duniho - 28 Jun 2007 21:05 GMT
> [...]
> What is the best way to do that date/time math?

Best?  I don't know.  I can tell you what I'd probably do: use the time to  
instantiate a DateTime object using Parse(), and then use the resulting  
DateTime object's hour, minute, and second properties as part of the  
instantiation of a new DateTime object to put in your List<>.

Pete
Pramod Anchuparayil - 28 Jun 2007 22:48 GMT
Here's an example, I am sure there are other ways....

       private IList<DateTime> ConvertToDateTimeList(string[] datetime)
       {
           return Array.ConvertAll(datetime, new Converter<string,
DateTime>(StringToDate));
       }

       private DateTime StringToDate(string dateTime)
       {
           return DateTime.Parse(dateTime);
       }

> I have a string[] of time values that look like this:
>   "16:00:00"
[quoted text clipped - 8 lines]
>
> Thanks.
Pramod Anchuparayil - 28 Jun 2007 22:52 GMT
> I have a string[] of time values that look like this:
>    "16:00:00"
[quoted text clipped - 8 lines]
>
> Thanks.

Or this...

       private IList<DateTime> ConvertToIList(string[] datetime)
       {
           return Array.ConvertAll(datetime, new Converter<string,
DateTime>(DateTime.Parse));
       }

-Pramod

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.