0

I have an interface that is feeding me a datetimeupdated variable with the type of DateTime but I need to read it as type TimeSpan. I'm trying to do the conversion and it kind of worked but not really. I followed this post. Convert DateTime to TimeSpan

Code:

if (e.CmsData != null)
{
    List<NewAgent> newAgentList = new List<NewAgent>();                    

    Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
    {                                         
        foreach (var item in e.CmsData.Agents)
        {
            NewAgent newAgents = new ScoreBoardClientTest.NewAgent();

            newAgents.AgentName = item.AgName;

            newAgents.AgentExtension = item.Extension;

            newAgents.AgentDateTimeChange = ConvertedDateTimeUpdated;

            newAgents.AuxReasons = item.AuxReasonDescription;

            newAgents.LoginIdentifier = item.LoginId;

            ConvertedDateTimeUpdated = item.DateTimeUpdated - new DateTime(01/01/2000);
            time = ConvertedDateTimeUpdated;
            agentTime = time -  DateTime.Now.TimeOfDay;

            newAgents.AgentDateTimeStateChange = agentTime;
            newAgentList.Add(newAgents);
        }

        datagrid.ItemsSource = newAgentList;
    }));                    
}

XAML:

<DataGrid.Columns>
    <DataGridTextColumn Header="Name" Binding="{Binding AgentName}"  />
    <DataGridTextColumn Header="Aux Reason" Binding="{Binding AuxReasons}" />
    <DataGridTextColumn Header="Time" Binding="{Binding AgentDateTimeStateChange}" />
</DataGrid.Columns>

AgentClass

class NewAgent
    {
        public string AgentName { get; set; }
        public int AgentExtension { get; set; }
        public TimeSpan AgentDateTimeChange { get; set; }
        public TimeSpan AgentDateTimeStateChange { get; set; }
        public String AuxReasons { get; set; }
        public string LoginIdentifier { get; set; }

    }

When I print this out to my list I get the below pasted into the fields instead of a format of 00:00:00.

please let me know if this is not enough code to understand what I am trying to accomplish and I'll post all of what I have. Also important to note that item.datetimeupdated is in the format of 00/00/0000 00:00:00.

enter image description here

Community
  • 1
  • 1
mcavanaugh418
  • 127
  • 1
  • 12

1 Answers1

2

I think that what you're trying to accomplish is display the time portion of the DateTime instance; but represented as a TimeSpan.

In which case, this should be all you need.

var timeSpan = item.DateTimeUpdated.TimeOfDay;

If you're trying to display "How long ago from now was this updated?" Then you'll need to do something like..

var timeSpanSince = DateTime.Now - item.DateTimeUpdated;

(These answers do not take into account the complexity caused by time-zones, or whether your database values are stored as UTC etc..)


Edit: Looking at the rest of your code, you might also be interested in these two questions on how to change the way the value is displayed in the data grid:

Community
  • 1
  • 1
Ben Jenkinson
  • 1,806
  • 1
  • 16
  • 31
  • I'm not worried about timezones or how database values are stored. an update will occur every few minutes and this will reset to 00:00:00 so the above should be enough. I'll test and update you if this works for me. Thanks – mcavanaugh418 Mar 30 '17 at 14:39
  • so this did work but I'm still getting it in the format of 00:00:00.0000000 Do you know if its possible to eliminate everything after the decimal point? – mcavanaugh418 Mar 30 '17 at 14:52
  • The grid is effectively doing `timeSpan.ToString()` and displaying the result. I've added some links to other questions that can show how to provide a `StringFormat` in the binding. – Ben Jenkinson Mar 30 '17 at 15:01