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 the timedelta() class from the datetime module to add minutes to datetime, e.g. result = dt + timedelta(minutes=10) . The timedelta class can be passed a minutes argument and adds the specified number of minutes to the datetime.
Use the timedelta() class from the datetime module to add hours to datetime, e.g. result = dt + timedelta(hours=10) . The timedelta class can be passed a hours argument and adds the specified number of hours to the datetime. Copied!
To add 30 minutes to a DateTime variable, the following will work: CloseTime = CloseTime. AddMinutes(30);
To add hours in the current date-time, we use AddHours() method of DateTime class in C#. Syntax: DateTime DateTime. AddHours(double);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With