Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date time-starting a new day at 3:00 am

Tags:

c#

datetime

I have a code that analyzes behavior of users on a certain web site, it uses many of DateTime functions. Now, I want to start a new day from 3:00 am instead of 12:00 am as it is in the default however I really don't want to change any other part of the code.

For example: say I have a DateTime like 2014-08-27t02:59:00 and than I do AddMinutes(2), the date should change to 2014-08-28t03:01:00.

Is there any method to set the "new days beginning" without changing the other functions?

like image 672
Maxim Dunavicher Avatar asked Nov 01 '22 18:11

Maxim Dunavicher


1 Answers

For BCL date handling no, I did a read through NodaTime's documentation and couldn't find anything there as well.

Without knowing to much about how your code works right now I would recommend you to go with either of the following ideas; create an extension method for getting the "correct" date (in your model) or create a new date-class for your purposes.

Extension-method:

public static class DateTimeExtension
{
  public static DateTime GetDay(this DateTime date)
  {
    return date.TimeOfDay > TimeSpan.FromHours(3) ? date.Date : date.AddDays(-1).Date;
  }
}

Or create your own type, MyDateTime, which works the way you want it to. Exactly what you need I do not know, but getting all the "normal" DateTime methods working required quite a bit of work.

like image 67
flindeberg Avatar answered Nov 09 '22 23:11

flindeberg