Search code examples
c#azure-table-storageazure-tablequery

how to find the hours between 2 dateTime in azure table storage using c#


I want find the number of hours in azure table storage

public UserTimeSheet GetTotalHoursByYear()
    {
        CloudTable cloudTable = GetCloudTable();
        var query = new TableQuery<DynamicTableEntity>()
        {
            SelectColumns = new List<string>()
        {
            "SignInTime", "SignOutTime"
        }
        };
        var queryOutput = cloudTable.ExecuteQuerySegmented<DynamicTableEntity>(query, null);

        var results = queryOutput.Results.ToList();
        foreach (var entity in results)
        {

        }
        return null;// results.ToList();
    }

I have return the code which select 2 dateTime columns. But i am not getting how get differnce of hours and sum it up. Please me to solve this.


Solution

  • Please use the code below(and please feel free to modify the code to meet your need):

            //other code.
    
            var queryOutput = cloudTable.ExecuteQuerySegmented<DynamicTableEntity>(query, null);
    
            var results = queryOutput.Results.ToList();
            foreach (var entity in results)
            {
                var start = DateTime.Parse(entity.Properties["SignInTime"].StringValue);
                var end = DateTime.Parse(entity.Properties["SignOutTime"].StringValue);                
    
                var diff_hour = end.Subtract(start).Hours;
                Console.WriteLine("the hours between signIn and signOut is: " + diff_hour);
            }
    

    Here is the test result at my side:

    enter image description here

    Note:

    In my table, the type of SignInTime and SignOutTime is String, not DateTime. And if you set the 2 columns' type as DateTime, you just need a little change to my code.

    enter image description here