Compare Date Created DateTime to DateTime.Today at 18:00, C #

In C #, I need to compare the value of DateTime.Today / 6pm, with the field that the Created DateTime is stored in.

Basically, there is a certain functionality, available only on the same day as the created day, and then only until 6 pm.

The part that I don’t quite understand is how to accurately represent 6pm compared to today. Is there a method that always returns, for example, Midnight, what can I do .AddHours(18); for?

Am I complicating this too much? Thanks.

+4
source share
7 answers
 DateTime created; //get this from wherever DateTime midnight = DateTime.Today; //DateTime.Today returns today date at midnight DateTime sixpm = midnight.AddHours(18); if (created >= midnight && created <= sixpm) { // created is today and prior to 6pm } 
+4
source
 DateTime SixPmToday = DateTime.Now.Date.AddHours(18); 

If you output this, say, to the console, you will have (in my regional settings):

 5/24/2010 6:00:00 PM 
+9
source

You can use DateTime.Today.AddHours(18) . DateTime.Today only gets the current date.

+7
source

try the following:

 var n =DateTime.Now; var today_6pm = new DateTime(n.Year, n.Month, n.Day, 18,0,0) 
+5
source

This is the solution.

 var creationDate = ... // fetch the creation date from somewhere var availableUntil = creationDate.Date.AddHours(18); if (DateTime.Now <= availableUntil) { // The functionality is available. } 

This checks, not until 6 pm on the day of creation.

+1
source

DateTime.Today.AddHours (18);

DateTime.Today returns a DateTime with the current date and 12:00 am as time.

0
source

How about this?

 public static bool IsBefore6PM(System.DateTime _date) { if(_date.CompareTo(System.DateTime.Today.AddHours(18)) < 0 && _date.CompareTo(System.DateTime.Today) >= 0) { return true; } else { return false; } } 

This can also be changed by stating that the actual date is between 8:00 and 18:00.

 public static bool IsBefore6PM(System.DateTime _date) { if(_date.CompareTo(System.DateTime.Today.AddHours(18)) < 0 && _date.CompareTo(System.DateTime.Today.AddHours(8)) >= 0) { return true; } else { return false; } } 
0
source

Source: https://habr.com/ru/post/1310763/


All Articles