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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:52:39+00:00 2026-05-27T23:52:39+00:00

I want to create some class that can calculate absolute or relative time period.

  • 0

I want to create some class that can calculate absolute or relative time period.
Meaning: the constructor will get either time period and units ( 3 weeks ) or two DateTime start and finish time. I have wrote the following code :

public class TimePeriod
{

    public State TimePerriodState { get; private set; }
    public RelativeTime RelativeTimePeriod { get; private set; }
    public int UnitsOfTime { get; private set; }

    public DateTime? StartTime { get; private set; }
    public DateTime? EndTime { get; private set; }

    public TimePeriod(RelativeTime relativeTime, int unitsOfTime)
    {
        TimePerriodState = State.Absolute;
        RelativeTimePeriod = relativeTime;
        UnitsOfTime = unitsOfTime;
    }

    public TimePeriod(DateTime startTime , DateTime endTime)
    {
        TimePerriodState = State.Relative;
        StartTime = startTime;
        EndTime = endTime;
    }


    public enum State
    {
        None,
        Absolute,
        Relative
    }

    public enum RelativeTime
    {
        None,
        Hours,
        Days,
        Weeks,
        Months,
        Year
    }
}

But I don’t like that the usage is base on the state.
In the end the data will appear in the UI as two different controls. Is there a better way of making the API little better? Maybe pass in the data and calculate the dateTime on the fly or some thing like that?

UPDATE :

the usage is relative to any time its being used. meaning enter a timespan is not possible. lets say we need fo have the relative time prior 10 days from now or fixed days from X to Y

The TimeSpan will be calculated in a different BL class (this is a POCO class)

  • 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-27T23:52:39+00:00Added an answer on May 27, 2026 at 11:52 pm

    You’re basically trying to store the same data twice.

    You can define any period of time by either:

    • A startdate and enddate, duration can be calculated by subtraction.
    • A startdate and duration (TimeSpan), Enddate can be calculated by adding the two.
    • You could do it with a duration and enddate but that’s making things harder.

    What you want to do is pick one way of storing it. If you want it the other way, write a function to calculate it. E.g:

    public TimePeriod(DateTime startTime , DateTime endTime)
    {
        StartTime = startTime;
        EndTime = endTime;
    }
    
    public TimePeriod(DateTime startTime , TimeSpan length)
    {
        StartTime = startTime;
        EndTime = startTime + length;
    }
    
    public TimeSpan GetDuration()
    {
        return EndTime - StartTime;
    }
    
    //Else you can just get the StartTime or EndTime variable (DateTime)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create some kind of AJAX script or call that continuously will
I want to create a class that will mainly house a Vector . The
I want to create a customized UIView class (a static lib) for some app
Let's say I create an instance of a class and want to assign some
I want to create some function for rendering images from within Joomla v1.5.23. That
I want to create some diagrams for some papers. Diagrams will contain some text,
I want create a helper class that containing method like cleanArray, split_char, split_word, etc.
I need to create some simple form that will be use to registration a
I want to create a handler that can be used to handle any event
i want to create some simple wrapper classes for an existing class library. To

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.