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 8965749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:52:39+00:00 2026-06-15T16:52:39+00:00

Whats the best way to get a datetime from sql server and convert it

  • 0

Whats the best way to get a datetime from sql server and convert it to use friendly strings like so:

if its more than 1 day > 1 Day ago.
more than 7 days > 1 weeks ago
1 year ago
3 minutes ago
56 seconds ago.

etc. Can this be done easily?

  • 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-06-15T16:52:40+00:00Added an answer on June 15, 2026 at 4:52 pm

    Your best bet is to make a DateTime extension method:

    public static class DateTimeExtensions
    {
        public static string ToRelative(this DateTime value)
        {        
            DateTime now = DateTime.Now; //maybe UtcNow if you're into that
    
            TimeSpan span = new TimeSpan(now.Ticks - value.Ticks);
            double seconds = Math.Abs(ts.TotalSeconds);
    
            if (seconds < 60)
                return string.Format("{0} seconds ago", span.Seconds);
    
            if (seconds < 2700)
                return string.Format("{0} minutes ago", span.Minutes);
    
            if (seconds < 86400)
                return string.Format("{0} hours ago", span.Hours);
    
            // repeat for greater "ago" times...
        }
    }
    

    Then make your call on your DateTime value like so:

    myDateTime.ToRelative();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is best way to get the current datetime as string from JavaScript formatted
Whats the best way to get the current time (HH:MM:SS) using Objective-C. I would
whats the best way to get Statistics Web pages by the users visited ?
What's the best way to get a ol to start from 'x'? The start
What is the best way to get error messages from a WF4 workflow back
What is the best way to get people to fill out a form correctly?
What is the best way to get statistics for an entire subversion repository and
What is the best way to get formatted Int32 numbers? Let say I have
There are many conflicting statements around. What is the best way to get the
What is the best/foolproof way to get the values of environment variables in Windows

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.