0

I have a DateTime property for which I've only set the time:

OpenHour = DateTimeOffset.ParseExact("12:00:00 AM", "hh:mm:ss tt", CultureInfo.InvariantCulture)

But the day has also been saved as the day I've saved this property value. I only wanted to set the time without the day so that it would be the same time every day.

Is there a way to do this ? Or do I need to create as CronJob to update every date's day to today's day ?

Edit

I'm not trying to create a time only value but know which way is best between creating a Cron Job to update the day of the time every day or if there's a better way.

And I tried to save it as strings but I needed to change it to DateTime to be able to use OrderBy when querying the place's list of hours and I've tried with strings it wasn't working.

Thank you for your help

Jimmy
  • 105
  • 15
  • Are you just looking to [represent a time without a date](https://stackoverflow.com/q/2037283/120955)? – StriplingWarrior Sep 06 '21 at 20:49
  • I looked this question up but wanted to make sure what was the best way to do it in my case which is pretty similar but where I could answer people and ask questions – Jimmy Sep 06 '21 at 21:20

1 Answers1

1

It is not possible to save "just the time" as a DateTime object.

A DateTime object actually stores the current time as a single integer, and then when you see the result it is filtered through a certain timezone and format.

"The time component of a DateTimeOffset value is measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar. " *(source) https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset?view=net-5.0

If you want to avoid having to update it every day you could store it as a string representing the time and then have a method to automatically parse that time into todays date into a newly instantiated DateTime object.

        string timeOfDay = DateTime.Now.ToString("hh:mm tt");
        
        static DateTime TodayAtTime(string timeOfDay)
        {
            string calendarDay = DateTime.Now.ToString("MM/dd/yyyy ");
            return DateTime.Parse(calendarDay + timeOfDay);
        }

        DateTime todayAtTime = TodayAtTime(timeOfDay);

        Console.WriteLine(todayAtTime.ToString("hh:mm tt MM/dd/yyyy"));
        Console.WriteLine(todayAtTime.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
Chris Welton
  • 104
  • 8
  • I see, the thing is I had it in strings but then had to switch to datetimes to use OrderBy and select the earliest and latest times for some purposes. But when you say "if you want to avoid having to update it every day", does that mean it's normal to have methods to update the date ? Is it common for systems to use these kinds of systems ? Wouldn't it be a problem when you have to update tens of thousands of DateTime objects ? – Jimmy Sep 06 '21 at 20:55
  • Is this just to do OrderBy on time regardless of date? – Chris Welton Sep 06 '21 at 21:05
  • If so strings should work fine. OrderBy works on strings too. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/sorting-data – Chris Welton Sep 06 '21 at 21:11
  • So it was to do it regardless of date yes, however I'd do it taking the date into account if the date was the current day's day, I had to not take the date into account since the date is outdated, re-create a DateTime object in the method with the time without the date and then use it. So if the DateTime object has the current day's day I would consider it. I tried with strings but it was making my life harder in order to get the earliest opening time vs the latest opening time of the day when you have store hours that go from one day to another – Jimmy Sep 06 '21 at 21:17
  • But if you're telling me it's okay to have a Cron Job that update the day every night at 11:59:30 for example I will do it, I just wanted to know what was the cost when you have hundreds or thousands of times to update – Jimmy Sep 06 '21 at 21:21
  • You should not have to do that much work. "I'd do it taking the date into account if the date was the current day's day, I had to not take the date into account since the date is outdated, " I do not understand what that means. What is it you are actually trying to acocmplish? – Chris Welton Sep 06 '21 at 21:25
  • I'm using OrderBy to get the earliest and latest time without taking the date into account because the date is wrong, but if the date was updating itself everyday to always be the current date I would take the date into account. I'm trying to have the date of the DateTime to be the current date everyday by somehow updating itself. I know strings would be useful but I tried to do it with string and I found out I couldn't really use OrderBy the way I wanted to I used DateTime and it was way easier but now I'm struggling with the date being the current date and not the date of data creation – Jimmy Sep 07 '21 at 07:36