Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify the latest time of day with DateTime

Tags:

.net

datetime

I am using a System.DateTime object to allow a user to select a date range. The user is only able to select a date (not time) using a third party calendar so I will need to automatically specify the time of day it should use (ie: 00:00:00 or 23:59:59) after the date is chosen.

How can I specify the time after the date is already stored as a DateTime object by the calendar selector? I could use the AddHours, AddMinutes, AddSeconds methods but those are relative to the current time which may not be 00:00:00.

The startDate will need to have a time of 00:00:00 and endDate have a time of 23:59:59 to account for the entire days.

like image 440
Joe Phillips Avatar asked May 04 '09 19:05

Joe Phillips


People also ask

Which datetime property will determine the current date and time?

With the Now property of the DateTime , we get the current date and time in local time. Console. WriteLine(now.

How can I change date from datetime?

Summary: in this tutorial, you will learn how to convert a datetime to a DATE by using the CONVERT() , TRY_CONVERT() , and CAST() functions. To convert a datetime to a date, you can use the CONVERT() , TRY_CONVERT() , or CAST() function.

What is Timeofday?

A time interval that represents the fraction of the day that has elapsed since midnight.

How do you declare an hour in Python?

Yes, just do date = datetime. strptime('26 Sep 2012', '%d %b %Y'). replace(hour=11, minute=59) .


1 Answers

If you already have a DateTime object created and want to replace the time with the 11:59:59PM for that given date, then you can use the .Date property to get the date with time set to 00:00:00 and then add the hours, minutes and seconds. For example:

var dt = yourDateInstance.Date.AddHours(23).AddMinutes(59).AddSeconds(59); 

If by latest time, you mean 11:59:59 PM, then this should also work:

var dt = new DateTime(Now.Year, Now.Month, Now.Day, 23, 59, 59); 
like image 168
Jose Basilio Avatar answered Oct 13 '22 05:10

Jose Basilio