Start date - start of a new day at 3 a.m.

I have code that analyzes user behavior on a specific website, it uses many DateTime functions. Now I want to start a new day from 3:00 am instead of 12:00 am , since it is the default, but I really do not want to change any other part of the code.

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

Is there a way to set the “start of new days” without changing other features?

+5
source share
2 answers

There is no date processing for BCL , I read the NodaTime documentation and did not find anything there.

Not knowing how your code works right now, I would recommend that you move on to any of the following ideas; either create an extension method to get 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. I don’t know what you need, but it takes a lot of work to get all the “normal” DateTime methods.

+1
source

Use custom timezone

 var displayName = "(GMT+03:00) Custom/Maxim Dunavicher Time"; var standardName = "Maxim Dunavicher Time"; var offset = new TimeSpan(03, 00, 00); var tz = TimeZoneInfo.CreateCustomTimeZone( standardName, offset, displayName, standardName); 

You calculate everything using UTC. But you are showing using this new time zone.

+1
source

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


All Articles