Question

I'm trying to merge today's date with an existing time value I have stored in a sql server database. Let me give you an example:

ClientTradedTime = "16:52:01" (this is of type timespan)

I want to merge that value with today's date in variable of type DateTime to use it else where, example:

DateTime mydate;

mydate = "2014-02-04 16:52:01" (this is what I want to see when I store it in my database)

How can I solve this problem?

Was it helpful?

Solution

Just Datime.Add() the TimeSpan to the DateTime's Date property and you will get your desired DateTime like:

DateTime updatedDt = mydate.Date.Add(ClientTradedTime);

Consider the following example:

DateTime myDate = DateTime.Now;
TimeSpan ClientTradedTime = new TimeSpan(16, 52, 51);
DateTime updatedDt = myDate.Date.Add(ClientTradedTime);

This will give you:

updatedDt = {04/02/2014 4:52:51 PM}

myDate.Date would give you DateTime with Time set to 00:00:00 and then you can add your TimeSpan to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top