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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:15:40+00:00 2026-06-13T05:15:40+00:00

For my app I need to know if Now() is between two values. The

  • 0

For my app I need to know if Now() is between two values.

The user can set a start- and an end-time so he will not disturbed by a notification (during the night for example).

So if have got two TimePickers (start- and end-time) that the user can set.

Lets say the user sets 22:00 for the StartTime and 07:00 for the EndTime (this would cover the night).

How can I check if the DateTime.Now is between the selected Start and End time?

EDIT:
I only want this to work with the Hour and minutes part. So if the user sets the Start and End time this should work for every night.

  • 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-13T05:15:41+00:00Added an answer on June 13, 2026 at 5:15 am

    First you need to convert everything to the same units (we’ll use TimeSpan), then you need to see whether the start-end times cross midnight, and finally do your comparison based on the results of that check:

    // convert everything to TimeSpan
    TimeSpan start = new TimeSpan(22, 0, 0);
    TimeSpan end = new TimeSpan(07, 0, 0);
    TimeSpan now = DateTime.Now.TimeOfDay;
    // see if start comes before end
    if (start < end)
        return start <= now && now <= end;
    // start is after end, so do the inverse comparison
    return !(end < now && now < start);
    

    Here’s a function to do it for you:

    bool TimeBetween(DateTime datetime, TimeSpan start, TimeSpan end)
    {
        // convert datetime to a TimeSpan
        TimeSpan now = datetime.TimeOfDay;
        // see if start comes before end
        if (start < end)
            return start <= now && now <= end;
        // start is after end, so do the inverse comparison
        return !(end < now && now < start);
    }
    

    You would call it like:

    bool silenceAlarm = TimeBetween(DateTime.Now, StartTime.Value, EndTime.Value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need my app to show as full screen. Now I know how to
I need to know when my app is being uninstalled from the device where
I need to know which iOS device is currently running app (saying more exactly
Hello Am Currently Doing An App Related To Sound Modulation I need to know
I'm making an app and I'm almost done. I just need to know how
I am working on a Windows 8 app. I need to know how to
Somebody know how use OpenX (php app) with Django? I need to use OpenX
my app need to show user scrollable menu bar under tab bar like fox
I am developing an app using android OS for which I need to know
My Rails app is starting to need complicated queries. Should I just start using

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.