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

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

Related

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

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

How To get Interval Between Two Datetime (Timestamp) [duplicate]

This question already has answers here:
Date Difference in ASP.Net
(6 answers)
Closed 3 years ago.
I organized a program which started on 31st December 2018 at 10:00pm hence its been four months ago, i want a way to find this duration by code.
for example , how youtube is able to tell when a comment was written(eg,4years ago,5 months ago).
You can simply substract a DateTime object from another, which results in a TimeSpan representing the difference:
DateTime x = DateTime.Now;
DateTime y = DateTime.Today;
TimeSpan difference = x - y;
var programStartDateTime = new DateTime(2018, 12, 31);
var timeSpan = DateTime.Now - programStartDateTime;
Console.WriteLine($"The difference is: {timeSpan.ToString()}");
I think below sample code may help you
DateTime date1 = DateTime.Now;
DateTime date2 = DateTime.Now.AddDays(-1);
TimeSpan time = date1 - date2;
WriteLine($"TimeSpan : {time}" );

Calculate minutes from a certain time to current time c# [duplicate]

This question already has answers here:
What is the easiest way to subtract time in C#?
(7 answers)
Closed 8 years ago.
I have a certain time ie. 10.30 AM. The user makes entry in a table. If the user makes entry after 10.30 AM , then the number of minutes from 10.30 Am to the current time should be calculated and set to a string.
Here is what I tried uptil now:-
int hour = DateTime.Now.Hour;
int mins = DateTIme.Now.Minute
if(hour>10 && mins >30)
{
string lateBy = "You are late by:"+ //????? how do I calculate this?
}
Any help would be great
Use TimeSpan to find the difference between the 2 datetime values, and use the TotalMinutes property to get the difference in minutes.
DateTime today = DateTime.Today;
DateTime start = new DateTime(today.Year,today.Month,today.Day,10,30,00);
DateTime now = DateTime.Now;
int hour = now.Hour;
int mins = now.Minute;
TimeSpan ts = now.Subtract(start);
if(ts.TotalMinutes > 0) //Time now is after 10:30
{
string lateBy = "You are late by:"+ ts.TotalMinutes.ToString();
}
Ignoring dates and assuming it is always the same day as the 10:30 deadline day:
string lateBy = "You are late by:"+ ((hour-10)*60 + mins-30) + " minutes";
Note this only works before midnight, i.e. for the same day.
Since both are DateTime, you can make use of DateTime.Subract
DateTime a = new DateTime(2014, 06, 20, 10, 30, 00);
DateTime b = DateTime.Now;
Console.WriteLine("You are late by:"+b.Subtract(a).TotalMinutes +"minutes");
Console.ReadLine();

How do I get the system date and time and store in a variable [duplicate]

This question already has answers here:
How to get the current date without the time?
(15 answers)
Closed 9 years ago.
I want to store the current date and time in a variable using C#. How would I do that if it has to be in the format YYYY, MM, DD, HH, MM, SS? I.e. I know I can store midnight on January 1st, 2013 as;
var time= new DateTime(2013, 1, 1, 0, 0, 0);
How do I do this with the system date and time?
var time = DateTime.Now;
Format the time when you retrieve it, not when you store it - i.e. -
string formattedTime = time.ToString("yyyy, MM, dd, hh, mm, ss");
You need DateTime's static field Now:
var time = DateTime.Now;
You can try using the DateTime.Now like this:-
var time = DateTime.Now;
Here it is:
var time = DateTime.Now;
DateTime currentTime = DateTime.Now;

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