Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 971337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:56:56+00:00 2026-05-16T02:56:56+00:00

i have this code to take a time in the past and generate a

  • 0

i have this code to take a time in the past and generate a readable string to represent how long ago it was.

  1. I would have thought Timespan.Hours would give you hours even if its multiple daye in the past but it looks like it breaks it down into its seperate components (days, months, etc). How would i get total hours ago (even if its more than 1 day?

  2. Is there any cleaner way to write this type of code below as it seems pretty spagetti-ish.

Here is the code

        DateTime when = GetDateTimeinPast();
        TimeSpan ts = DateTime.Now.Subtract(when);

        switch (ts.Days)
        {
            case 0:
               if (ts.Hours < 1)
                    b.Append( ts.Minutes + " minutes ago");
               else
                   b.Append( ts.Hours + " hours ago");
                break;
            case 1:
                b.Append( " yesterday");
                break;
            case 2:
            case 3:                
            case 4:

                b.Append( "on " + when.DayOfWeek.ToString());
                break;
            default:
                b.Append(ts.Days + " days ago");
                break;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:56:57+00:00Added an answer on May 16, 2026 at 2:56 am

    Use the TotalHours property or other Total[TimeUnit] properties in the timespan object.

    For a timespan of 1:10 (hh:mm), it equates to 1 Hours and 10 Minutes or 1.167 TotalHours and 70 TotalMinutes.


    As for cleaning it up, stick to using if/else branches as you had earlier. switch/case will not help you with these conditions, only for specific values. Something like this:

    DateTime when = GetDateTimeinPast();
    TimeSpan ts = DateTime.Now.Subtract(when);
    if (ts.TotalHours < 1)
        b.AppendFormat("{0} minutes ago", (int)ts.TotalMinutes);
    else if (ts.TotalDays < 1)
        b.AppendFormat("{0} hours ago", (int)ts.TotalHours);
    //etc...
    

    C# 8 and up, you could use switch expressions and property patterns to condense it further to a single expression.

    (DateTime.Now - when) switch
    {
        { TotalHours: < 1 } ts => $"{ts.Minutes} minutes ago",
        { TotalDays: < 1 } ts => $"{ts.Hours} hours ago",
        { TotalDays: < 2 } => $"yesterday",
        { TotalDays: < 5 } => $"on {when.DayOfWeek}",
        var ts => $"{ts.Days} days ago",
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code like this: // Take the xml message and turn it into
I have this code private void writeReport(IReport report, string reportName) { string reportString =
I have this code I been working on but I'm having a hard time
I have this code to take screenshots of layouts in Android. It is not
If I have this peace of code: <rich:select enableManualInput=true defaultLabel=Select time spent> <f:selectItems value=
I have this code! <div class=time\54> <div class=time\98> <div class=time\69> <div id=result>{INFO}</div> I need
I have this php code that should take all the php files in a
I have this code for doing an ajax request to a webservice: var MyCode
I have this code: $(div[id^='intCell']).mouseover(function() { $(this).css({ border:,1px solid #ff097c}); }).mouseout(function() { $(this).css({border:,1px solid
I have this code to show a map using the Virtual Earth API: <script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.