How to Get Round off Current Hour in C# [duplicate] - c#

This question already has answers here:
Rounding up a time to the nearest hour
(6 answers)
Closed 1 year ago.
I need to get the round of current hour, for an example date 28/05/2021 2:16 PM , but i need 28/05/2021 2:00 PM just ignore the minute.

This sould work
DateTime date = DateTime.Now;
DateTime formattedDate = date.AddMinutes(-date.Minute);
Or
DateTime date = DateTime.Now;
DateTime formattedDate = date.Date.AddHours(date.Hour);
to also exclude the seconds

Related

Add hours/minute to a datetime variable in C# [duplicate]

This question already has answers here:
Add hours or minutes to the current time
(4 answers)
Closed 5 years ago.
I want to add 30 minutes to my date time variable.
My code:
string time = ViewState["CloseTime"].ToString();
DateTime Closetime = DateTime.ParseExact(time, "HH:mm:ss", CultureInfo.InvariantCulture);
Here my datetime variable is Closetime. I want to add 30 minute to it. How is it possible?
Use:
DateTime currentTime = DateTime.Now;
DateTime x30MinsLater = currentTime.AddMinutes(30);
Console.WriteLine(string.Format("{0} {1}", currentTime, x30MinsLater));
Result:
4/11/2017 3:53:20 PM 4/11/2017 4:23:20 PM
Try AddMinutes(),
DateTime newDate = Closetime.AddMinutes(30);
Simply use CloseTime.AddMinutes(30);. Make sure that this results in a new DateTime object.
var newTime = CloseTime.AddMinutes(30);
To add 30 minutes to a DateTime variable, the following will work:
CloseTime = CloseTime.AddMinutes(30);
There are similar methods for adding seconds, hours, days, etc.
See here for the documentation: Methods for DateTime Struct

Convert string to datetime now format [duplicate]

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 8 years ago.
How I can convert string 23:59 to datetime format?
So that I can reduce the current time in it.
Example:
String = 23:59 minus
Datime.Now = 13.8.2014 10:59:55
Time left 12 hours 0 minutes.
i would use ParseExact
TimeSpan tsDifference = DateTime.ParseExact("23:59", "HH:mm", System.Globalization.CultureInfo.InvariantCulture) - DateTime.Now;
use this :
DateTime date = Convert.ToDateTime(string);
System.TimeSpan diff= DateTime.Now.Subtract(date);
diff will contain your deffernce

How to Get the StartDate based on startDayOfweek and current date in c# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I get the DateTime for the start of the week?
I need to get start date of a week based on a input date and period start DayOfWeek. The week may start either from sunday or monday or etc.
//Ger start date based on a date and startdayofweek
GetStartDate(DateTime date, DayOfWeek periodStartDayOfWeek)
{
// return startdateof week
}
What about this?
while (date.DayOfWeek != periodStartDayOfWeek)
{
date = date.AddDays(-1);
}
return date;

c# previous week start date [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Calculate previous week's start and end date
I need to get the previous week start date in c#
for eg. today is Jan 9th.
previous week start date is Jan 1th.
I am using the following code
DayOfWeek weekStart = DayOfWeek.Sunday;
DateTime startingDate = DateTime.Today;
while (startingDate.DayOfWeek != weekStart)
startingDate = startingDate.AddDays(-1);
DateTime previousWeekStart = startingDate.AddDays(-7);
Is this the best way in c#
Thanks
As previously stated the duplicate's response will probably do for you
mondayOfLastWeek = DateTime.Now.AddDays( -(int)DateTime.Now.DayOfWeek - 6 );
sundayOfLastWeek = DateTime.Now.AddDays(-(int)DateTime.Now.DayOfWeek)
Try:
DateTime.Today.AddDays(-7);

What is the number of days between two dates? [duplicate]

This question already has answers here:
Calculate difference between two dates (number of days)?
(17 answers)
Closed 9 years ago.
We want to find the number of days between two dates. This is simple when the dates are in the same year.
Is there a built-in way to do this if the dates are in different years, or do we just have to loop through each year?
Subtracting a date from another yields a TimeSpan. You can use this to determine the number of whole days using the Days property, or whole and fractional days using the TotalDays property.
DateTime start = ...;
DateTime end = ...;
int wholeDays = (end - start).Days;
or
double totalAndPartialDays = (end - start).TotalDays;
you can probably do something like:
TimeSpan ts = endDate - startDate;
ts.Days
What are you missing?
DateTime - DateTime => Timespan
and Timespan has Days and TotalDays properties.
DateTime date1 = DateTime.Now;
DateTime date2 = new DateTime(date1.Year - 2, date1.Month, date1.Day);
Int32 difference = date1.Subtract(date2).Days;

Categories

Resources