how can i get date time in c?

I would do something like this, putting all my expiry times into an ordered enumerable of TimeSpan. Please note I’ve not tested this code, so use at your own risk.

I’m also assuming that timezones aren’t an issue, and that the enumerable will always have at least one expiry time.

    public DateTime GetCacheExpiryDateTime(IOrderedEnumerable<TimeSpan> expiryTimes)
    {
        var now = DateTime.Now;
        var isNextDay = false;
    
        // Find the first expiry time in the list which is greater than the current time
        // If there aren't any, set the nextExpiryTime to the first time in the list (next day)
        TimeSpan nextExpiryTime = expiryTimes.FirstOrDefault(time => now.TimeOfDay < time);
        if (nextExpiryTime == default(TimeSpan))
        {
            nextExpiryTime = expiryTimes.FirstOrDefault();
            isNextDay = true;
        }
    
        // If isNextDay is set, then we add a day to our current day
        // and return the Date plus the expiryTime to return the next expiry date and time
        return now.Date.AddDays(isNextDay ? 1 : 0).AddTicks(nextExpiryTime.Ticks);
    }

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top