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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:11:45+00:00 2026-06-09T21:11:45+00:00

I am having relative datetime string like: 5 minutes ago 10 hours ago 3

  • 0

I am having relative datetime string like:

  • “5 minutes ago”
  • “10 hours ago”
  • “3 days ago” etc.

How can I convert this to exact datetime, as exactly opposite of this question

  • 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-09T21:11:47+00:00Added an answer on June 9, 2026 at 9:11 pm

    This code should work:

            string input = "10 days ago";
    
            DateTime result = DateTime.MinValue;
            int minutesMultiplier = 0;
    
            if (input.Contains("minute"))
                minutesMultiplier = 1;
            else
                if (input.Contains("hour"))
                    minutesMultiplier = 60;
                else
                    if (input.Contains("day"))
                        minutesMultiplier = 1440;
                    else
                        throw new Exception("Couldn't parse time format");
    
            string numberStr = input.Split(' ')[0];
            int number;
            if (int.TryParse(numberStr, out number))
                result = DateTime.Now.AddMinutes(-number * minutesMultiplier);
    

    It does the parsing of an interval name (such as minute, hour, day) and multiplies them to get the number of minutes because later on it uses DateTime.Now.AddMinutes method, same thing could be done using TimeSpan and calling DateTime.Now.Add.

    Here is one more example that handles case of a string that contains more than one interval name, such as “10 hours and 15 minutes ago”:

            // If there are mixed interval types in an input string
            string input = "10 days and 10 hours ago";
    
            // Parse out the intervals and numbers
            var matches = Regex.Matches(input, 
                           @"(?<number>\d+)\s(?<interval>(day)|(minute)|(hour))");
    
            // Convert them to dictionary
            var dic = matches
                .Cast<Match>()
                .ToDictionary(
                    key => key.Groups["interval"].Value, 
                    o => int.Parse(o.Groups["number"].Value));
    
            // Calculate the total number of minutes for each interval
            DateTime result = DateTime.MinValue;
            int totalMinutes = 0;
    
            foreach (var keyValue in dic)
            {
                 if (keyValue.Key.Contains("minute"))
                     totalMinutes += keyValue.Value;
                else
                     if (keyValue.Key.Contains("hour"))
                        totalMinutes += keyValue.Value * 60;
                    else
                         if (keyValue.Key.Contains("day"))
                            totalMinutes += keyValue.Value * 1440;
                        else
                            throw new Exception("Unparsable time format");
            }
    
            result = DateTime.Now.AddMinutes(-totalMinutes);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a Relative layout with few images in it like this below.
I'm having some trouble computing specific days relative to the current date using NSDate
Is there an alternative to having to do something like this: ../../../../index.html when using
I am having a little problem with Relative Layouts. I'm doing a project in
I'm having trouble making divs top position be relative to its preceding subling. I
We are having some issue with our relative path in wordpress. Earlier our application
I'm having a little trouble with relative URIs I have a simple HttpListener app
I have been having problems creating a relative layout in XML for a list
I've found myself having a requirement to configure log4net based on a file relative
I'm having a really difficult time getting eclipse to play nicely with this external

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.